Android Notification Channel

In this tutorial, we’ll be looking into how the introduction of Android Oreo has brought a drastic change in the Notifications. You aren’t far away from getting an Android Oreo update. Before you do get, let’s look into the working of notifications and what you need to change and adapt as Android Developer.

Android Notification Channel

We have discussed and implemented Notification, here and here.

With the introduction of Android Oreo, Google has strived the Notifications system more user-friendly. Android Oreo has completely redesigned notifications. The power to receive the kinds of notifications has been given in the hands of the end users. The reason this all became possible is: Notification Channels.

Notification Channels allow us to separate notifications into different groups/categories. Every channel would have a common functionality. It allows the user to customize their notification settings.

Thanks to this feature the user can do the following things from the Apps Settings:

  • Block notifications from a particular channel.
  • Set priority/silent on different notification channels.

Without configuring Notification Channels, you cannot build notification for applications with Android API >=26. Notification Channels would be ignored for older applications with the Android API Creating Notification Channel

Following code creates a notification channel:

The NotificationChannel constructor requires us to specify the channel_id and channel_name strings. The Importance argument is an int which specifies the level of interruption by the notification. It can be one of the following values:

  • IMPORTANCE_DEFAULT – Shows up in the system tray. Makes sound. Doesn’t visually pop up.
  • IMPORTANCE_HIGH – Visually pops up too.
  • IMPORTANCE_LOW – Shows in the tray. No pop up. No sound.
  • IMPORTANCE_NONE – Doesn’t show up. Kind of blocked notifications.

Besides the public methods specified above, following are some handy methods that come with NotificationChannels.

    • setGroup()/getGroup() – Setters and getters for the channel. We’ll look at this later.

?

  • setBypassDnd()?- Set the INTERRUPTION_PRIORITY_VALUE to bypass do not disturb.
  • canBypassDnd() – Check whether the notification channel can display notification in DND mode.
  • setLockScreenVisibility() – Set whether notifications from this channel should be displayed on the lock screen or not.
  • canShowBadge() – Can show the badge/notification dot on the application icon.
  • getName()/getId() – Retrieve the channel name and id respectively.

Once the Notification Channel is created using createNotificationChannel(), every notification created from it will have common properties unless modified.

Note: The above code snippet is valid for Android version Oreo and above only. Hence it must be enclosed in the following condition.

Creating a Notification

The following code snippet creates a Notification from NotificationChannel.

From Android Oreo, it is mandatory to specify the NotificationChannel id in the Builder constructor itself.

Reading And Deleting Notification Channels

To retrieve a notification channel we can call the method getNotificationChannel() on the NotificationManager.
We need to pass the channel_id of the relevant channel.
Also, to retrieve a list of all NotificationChannels we can invoke the method getNotificationChannels().

Deleting a NotificationChannel
To delete a NotificationChannel, the following snippet is used.

Notification Channel Groups

A NotificationChannelGroup is used to created different categories for NotificationChannels.
The same NotificationChannels can be used in different circumstances depending on the group from which they are invoked.
You can have two groups named Hourly, Daily. All the Notification Channels would be present in both the groups. It’s your choice from which group you want to use the channel to create Notifications.

Creating NotificationChannelGroup

Following is one way to create NotificationChannelGroups.

You need to set the group_id and group_name.
Furthermore, you need to set the group on the NotificationChannel too using setGroup(). Pass in the group_id in the setGroup() method.

Modifying Notification from Settings

The end user can modify the Notification Channel from the Settings.
Settings | Apps | App Name | App Notifications

android-notification-channels-settings

Alternatively we can redirect them from the app itself using Intents.

We pass the channel id and app package name. This specifically opens the particular channel ID.

android-notification-channels-settings

Let’s create a basic application with the use cases of NotificationChannel and NotificationChannelGroup

Project Structure

In this application, we’ll be using RadioButton to change between the groups and Spinners to choose the current NotificationChannel. The EditText would be used to set the body of the notification.

android-bundle-notification-project-structure

Android NotificationChannel Code

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

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

In the above code, we begin by creating the NotificationChannelGroup and NotificationChannel for the RadioButtons and Spinners respectively.

The channel_id is appended with the group_id in order to know which group does the channel belong to.

On Button click we create the notification based on which radio button and spinners were selected and retrieve the channel_id and group_id accordingly.

For this example, by default, we’ve blocked notifications from all the channels belonging to second group.
(Enable that channel from the settings to view those notifications).

On a notification click, we show up a dialog that allows the user to goto the settings to change the current NotificationChannel settings.

On Notification click launches the same activity. Hence in the Manifest file we need to set android:launchMode="singleTop" in the activity tag.

Output

The output of the above application in action is given below.

android notification channel example

Long Press on a Notification, it allows us to change the current channel settings with the slider.

The All categories option that’s visible lets us view all types of channels and groups.

Do take note that we’d toggled a few channels from the first and second group. It lead to blocked notifications in the first group and default notifications in the second.

Android Notification Dots

Notification Dots/Badges are displayed on the app icon if there are unread notifications.

We can use the method setShowBage(boolean) on the notification to display/hide the dot for the particular channel.

Thanks to Notification Dots, long pressing the app icon can now display/cancel the pending notifications too as shown below.

android notification dots example

Do note that the count for the pending notifications is displayed in the Notification Dot menu too.

This brings an end to this tutorial. You can download the final Android NotificationChannels Project from the link below.

By admin

Leave a Reply

%d bloggers like this: