建立通知群組

從 Android 7.0 (API 級別 24) 開始,您可以在群組中顯示相關通知。舉例來說,如果應用程式會顯示收到電子郵件的通知,請將所有新電子郵件通知放在同一個群組,讓它們可以一起收合。

如要支援舊版,請新增單獨顯示的摘要通知,以摘要方式列出所有通知。這種管理方式通常是使用收件匣樣式的通知來達成。

圖 1 收合 (頂端) 和展開 (底部) 的通知群組。

如果您的用途符合下列所有條件,請使用通知群組:

  • 子通知就是完整的通知,可以單獨顯示,而不需要群組摘要。

  • 顯示個別子項通知有好處。例如:

    • 這些通知可以採取行動,且每個通知的特定動作都可以執行。

    • 每則通知中都會提供更多資訊,讓使用者查看。

如果您的通知不符合上述條件,請考慮更新現有通知以加入新資訊,或是建立訊息樣式通知,以便在同一個對話中顯示多項更新資訊。

建立群組並新增通知

如要建立通知群組,請定義該群組的專屬 ID 字串。接著,針對您要在群組中的每則通知,呼叫 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) 以上版本中,如果應用程式傳送四則以上的通知,且未指定群組鍵或群組摘要,系統可能會自動將這些內容分為一組。系統會自動顯示分組通知,並提供群組摘要通知,並加上部分已分組通知的文字片段。使用者可以展開此摘要通知,查看每則通知,就像手動將的通知分組一樣。

自動分組功能的運作方式可能會因裝置類型而異。為了讓所有裝置和版本都能提供最佳體驗,如果您知道通知必須分組,請指定群組索引鍵和群組摘要,確保通知可以分組。