从 Android 7.0(API 级别 24)开始,您可以在一个组中显示相关通知。例如,如果您的应用针对收到的电子邮件显示通知,请将有关新电子邮件的所有通知放入同一个群组中,以便它们收起来。
如需支持较低版本,请添加摘要通知,该摘要通知将单独显示,以汇总所有单独的通知。通常,最好使用收件箱样式的通知来实现此目的。
如果您的用例满足以下所有条件,请使用通知组:
子通知是完整通知,可以单独显示,而无需通知组摘要。
单独显示子级通知有一个好处。例如:
它们是可操作的,具体操作特定于每条通知。
每条通知中都包含更多信息供用户查看。
如果您的通知不符合上述条件,不妨考虑使用新信息更新现有通知,或创建消息样式的通知,以便在同一对话中显示多项更新。
创建通知组并为其添加通知
如需创建通知组,请为该通知组定义一个唯一标识符字符串。然后,对于您想要添加到通知组中的每条通知,调用 setGroup()
并传入通知组名称。例如:
Kotlin
val GROUP_KEY_WORK_EMAIL = "com.android.example.WORK_EMAIL" val newMessageNotification = NotificationCompat.Builder(this@MainActivity, CHANNEL_ID) .setSmallIcon(R.drawable.new_mail) .setContentTitle(emailObject.getSenderName()) .setContentText(emailObject.getSubject()) .setLargeIcon(emailObject.getSenderAvatar()) .setGroup(GROUP_KEY_WORK_EMAIL) .build()
Java
String GROUP_KEY_WORK_EMAIL = "com.android.example.WORK_EMAIL"; Notification newMessageNotification = new NotificationCompat.Builder(MainActivity.this, CHANNEL_ID) .setSmallIcon(R.drawable.new_mail) .setContentTitle(emailObject.getSenderName()) .setContentText(emailObject.getSubject()) .setLargeIcon(emailObject.getSenderAvatar()) .setGroup(GROUP_KEY_WORK_EMAIL) .build();
默认情况下,系统会根据通知的发布时间对其进行排序,但您可以通过调用 setSortKey()
更改通知顺序。
如果针对某个通知组的提醒必须由其他通知处理,请调用 setGroupAlertBehavior()
。例如,如果您只希望通知组的摘要发出通知,则该通知组中的所有子级都必须具有通知组提醒行为 GROUP_ALERT_SUMMARY
。其他选项包括 GROUP_ALERT_ALL
和 GROUP_ALERT_CHILDREN
。
设置通知组摘要
分组通知必须有一个额外的通知来充当通知组摘要。要启用分组通知,您必须设置通知组摘要。此通知组摘要必须包含通知组中其他通知的部分文本,以帮助用户了解通知组的内容。群组摘要的显示方式取决于 Android 版本:
在低于 7.0(API 级别 24)的 Android 版本中,由于无法显示嵌套通知组,系统仅显示您的通知组摘要通知,而隐藏所有其他通知。用户可以点按群组摘要通知以打开您的应用。
在 Android 7.0 及更高版本中,系统会将通知组摘要通知显示为一组嵌套通知,并用每个分组通知的文本摘要进行标记。不会显示您在群组摘要通知中设置的文本。用户可以展开嵌套的通知组以查看该组中的各个通知,如图 1 所示。
即使较新版本的 Android 未显示您设计的通知组摘要文本,您也始终需要手动设置摘要才能启用分组通知。设备组摘要的行为在某些设备类型(例如穿戴式设备)上可能会有所不同。在群组摘要中设置富媒体内容有助于在所有设备和版本上提供最佳体验。
如需添加通知组摘要,请按以下步骤操作:
创建包含通知组说明的新通知 - 通常最好使用收件箱样式的通知来完成。
通过调用
setGroup()
将摘要通知添加到通知组中。通过调用
setGroupSummary(true)
指定必须将其用作通知组摘要。
以下代码展示了创建组摘要的示例:
Kotlin
// Use constant ID for notifications used as group summary. val SUMMARY_ID = 0 val GROUP_KEY_WORK_EMAIL = "com.android.example.WORK_EMAIL" val newMessageNotification1 = NotificationCompat.Builder(this@MainActivity, CHANNEL_ID) .setSmallIcon(R.drawable.ic_notify_email_status) .setContentTitle(emailObject1.getSummary()) .setContentText("You will not believe...") .setGroup(GROUP_KEY_WORK_EMAIL) .build() val newMessageNotification2 = NotificationCompat.Builder(this@MainActivity, CHANNEL_ID) .setSmallIcon(R.drawable.ic_notify_email_status) .setContentTitle(emailObject2.getSummary()) .setContentText("Please join us to celebrate the...") .setGroup(GROUP_KEY_WORK_EMAIL) .build() val summaryNotification = NotificationCompat.Builder(this@MainActivity, CHANNEL_ID) .setContentTitle(emailObject.getSummary()) // Set content text to support devices running API level < 24. .setContentText("Two new messages") .setSmallIcon(R.drawable.ic_notify_summary_status) // Build summary info into InboxStyle template. .setStyle(NotificationCompat.InboxStyle() .addLine("Alex Faarborg Check this out") .addLine("Jeff Chang Launch Party") .setBigContentTitle("2 new messages") .setSummaryText("janedoe@example.com")) // Specify which group this notification belongs to. .setGroup(GROUP_KEY_WORK_EMAIL) // Set this notification as the summary for the group. .setGroupSummary(true) .build() NotificationManagerCompat.from(this).apply { notify(emailNotificationId1, newMessageNotification1) notify(emailNotificationId2, newMessageNotification2) notify(SUMMARY_ID, summaryNotification) }
Java
// Use constant ID for notifications used as group summary. int SUMMARY_ID = 0; String GROUP_KEY_WORK_EMAIL = "com.android.example.WORK_EMAIL"; Notification newMessageNotification1 = new NotificationCompat.Builder(MainActivity.this, CHANNEL_ID) .setSmallIcon(R.drawable.ic_notify_email_status) .setContentTitle(emailObject1.getSummary()) .setContentText("You will not believe...") .setGroup(GROUP_KEY_WORK_EMAIL) .build(); Notification newMessageNotification2 = new NotificationCompat.Builder(MainActivity.this, CHANNEL_ID) .setSmallIcon(R.drawable.ic_notify_email_status) .setContentTitle(emailObject2.getSummary()) .setContentText("Please join us to celebrate the...") .setGroup(GROUP_KEY_WORK_EMAIL) .build(); Notification summaryNotification = new NotificationCompat.Builder(MainActivity.this, CHANNEL_ID) .setContentTitle(emailObject.getSummary()) // Set content text to support devices running API level < 24. .setContentText("Two new messages") .setSmallIcon(R.drawable.ic_notify_summary_status) // Build summary info into InboxStyle template. .setStyle(new NotificationCompat.InboxStyle() .addLine("Alex Faarborg Check this out") .addLine("Jeff Chang Launch Party") .setBigContentTitle("2 new messages") .setSummaryText("janedoe@example.com")) // Specify which group this notification belongs to. .setGroup(GROUP_KEY_WORK_EMAIL) // Set this notification as the summary for the group. .setGroupSummary(true) .build(); NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this); notificationManager.notify(emailNotificationId1, newMessageNotification1); notificationManager.notify(emailNotificationId2, newMessageNotification2); notificationManager.notify(SUMMARY_ID, summaryNotification);
摘要通知 ID 必须保持不变,以便仅发布一次,以便日后在摘要信息发生更改时进行更新。后续向该组添加内容会导致更新现有摘要。
如需查看使用通知的示例代码,请参阅 Android 通知示例。
自动分组
在 Android 7.0(API 级别 24)及更高版本中,如果您的应用发送了 4 条或更多通知,并且未指定组键或组摘要,系统可能会自动将这些通知分为一组。系统会自动分组显示通知,并附带通知组摘要通知,该摘要通知会标有某些分组通知的文本片段。与手动分组的通知一样,用户可以展开此摘要通知以查看各个通知。
自动分组行为在某些设备类型上可能会有所不同。为了在所有设备和版本上提供最佳体验,如果您知道通知必须分组,请指定组键和组摘要,以确保这些通知已分组。