建立通知群組

從 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_ALL敬上 和 GROUP_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("[email protected]"))
        // 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("[email protected]"))
        // 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 通知 範例 ,直接在 Google Cloud 控制台實際操作。

自動分組

在 Android 7.0 (API 級別 24) 以上版本中,如果應用程式傳送四個以上 如未指定群組鍵或群組摘要,系統可能會 自動分組已分組的通知會自動顯示 群組摘要通知中,有部分項目是內容片段 。使用者只要展開這則摘要通知,即可查看 個別通知,就像手動分組的通知一樣。

自動分組的行為可能會因裝置類型而異。提供最優質的 在所有裝置上和版本中呈現通知 但如果您知道必須開啟通知 群組,請指定群組索引鍵和群組摘要,確保兩者皆已分組。