通知渠道


该页面已存档。请参阅 推送通知指南 了解最新信息。

通知通道是 Android Oreo 中的一项新功能,可让用户更好地控制他们收到的通知。从 Android Oreo 开始,每个本地和推送通知都必须分配给单个通道。用户可以在其操作系统设置中查看所有通知渠道,并且可以根据每个渠道自定义警报的行为。

¥Notification channels are a new feature in Android Oreo that give users more control over the notifications they receive. Starting in Android Oreo, every local and push notification must be assigned to a single channel. Users can see all notification channels in their OS Settings, and they can customize the behavior of alerts on a per-channel basis.

通知通道没有任何效果,并且在所有 iOS 设备上都会被忽略。

¥Notification channels have no effect and are ignored on all iOS devices.

设计渠道

¥Designing channels

通道使用户可以更好地控制他们希望从你的应用接收的各种警报。你应该为可能发送的每种类型的通知创建一个通道,例如警报、聊天消息、更新通知等。

¥Channels give users more control over the various kinds of alerts they want to receive from your app. You should create a channel for each type of notification you might send, such as Alarms, Chat messages, Update notifications, or the like.

根据 Android 开发者文档:

¥According to the Android developer documentation:

你应该为需要发送的每种不同类型的通知创建一个通道。你还可以创建通知渠道来反映应用用户所做的选择。例如,你可以为用户在消息应用中创建的每个对话组设置单独的通知渠道。

¥You should create a channel for each distinct type of notification you need to send. You can also create notification channels to reflect choices made by users of your app. For example, you can set up separate notification channels for each conversation group created by a user in a messaging app.

创建通道时,你可以为其通知指定各种设置,例如优先级、声音和振动。创建通道后,控制权完全切换到用户,然后用户可以根据自己的喜好为每个通道自定义这些设置。你的应用无法再更改任何设置。尽管你的应用可以通过编程方式删除通道,但 Android 不建议这样做,并且会在用户设备上保留一些痕迹。

¥When you create a channel, you can specify various settings for its notifications, such as priority, sound, and vibration. After you create the channel, control switches entirely to the user, who can then customize these settings to their own liking for each channel. Your app can no longer change any of the settings. Although it is possible for your app to programmatically delete channels, Android does not recommend this and keeps a relic on the user's device.

你可以在 Android 开发者网站 上阅读有关通知渠道的更多信息。花一些心思来设计对用户有意义且有用的自定义工具的渠道是个好主意。

¥You can read more about notification channels on the Android developer website. It's a good idea to put some thought into designing channels that make sense and are useful customization tools for your users.

创建和使用渠道

¥Creating and using channels

创建通道很简单 - 在创建本地通知(或接收推送通知)之前,只需调用以下方法:

¥Creating a channel is easy -- before you create a local notification (or receive a push notification), simply call the following method:

if (Platform.OS === 'android') {
  Notifications.createChannelAndroidAsync('chat-messages', {
    name: 'Chat messages',
    sound: true,
  });
}

创建一个已经存在的通道本质上是一个空操作,因此每次应用启动时调用它都是安全的。例如,应用根组件的 componentDidMount 可能是一个好地方。但是,请注意,创建通知通道后你无法更改通知通道的任何设置 - 只有用户可以执行此操作。因此,请务必仔细规划你的渠道。

¥Creating a channel that already exists is essentially a no-op, so it's safe to call this each time your app starts up. For example, the componentDidMount of your app's root component might be a good place for this. However, note that you cannot change any settings of a notification channel after it's been created -- only the user can do this. So be sure to plan your channels carefully.

然后,当你想要发送聊天消息通知时,请将 channelId: 'chat-messages' 字段添加到 推送通知消息 中,或者创建一个本地通知,如下所示:

¥Then, when you want to send a notification for a chat message, either add the channelId: 'chat-messages' field to your push notification message, or create a local notification like this:

Notifications.presentLocalNotificationAsync({
  title: 'New Message',
  body: 'Message!!!!',
  android: {
    channelId: 'chat-messages',
  },
});

然后,Expo 将通过 chat-messages 通道向用户显示你的通知,同时尊重用户对该通道的所有设置。

¥Expo will then present your notification to the user through the chat-messages channel, respecting all the user's settings for that channel.

如果你创建通知但未指定 channelId,Expo 会自动为你创建 '默认' 通道并通过该通道渲染通知。但是,如果你指定尚未在设备上创建的 channelId,则通知将不会在 Android 8+ 设备上显示。因此,提前计划并确保在发送通知之前创建可能需要的所有渠道非常重要。

