从 Android 8.0(API 级别 26)开始,所有通知都必须分配到一个渠道。对于每个渠道,您可以设置应用于该渠道中的所有通知的视觉和听觉行为。用户可以更改这些设置,并决定应用中的哪些通知渠道具有干扰性或可见。
请观看以下视频,简要了解 Android 8.0 中的渠道和其他通知功能。
您可以在系统设置中找到每个应用的通知渠道用户设置,如图 1 所示。
创建通知渠道后,您将无法更改通知行为。此时,用户拥有完全的控制权。但是,您仍然可以更改渠道的名称和说明。
为您需要发送的每种通知分别创建一个渠道。您还可以创建通知渠道来反映用户的选择。例如,您可以为用户在即时通讯应用中创建的每个对话组设置单独的通知渠道。
如果您以 Android 8.0(API 级别 26)或更高版本为目标平台,则必须实现一个或多个通知渠道。如果您的 targetSdkVersion
设为 25 或更低版本,当应用在搭载 Android 8.0(API 级别 26)或更高版本的设备上运行时,其行为与在搭载 Android 7.1(API 级别 25)或更低版本的设备上相同。
创建通知渠道
如需创建通知渠道,请按以下步骤操作:
构建一个具有唯一渠道 ID、用户可见名称和重要性级别的
NotificationChannel
对象。(可选)使用
setDescription()
指定用户在系统设置中看到的说明。注册通知渠道,方法是将其传递给
createNotificationChannel()
。
以下示例展示了如何创建和注册通知渠道:
Kotlin
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { // Create the NotificationChannel. val name = getString(R.string.channel_name) val descriptionText = getString(R.string.channel_description) val importance = NotificationManager.IMPORTANCE_DEFAULT val mChannel = NotificationChannel(CHANNEL_ID, name, importance) mChannel.description = descriptionText // Register the channel with the system. You can't change the importance // or other notification behaviors after this. val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager notificationManager.createNotificationChannel(mChannel) }
Java
private void createNotificationChannel() { // Create the NotificationChannel, but only on API 26+ because // the NotificationChannel class is not in the Support Library. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { CharSequence name = getString(R.string.channel_name); String description = getString(R.string.channel_description); int importance = NotificationManager.IMPORTANCE_DEFAULT; NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance); channel.setDescription(description); // Register the channel with the system. You can't change the importance // or other notification behaviors after this. NotificationManager notificationManager = getSystemService(NotificationManager.class); notificationManager.createNotificationChannel(channel); } }
使用原始值重新创建现有通知渠道不会执行任何操作,因此在启动应用时可以放心地调用此代码。
默认情况下,发布到指定渠道的所有通知均使用由 NotificationManagerCompat
类中重要性级别(如 IMPORTANCE_DEFAULT
或 IMPORTANCE_HIGH
)定义的所有通知。如需详细了解重要性级别,请参阅下一部分。
如果您想进一步自定义频道的默认通知行为,可以对 NotificationChannel
调用 enableLights()
、setLightColor()
和 setVibrationPattern()
等方法。请注意,创建渠道后,您将无法更改这些设置,并且对于是否启用这些行为,用户拥有最终控制权。
您还可以通过调用 createNotificationChannels()
在一次操作中创建多个通知渠道。
设置重要性级别
频道重要性会影响在频道中发布的所有通知的干扰级别。在 NotificationChannel
构造函数中使用从 IMPORTANCE_NONE(0)
到 IMPORTANCE_HIGH(4)
的五个重要性级别之一进行指定。
为了支持搭载 Android 7.1(API 级别 25)或更低版本的设备,您还必须使用 NotificationCompat
类中的优先级常量为每个通知调用 setPriority()
。
重要性 (NotificationManager.IMPORTANCE_*
) 和优先级 (NotificationCompat.PRIORITY_*
) 常量会映射到用户可见的重要性选项,如下表所示。
用户可见的重要性级别 | 重要性(Android 8.0 及更高版本) | 优先级(Android 7.1 及更低版本) |
---|---|---|
紧急 :发出提示音,并以浮动通知的形式显示。 |
IMPORTANCE_HIGH |
PRIORITY_HIGH 或 PRIORITY_MAX |
高 发出声音。 |
IMPORTANCE_DEFAULT |
PRIORITY_DEFAULT |
中 :不发出声音。 |
IMPORTANCE_LOW |
PRIORITY_LOW |
低 :不发出提示音,且不会在状态栏中显示。 |
IMPORTANCE_MIN |
PRIORITY_MIN |
无 :不发出提示音,且不会在状态栏或通知栏中显示。 |
IMPORTANCE_NONE |
N/A |
无论重要程度如何,所有通知都会在非干扰系统界面位置显示,例如,显示在抽屉式通知栏中,以及在启动器图标上作为标志显示,不过您可以修改通知标志的外观。
将渠道提交到 NotificationManager
后,便无法更改重要性级别。不过,用户可以随时更改他们对应用频道的偏好设置。
如需了解如何选择适当的优先级,请参阅通知设计指南中的“优先级”部分。
读取通知渠道设置
用户可以修改通知渠道的设置,包括振动和提醒提示音等行为。如果您想了解用户对您的通知渠道应用的设置,请按以下步骤操作:
通过调用
getNotificationChannel()
或getNotificationChannels()
获取NotificationChannel
对象。查询特定的渠道设置,例如
getVibrationPattern()
、getSound()
和getImportance()
。
如果您检测到某项渠道设置阻碍了应用的预期行为,可以建议用户更改该设置,并提供一项用于打开渠道设置的操作,如下一部分所示。
打开通知渠道设置
创建通知渠道后,您便无法以编程方式更改通知渠道的视觉和听觉行为。只有用户可以在系统设置中更改频道行为。为了让用户能够轻松访问这些通知设置,请在应用的设置界面中添加一个用于打开这些系统设置的项。
您可以通过使用 ACTION_CHANNEL_NOTIFICATION_SETTINGS
操作的 Intent
打开通知渠道的系统设置。
例如,以下示例代码展示了如何将用户重定向到通知渠道的设置:
Kotlin
val intent = Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS).apply { putExtra(Settings.EXTRA_APP_PACKAGE, packageName) putExtra(Settings.EXTRA_CHANNEL_ID, myNotificationChannel.getId()) } startActivity(intent)
Java
Intent intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS); intent.putExtra(Settings.EXTRA_APP_PACKAGE, getPackageName()); intent.putExtra(Settings.EXTRA_CHANNEL_ID, myNotificationChannel.getId()); startActivity(intent);
请注意,该 intent 需要两个 extra,用于指定应用的软件包名称(也称为应用 ID)和要修改的渠道。
删除通知渠道
您可以通过调用 deleteNotificationChannel()
删除通知渠道。以下示例代码演示了如何完成此过程:
Kotlin
// The id of the channel. val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager val id: String = "my_channel_01" notificationManager.deleteNotificationChannel(id)
Java
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); // The id of the channel. String id = "my_channel_01"; notificationManager.deleteNotificationChannel(id);
创建通知渠道分组
如果您希望进一步整理频道在设置界面中的外观,可以创建渠道组。如果您的应用支持多个用户帐号(例如用于工作资料的帐号),这是个好主意,因为这样可让您为每个帐号创建一个通知渠道分组。这样,用户就可以轻松识别和控制具有相同名称的多个通知渠道。
例如,社交网络应用可能支持个人帐号和工作帐号。在这种情况下,每个帐号可能需要多个具有相同功能和名称的通知渠道,如下所示:
具有两个渠道的个人帐号:
新评论
帖子推荐
具有两个渠道的企业帐号:
新评论
帖子推荐
通过将每个帐号的通知渠道整理成组,用户可以区分这些渠道。
每个通知渠道组都需要一个 ID(在您的软件包中必须是唯一的)以及用户可见的名称。以下代码段演示了如何创建通知渠道分组。
Kotlin
// The id of the group. val groupId = "my_group_01" // The user-visible name of the group. val groupName = getString(R.string.group_name) val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager notificationManager.createNotificationChannelGroup(NotificationChannelGroup(groupId, groupName))
Java
// The id of the group. String groupId = "my_group_01"; // The user-visible name of the group. CharSequence groupName = getString(R.string.group_name); NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.createNotificationChannelGroup(new NotificationChannelGroup(groupId, groupName));
创建新组后,您可以调用 setGroup()
将新的 NotificationChannel
对象与该组相关联。
将渠道提交给通知管理器后,您便无法更改通知渠道与通知组之间的关联。