通知渠道


警告 本页面已存档。有关最新信息,请参阅推送通知指南

通知渠道是 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:

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

当你创建通道时,可以为其通知指定各种设置,例如优先级、声音和振动。创建通道后,控制权完全转移到用户手中,用户可以为每个通道自定义这些设置。你的应用无法再更改任何设置。虽然你的应用可以以编程方式删除通道,但 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 设置仅影响通知的界面行为,与推送通知的 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 }, ];