创建一组通知

从 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 7.0(API 级别 24)及更高版本上,系统会使用每条通知中的文本摘要,自动为您的通知组创建摘要。用户可以展开此通知以查看每条单独的通知,如图 1 所示。要支持无法显示嵌套通知组的较低版本,您必须另外创建一条通知来充当摘要。这是显示的唯一通知,系统会隐藏所有其他通知。因此,此摘要应包含所有其他通知的片段,供用户点按以打开您的应用。

如需添加通知组摘要,请按以下步骤操作:

  1. 使用通知组说明创建新通知 - 通常最好使用收件箱样式的通知实现此目的。
  2. 通过调用 setGroup() 将摘要通知添加到通知组中。
  3. 通过调用 setGroupSummary(true) 指定将其用作通知组摘要。

例如:

Kotlin

    //use constant ID for notification 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 notification 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 通知示例