This week we saw the announcement of the first Android-O developer preview. One of the features of this announcement includes Notification Channels — in this article we’re going to take a look at exactly what these are and how we can make use of them within our applications!
Notification Channels provide us with the ability to group the notifications that our application sends into manageable groups. Once our notifications are in these channels, we no longer have input into their functionality — so it is up to the user to manage these channels. When it comes to altering the settings for our application notifications, the user will be presented with these options:
Starting from the left, you can see in the first screen that the notification settings for our app displays the notification settings for our application. From here the user can:
- Block all notification channels for our app. This means that notifications from our application will never be shown on the users device
- If supported, the user can state whether or not our applications notifications should be shown as badges on the Home app
- The notification categories that exist for our application. From here the user can toggle these to be enabled or disabled
The next screen (in the middle) can be accessed once the user selects a notification category from the first screen. From here the user is able to:
- Block all notifications from our app that come from this channel
- Display notifications from this channel within the Home app, if supported
And as shown in the final screenshot, the user can also set the importance of notifications from this channel. The option selected here will state how they want to be prompted when a notification is received.
We also have the ability to group notification channels into separate groups. This is so that we are able to have the same notification channels across multiple application modes.
For example, my application may support Personal and Business mode, or Child and Parent mode — this allows us to give the option to manage notification settings across multiple groups.
These are displayed in the same place as our notification channels, except just separated into their corresponding group.
On older versions of Android (pre-O) these new features will be completely ignored, so we don’t have to worry about current implementations breaking.
Now we know a little more about what notification channels are, I think it’s time we look at how we can implement them into our application!
It’s quite straightforward for us to create notification channels within our app.We now have access to a method called createNotificationChannel() from the Notification manager. We can use this to create channels for our application notifications. When doing this, it will look something like so:
We can manipulate this NotificationChannel by making use of some of the public methods that it provides to us:
- getId() — Retrieve the ID of the given channel
- enableLights() — If the device in use supports notification lights, then this states whether or not this notification channel should show a light
- setLightColor() — If we decide that our channel supports notification lights, then this allows use to pass an int value that defines the color to be used for the notification light
- enableVibration() — State whether or not notifications from this channel should vibrate when shown on the device
- setVibrationPattern() — Set the vibration pattern to be used (long) if vibration is enabled for this notification channel
- setImportance() — Set the level of interruption for the given notification channel. This can be one of these importance values.
- getImportance() — Retrieve the importance value for the given notification channel
- setSound() — Provide a Uri for a sound to be played when a notification is posted to this channel
- getSound() — Retrieve the sound assigned to this notification
- setGroup() — Set the group in which the notification is assigned to
- getGroup() — Retrieve the group in which the notification has been assigned to
- setBypassDnd() — Set whether or not the notification should bypass Do Not Disturb mode (the INTERRUPTION_FILTER_PRIORITY value)
- canBypassDnd() — Retrieve whether or not the notification can bypass Do Not Disturb mode
- getName() — Retrieve the user visible name for the specified channel
- setLockScreenVisibility() — Set whether or not notifications from this channel should be displayed on the lock screen
- getLockscreenVisibility() — Retrieve whether or not notifications from this channel will appear on the lockscreen
- getAudioAttributes() — Retrieve the audio attributes for the sound which has been assigned to the corresponding notification channel
- canShowBadge() — Retrieve whether or not notifications from this channel are able to appear as badges within a launcher application
Now we have our notification channel created, we are able to reference this channels ID when it comes to creating a Notification to be displayed on the users device.
As mentioned earlier, we are able to group our notifications channels into manageable groups. This allows us to have identically named notification channels defined into different manageable sections, which can be useful if our application supports multiple accounts and / or modes.
We can create a notification channel group by using the createNotificationChannelGroup() method provided by the Notification Manager. When doing so, we need to pass it an instance of a NotificationChannelGroup consisting of:
- Group Id — The unique identifier for this group
- Group Name — The name for the group which will be displayed to the user
Once we’ve created a group, we can use the setGroup() method when creating our Notification Channel instance, passing in the ID of the group which we wish to associate our notification channel with.
As of Android O, creating a Notification instance now requires a channel ID to be set by the use of the setChannel() method. This is to ensure that our notification belongs to a channel that can be managed by the user from their device settings.
We can then send a notification in the same way which we have always been doing, like so:
Note: Realistically you’ll create the notification channels separate from when sending your notifications.
Our applications don’t have the ability to alter the notification channel settings, but we do have the ability to read these settings incase we wish to change our behaviour or prompt the user for something based on these.
We can use the getNotificationChannel() method to retrieve a single notification channel, we just need to pass in the ID of the channel that we are requested when doing so. When we call this method, we will be returned an instance of a NotificationChannel class.
After a notification channel has been created, our user is responsible for modifying any of it’s settings. Whilst we can’t provide the ability to modify these settings from within our app, we can direct the user to the notification settings page within the device settings via the user of the Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS intent action.
To do this we just need to provide two extras when creating the intent:
- EXTRA_CHANNEL_ID — The ID of the channel settings which you wish to navigate the user to
- EXTRA_APP_PACKAGE — The package for your application
If we no longer wish to use a notification channel any longer, then we can use the deleteNotificationChannel() method provided by the API. When calling this method, you simply pass the id for the channel which you wish to delete and it will be removed from available notification channels. This can be done like so:
It’s important to note that notification channels will still be visible within the notification settings as a way to avoid spam.
That’s all pretty neat isn’t it! Hopefully from this you can see just how easy it is to create notification channels, notification channel groups as well as managing them from there. I’m looking forward to both adding these to any applications I work on, as well as using them in applications so that I can take a more fine grained approach to managing notifications for those applications.
Any questions or tips building on what we’ve looked at, I’d love to hear from you!
Joe Birch (@hitherejoe) | Twitter
The latest Tweets from Joe Birch (@hitherejoe). Android Engineer @Buffer. You'll also find me speaking / writing …
P.s Check out my other work at joebirch.co