Starting in Android 7.0 (API level 24), you can display related notifications in a group (previously called "bundled" notifications). For example, if your app shows notifications for received emails, you should put all notifications into the same group so they can be collapsed together.
To support older versions, you can also add a summary notification that appears alone to summarize all the separate notifications—this is often best done with the inbox-style notification.
You should use notification groups if all of the following conditions are true for your use case:
- The child notifications are complete notifications and can be displayed individually without the need for a group summary.
- There is a benefit to surfacing the child notifications individually. For example:
- They are actionable, with actions specific to each notification.
- There is more information in each notification that the user should see.
If your notifications don't meet the above criteria, you should instead consider updating an existing notification with new information or create a message-style notification to show multiple updates in the same conversation.
Create a group and add a notification to it
To create a notification group, define a unique identifier string
for the group. Then, for each notification you want in the group, just
setGroup(), passing the group name. For example:
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();
By default, notifications are sorted according to when they were posted, but you
can change order by calling
If alerts for a notification's group should be handled by a different
setGroupAlertBehavior(). For example, if you want only the summary of
your group to make noise, all children in the group should have the group alert
The other options are
Set a group summary
On Android 7.0 (API level 24) and higher, the system automatically builds a summary for your group using snippets of text from each notification. The user can expand this notification to see each separate notification, as shown in figure 1. To support older versions, which cannot show a nested group of notifications, you must create an extra notification that acts as the summary. This appears as the only notification and the system hides all the others. So this summary should include a snippet from all the other notifications, which the user can tap to open your app.
To add a group summary, proceed as follows:
- Create a new notification with a description of the group—often best done with the inbox-style notification.
- Add the summary notification to the group by calling
- Specify that it should be used as the group summary by calling
//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("firstname.lastname@example.org")) //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);
The summary notification ID should stay the same so that it is only posted once, and so you can update it later if the summary information changes (subsequent additions to the group should result in updating the existing summary).