651.288.7000 info@intertech.com

Android Nougat Development Tutorial (Part 2): Nougat Bundled Notifications

by | Apr 18, 2017

Introduction

Android 7, also known as Nougat, introduces several new features. This blog post demonstrates one enhancement made to Notifications: Bundled Notifications.

This feature allows related notifications to be grouped together. The group can be expanded to reveal the individual notifications. This allows an application to display several different messages without overwhelming the notification drawer.

Bundled notifications share some similarities with InboxStyle. They both display several messages in a compact way. The difference is that bundled notifications enable the user to view the details of each individual notification, whereas inbox style is for a single notification that contains multiple lines (each line might contain the subject of an email, for example).

The code used in this tutorial is available in the demo app.

 

When To Use Bundled Notifications

When is it beneficial to use bundle notifications, as opposed to inbox style? Since bundled notifications can be viewed and selected individually (by expanding the group), they are beneficial if the notifications require independent actions, or the contents are better viewed independently. Consider the following examples:

  • You have a messaging app, and you want to allow the user to delete messages. In this case, you likely want to allow that action on individual notifications.
  • You are using BigTextStyle, which displays additional text when the notification is selected. Inbox style only allows a single line per message, whereas bundled notifications can be expanded individually to show the addition text, thereby taking advantage of the BigTextStyle.

Keep in mind that when bundling notifications, the summary notification (set using setGroupSummary) is not displayed. So, if it is important that your notifications have a summary, bundling may not be the way to go.

 

Bundled Notifications In Action

To help understand bundled notifications, let’s see them in action. First, let’s review what a notification with InboxStyle looks like, so that we can see how bundled notifications differ.

InboxStyle, expanded

InboxStyle, expanded

 

InboxStyle, collapsed

InboxStyle, collapsed

 

The inbox style notification can be expanded and collapsed. When collapsed, it shows the basic text (set with setContentText). When expanded, it shows the messages that were set on the inbox style using addLine. The thing to note here is that this is a single notification. Messages cannot be expanded individually, nor can actions be set individually.

Here are the same messages, using bundled notifications in Android 7 Nougat:

Bundled Notifications, collapsed

Bundled Notifications, collapsed

 

nougat_bundled_expanded

Bundled Notifications, expanded

 

The “bundle” of notifications is collapsed initially. Each notification is compacted to a single line which contains the title and a portion of the text.

When the group is expanded, the notifications display individually. Each notification can be expanded, if there is more content (as is case with big text style). Expanding a notification will also reveal its actions, if there are any. In the example above, notification #2 is expanded to reveal the full text and the action.

 

Creating Bundled Notifications

Creating bundled notifications is only slightly different than any other notification. You simply use setGroup to set a key for the group. Notifications with the same key will be grouped together. For example:

You would use similar logic to create more notifications with the same group key. Of course, the message and maybe the title will be different.

The next step is very important: each notification in the group must have a unique ID (the ID is the first parameter passed into notify). If the IDs are not unique, each notification will replace the previous one, which is not what we want. In the demo app, I am simply keeping a counter to track the number of “bundled” notifications, and adding that to an arbitrary constant:

How you accomplish this doesn’t really matter, as long as each notification ID is unique to your application.

 

Backwards Compatibility

On systems prior to Nougat (API level 24), calling setGroup will have no effect. Each of the notifications will display individually. If you want to avoid this, you can create a summary notification for the group. Simply call setGroupSummary(true) on the summary notification, and also remember to call setGroup using the same group key.

On systems prior to Nougat, the summary notification will be used instead of bundled notifications. On Nougat and above, the summary is not used at all. Here is how that looks in the demo app. The summary is using InboxStyle:

As long as you are using classes from the support library (e.g. NotificationManagerCompat) you don’t need any logic to detect the API level. You can simply issue the bundled notifications, as well as the summary notification, and the library will handle the rest.

 

Using Actions On Bundled Notifications

As previously mentioned, one of the benefits of bundled notifications is that each notification can have its own actions. However, you need to take care with the PendingIntents used with the actions. You need to be aware that Android may consider your pending intents to be the same. That is why when creating a pending intent, you must provide a flag that tells Android what to do in case an “equal” intent already exists (for example, cancel or update the current one).

Depending on your needs, it may be necessary to ensure that each pending intent remains distinct. For example, say we wanted an action to delete a message. In that case, we may add extra data to the intent (e.g. putExtra) that represents a unique ID, so we know what message to delete.  Adding an extra does not make an intent distinct (see the documentation to learn what makes a pending intent distinct).

One simple way to ensure that intents are distinct is to use unique request codes. The request code is the second parameter passed into getActivity and getService. Previously, I mentioned the need for unique notification IDs when creating bundled notifications. The demo app uses the same technique to ensure the request codes are also unique.

 

Conclusion

Bundled notifications are a new feature in Android 7 Nougat. In this tutorial, we discussed the advantages of bundled notifications, as well as how to implement them.

To learn about another new feature introduced in Android 7 Nougat, see tutorial on Inline Reply Actions.

Both of these new features are demonstrated in the demo application.

Working with Android 7 Nougat? We can help you implement these new features and even help create the Android app that you are building. Take a look at what we can do on our Android Consulting page.

Intertech also offers Android Training! See if you can find a course that best fits you and your team.

Follow Us

Categories

Pin It on Pinterest

Share This

Like What You've Read?

Subscribe to the Blog.

Every Friday we send that week's content from our Developers via email. Try it out!

Some ad blockers can block the form below.

You have Successfully Subscribed!