Android Notifications#3: Expandable Notifications and Notifications Groups
In the first blog of this series, we saw some basic concepts related to notifications and also learned how to make a basic notification using channels. In the second, we saw how to set actions and some properties to have better control of our notifications.
In this blog, we will take a step further and learn how to build expandable and time-sensitive notifications. We will also have a look at notification groups.
Expandable notification:
By default, the notification’s text content is truncated to fit on one line. If you want your notification to be longer, you can enable a larger text area that’s expandable by applying an additional template, as shown below.
Building Expandable Notification:
To start, build a basic notification, as discussed in the first blog of the series. Then, call setStyle()
with a style object and supply information corresponding to each template, as shown below.
1. Add a large image
To add an image in your notification, pass an instance of NotificationCompat.BigPictureStyle to setStyle().
To make the image appear as a thumbnail only while the notification is collapsed (as shown in the figure), call setLargeIcon() and pass it the image, but also call BigPictureStyle.bigLargeIcon() and pass it null, so the large icon goes away when the notification is expanded:
2. Add a large block of text:
Apply NotificationCompat.BigTextStyle to display text in the expanded content area of the notification:
Tip: To add formatting in your text (bold, italic, line breaks, and so on), you can add styling with HTML markup.
3. Create an inbox-style notification:
Apply NotificationCompat.InboxStyle to notification if you want to add multiple short summary lines, such as snippets from incoming emails. This allows you to add multiple pieces of content text that are each truncated to one line, instead of one continuous line of text provided by NotificationCompat.BigTextStyle.
To add a new line, call addLine() up to 6 times. If you add more than six lines, only the first six are visible.
Tip: You can distinguish the message’s subject and message in each line by adding styling with HTML markup (such as bolding the subject).
4. Create a notification with media controls:
Apply NotificationCompat.MediaStyle to display media playback controls and track information.
Call addAction() up to five times to display up to five separate icon buttons. And call setLargeIcon() to set the album artwork.
Unlike the other notification styles, MediaStyle allows you to also modify the collapsed-size content view by specifying three action buttons that should also appear in the collapsed view. To do so, provide the action button indices to setShowActionsInCompactView().
If the notification represents an active media session, also attach a MediaSession.Token to the notification using setMediaSession(). Android then identifies this as a notification representing an active media session and respond accordingly (by showing album artwork in the lock screen, for example).
For more information, also read Using MediaStyle notifications with a foreground service. For sample code that uses notifications, see the Android Notifications Sample.
Note: Notifications created with
NotificationCompat.MediaStyle
will have their category set toCATEGORY_TRANSPORT
unless you set a different category usingsetCategory()
.
Display time-sensitive notifications:
Sometimes we need to alert users with some notifications of high priority like an incoming phone call or alarm. We can achieve this by setting the priority as PRIORITY_HIGH while building the notification.
.setPriority(NotificationCompat.PRIORITY_HIGH)
For the highest priority notifications, we can use full-screen intents, which will interrupt the user with a full-screen notification. To achieve that, add this to the builder:
.setFullScreenIntent(fullScreenPendingIntent, true)
Note: The system UI may choose to display a heads-up notification, instead of launching your full-screen intent, while the user is using the device.
If your notification is an ongoing one, such as an incoming phone call, associate the notification with a foreground service. The following code snippet shows how to display a notification that’s associated with a foreground service:
// Provide a unique integer for the "notificationId" of each // notification.
startForeground(notificationId, notification)
Notification Groups:
To avoid bombarding your users with multiple or redundant notifications when you have additional updates, you should consider updating an existing notification rather than issuing a new one, or consider using the inbox-style notification to show conversation updates.
However, if it’s necessary to deliver multiple notifications, you should consider grouping those separate notifications into a group (available on Android 7.0 and higher). A notification group allows you to collapse multiple notifications into just one post in the notification drawer, with a summary. The user can then expand the notification to reveal the details for each individual notification.
The user can progressively expand the notification group and each notification within it for more details.
Note: If the same app sends four or more notifications and does not specify a grouping, the system automatically groups them together.
For the implementation of this check, this article in the documentation. I found it the most simpler way to put it hence not writing it here.
The story does not end here. There is a lot more cool stuff about notifications in the documentation I just provided you the basic tools to build notifications. I will add another article to this series discussing how to create and work with messaging-style notifications.
If you are interested check these articles also:
Feel free to comment if there are doubts or if you feel anything needs to be corrected😀.