创建通知分组

从 Android 7.0(API 级别 24)开始,您可以将相关通知分组显示。例如,如果您的应用显示收到的电子邮件的通知,您可以将所有新电子邮件消息的通知放入同一组中,以便它们折叠在一起显示。

为了支持旧版本,请添加单独显示的摘要通知,以汇总所有不同的通知。这通常使用收件箱样式通知最能实现。

图 1. 折叠(顶部)和展开(底部)的通知分组。

如果您的用例满足以下所有条件,请使用通知分组

  • 子通知是完整的通知,可以单独显示,无需分组摘要。

  • 单独显示子通知是有好处的。例如

    • 它们是可执行的,且操作针对每个通知。

    • 每个通知中都有更多信息供用户查看。

如果您的通知不满足上述条件,请考虑更新现有通知以包含新信息,或创建消息样式通知以在同一对话中显示多个更新。

创建分组并将通知添加到其中

要创建通知分组,请为该分组定义一个唯一的标识符字符串。然后,对于分组中的每个通知,调用 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_ALLGROUP_ALERT_CHILDREN

设置分组摘要

分组通知必须有一个充当分组摘要的额外通知。要启用分组通知,您必须设置分组摘要。此分组摘要必须包含分组中其他每个通知的一些文本,以帮助用户了解分组中的内容。分组摘要的显示方式取决于 Android 版本

  • 在 Android 7.0(API 级别 24)之前的版本上,由于无法显示嵌套的通知分组,系统只会显示您的分组摘要通知并隐藏所有其他通知。用户可以点按分组摘要通知来打开您的应用。

  • 在 Android 7.0 及更高版本上,系统会将您的分组摘要通知显示为嵌套的通知分组,并标有每个分组通知的文本片段。它不会显示您在分组摘要通知上设置的文本。用户可以展开嵌套的通知分组以查看分组中的各个通知,如图 1 所示。

即使较新版本的 Android 不显示您设计的分组摘要文本,您也始终需要手动设置摘要以启用分组通知。分组摘要的行为可能会因某些设备类型(例如可穿戴设备)而异。在分组摘要上设置富文本内容有助于在所有设备和版本上提供最佳体验。

要添加分组摘要,请按如下步骤操作

  1. 创建带有分组描述的新通知——通常使用收件箱样式通知最能实现。

  2. 通过调用 setGroup() 将摘要通知添加到分组。

  3. 通过调用 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)及更高版本上,如果您的应用发送通知但未指定分组键或分组摘要,系统可能会自动将它们分组。自动分组的通知会显示带有分组摘要通知,并标有部分分组通知的文本片段。用户可以展开此摘要通知以查看每个单独的通知,这与手动分组的通知类似。

自动分组行为可能因某些设备类型而异。为了在所有设备和版本上提供最佳体验,如果您知道通知必须分组,请指定分组键和分组摘要以确保它们被分组。