Notification / Notification.Builder / Nougat

Nougat – Bundled Notifications

Nougat has now been released in to the wild and there are a number of new features which developers should be considering. In this loosely-related series of articles we’re going to be looking at various aspects of these new features to see how we can best make use of them. In this article we’re going to look at one of the new Nougat Notification types.

nougat-smallNotifications have been extended in Nougat and we now have some additional ways of presenting notifications. Being a (pseudo!) messaging app which is generating a number of distinct notifications over a period of time, it may be good to group these notifications together and ‘Bundled Notifications’ enable us to do precisely that. Anyone who has developed notifications for Android Wear may already be familiar with the concept of stacking notifications. Bundled notifications work in exactly the same way, and the API is pretty much identical. But for those unfamiliar with stacking notifications we’ll give a ful explanation of how to implement bundled notifications.

Firstly it is important to understand the behaviour that the user will see. On a Nougat or later device bundled notifications will appear as a list of the individual notifications with the newest at the top under a common header:

bundled-closed-cropped

There is an indication that there are further notification – the +2 in the screen capture. If the user taps on the header, then the notification list will expand to show details of all of the notifications:

bundled-open-cropped

The benefits to the user should be pretty obvious – if we are producing a large number of distinct notifications, then by bundling them in this way we allow the system to present them to the user without overwhelming him/her with a large volume because the collapsed view will be shown. Also the volume of our notifications will not hide notifications from other apps which may be important to the user.

So what happens on older devices? Bundled notifications on a Marshmallow device look something like this:

bundled-legacy-cropped

Essentially the benefits are the same – a large number of notifications from our app will not overwhelm the user, but there is much less detail – and the user will not see any individual notifications. We can control what goes in to this single notification and also add an action which will enable the user to open the app directly to the list of unread messages, if necessary. The important thing is to consider both Nougat+ users and legacy users when designing the notifications.

So to implement this we’ll create a NotificationBuilder class:

We may need to generate messages before the user has unlocked the device following a reboot so it is important to use device storage for our SharedPreferences, if it’s available.

Let’s look at how we actually generate the notifications:

The sendBundledNotification() method is what we call to add a new notification. This builds two separate notifications – the message notification and the summary notification. In the earlier examples each individual message which appeared on Nougat+ devices was the message notification; and the single notification which appeared on pre-Nougat devices was the summary notification. What binds these together is using an identical group key for both notifications – which gets applied to the notification by calling setGroup() on the notification Builder.

Another important point is that the message notification gets posted using a uniquely generated notification ID (we store an ID field in SharedPreferences and increment it for each new notification); whereas the summary message uses a fixed ID. This allows us to add new notifications for each message while only providing a single summary notification.

The summary message needs to be flagged as a summary message by calling setGroupSummary(true) on the notification Builder. The official documentation that I’ve seen strongly recommend the user of a summary notification. However, in my testing, my experience was that messages will simply show as individual notifications (and won’t get bundled) if you don’t provide a summary notification. So it’s really more a requirement than recommendation.

Finally, it is worth pointing out that I’m using NotificationCompat throughout which is always good practice. Furthermore setGroup() and setGroupSummary() are only available in API 20 and later so you’ll have to use this if you’re targeting APIs below 20.

All that remains is to create notifications from our MessengerService:

So that’s it for bundled notifications, however there is another way of presenting notifications which is particularly well suited to messaging apps and we’ll take a look at that in the next article.

The source code for this article is available here.

© 2016, Mark Allison. All rights reserved.

CC BY-NC-SA 4.0 Nougat – Bundled Notifications by Styling Android is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. Permissions beyond the scope of this license may be available at http://blog.stylingandroid.com/license-information.

Leave a Reply

Your email address will not be published. Required fields are marked *