¥If you create a notification and do not specify a channelId, Expo will automatically create a 'Default' channel for you and present the notification through that channel. If, however, you specify a channelId that has not yet been created on the device, the notification will not be shown on Android 8+ devices. Therefore, it's important to plan ahead and make sure that you create all the channels you may need before sending out notifications.

在不支持通知通道的 Android 7 及更低版本的设备上,Expo 将记住你创建通道的相关设置(在本例中为 sound: true),并将它们直接应用于单个通知,然后再将其渲染给用户。

¥On devices with Android 7 and below, which don't support notification channels, Expo will remember the relevant settings you created the channel with (in this case, sound: true) and apply them directly to the individual notification before presenting it to the user.

更新现有应用以使用通道

¥Updating an existing app to use channels

如果你现有的 Android 应用依赖 soundvibratepriority 通知设置,则需要更新它才能利用渠道,因为这些设置不再对单个通知产生任何影响。如果你不依赖这些设置,你可能仍然需要更新它,以便让用户更好地控制你的应用渲染的不同类型的通知。(请注意,此处涉及的客户端 priority 设置仅影响通知的 UI 行为,与 推送通知的 priority 不同,后者不受通知通道影响。)

¥If you have an existing Android app that relies on sound, vibrate, or priority settings for notifications, you'll need to update it to take advantage of channels, as those settings no longer have any effect on individual notifications. If you do not rely on those settings, you may want to update it anyway to give users more control over the different types of notifications your app presents. (Note that the client-side priority setting involved here only affects the notification's UI behavior and is distinct from the priority of a push notification, which is not affected by notification channels.)

为此,首先请确保你使用的是适用于你的 SDK 版本的最新 expo 次要更新。SDK 22 及更高版本支持通知通道,因此,例如,如果你使用 SDK 27,则应该 npm install/yarn add expo@^27.1.0

¥To do this, first make sure you are using the latest minor update of expo for your SDK version. Notification channels are supported in SDKs 22 and above, so for example, if you're on SDK 27 you should npm install/yarn add expo@^27.1.0.

接下来,规划你的应用所需的通知渠道。这些可能对应于你在各个通知上使用的 soundvibratepriority 设置的不同排列。

¥Next, plan out the notification channels your app will need. These may correspond to the different permutations of the sound, vibrate and priority settings you used on individual notifications.

一旦你决定了一组渠道,你需要向应用添加逻辑来创建它们。我们建议简单地在应用根组件的 componentDidMount 中创建所有通道;这样所有用户都将确保获得所有通道并且不会遗漏任何通知。

¥Once you have decided on a set of channels, you need to add logic to your app to create them. We recommend simply creating all channels in componentDidMount of your app's root component; this way all users will be sure to get all channels and not miss any notifications.

例如,如果这是你之前的代码:

¥For example, if this is your code before:

_createNotificationAsync = () => {
  Notifications.presentLocalNotificationAsync({
    title: 'Reminder',
    body: 'This is an important reminder!!!!',
    android: {
      priority: 'max',
      vibrate: [0, 250, 250, 250],
      color: '#FF0000',
    },
  });
};

你可以把它改成这样:

¥You might change it to something like this:

componentDidMount() {
  // ...
  if (Platform.OS === 'android') {
    Notifications.createChannelAndroidAsync('reminders', {
      name: 'Reminders',
      priority: 'max',
      vibrate: [0, 250, 250, 250],
    });
  }
}

// ...

_createNotificationAsync = () => {
  Notifications.presentLocalNotificationAsync({
    title: 'Reminder',
    body: 'This is an important reminder!!!!',
    android: {
      channelId: 'reminders',
      color: '#FF0000',
    },
  });
}

这将创建一个名为 "提醒事项" 的通道,默认设置为 max 优先级和振动模式 [0, 250, 250, 250]。Android 8 用户可以随时更改这些设置,甚至可以完全关闭 "提醒事项" 通道的通知。当调用 presentLocalNotificationAsync 时,操作系统将读取通道的设置并相应地渲染通知。

¥This will create a channel called "Reminders" with default settings of max priority and the vibrate pattern [0, 250, 250, 250]. Android 8 users can change these settings whenever they want, or even turn off notifications completely for the "Reminders" channel. When presentLocalNotificationAsync is called, the OS will read the channel's settings and present the notification accordingly.

使用 Expo API 服务发送通道通知

¥Send channel notification with Expo API service

[
  {
    to: 'ExponentPushToken[xxxxxx]',
    title: 'test',
    priority: 'high',
    body: 'test',
    sound: 'default', // android 7.0 , 6, 5 , 4
    channelId: 'chat-messages', // android 8.0 later
  },
];
Expo 中文网 - 粤ICP备13048890号