Android Bundled Notifications With Examples

In this tutorial, we’ll be implementing Bundled Notifications in our Android Application. We’ve seen and implemented Direct Reply for Android Nougat. If you aren’t aware about Notifications and Pending Intents do go through the tutorial before proceeding.

Android Bundled Notifications

Android Bundled Notifications are handy when you have too many notifications stacked vertically. Bundled Notifications collapse them all into a single notification with a group summary set on the group notification. This saves us from scrolling through all the notifications. Also, if you receive many single notifications at once, instead of getting multiple noisy notification sounds, bundled notifications would give a single notification sound.

A Bundle Notification is of the format:

  • 1 Summary Notification
  • N enclosed Single Notifications

We can set a separate tone for every single notification. Every notification of a bundle would have the same group key.

When a new notification is added, it gets merged in the bundle with the matching group key.

We can add a separate action on each notification click as well as a separate action on bundle notification click.
The Bundle Notification can exist without any enclosed notifications.

Creating a Bundle Notification

A Bundle Notification is defined in the following way.

A Bundle Notification must have the setGroupSummary() set to true.

The group key set in the setGroup() method must be set in all the notifications that are to be enclosed in the bundle.

Note: A bundle_channel_id needs to defined since Android Oreo. We’ve discussed Notification Channels at length in a separate tutorial.

We’ve set the importance of the bundle notification channel to LOW to avoid simultaneous sounds from the Bundle and single notifications. We’ll create a separate channel for single notifications.

In the sample application, we’ll be implementing the Bundle Notifications feature. We’ll be using a Button to create a new Bundle and another button to add new Single Notifications in the current Bundle.

On a notification click, we’ll cancel the notification and display it’s ID in a Toast.

We’ll not be focusing on push notifications from the backend in this tutorial. Our goal is to understand the Bundle Notifications feature only.

Android Bundled Notifications Project Structure

android-bundle-notification-project-structure

A Single Activity application.

Android Bundled Notifications Code

The code for the activity_main.xml layout class is given below.

The code for the MainActivity.java class is given below.

  1. btnSingleNotification adds a Single Notification in the current bundle notification. If the current bundle notification doesn’t exist it creates one first.
  2. btnBundleNotification creates a new Bundle Notification and updates the group key by incrementing the id.
  3. We set a bundleNotificationId equal to the singleNotificationId initially.
  4. Every time a new bundle notification is created, we increment the bundleNotificationId by 100.
  5. Every time a single notification is created inside the bundle notification, we increment the singleNotificationId by 1 on the current bundleNotificationId.
  6. We’ve created separate channels for Bundle and Single Notifications.
  7. onNewIntent is triggered on Notification click. It gets the notification id from the intent data and cancels the respective notification.

Android Bundled Notifications App Output

Let’s see the output from the above code.
android bundled notifications app output

Do note that in the above output, clicking any notification cancels only the last notification added.

Why?

Because PendingIntent updates the data to the latest and we’re using request code as 0 for each of the PendingIntents. So it returns the latest data only.

We need to set a different request code for each notification.

Let’s set the request code as bundleNotificationId for bundle notifications and singleNotificationId for the single ones.

Let’s see the output now.
android bundled notifications example

Works!. This brings an end to android bundled notifications tutorial. You can download the Android BundledNotificationsExample Project from the link below.

By admin

Leave a Reply

%d bloggers like this: