Android O / Notification

Oreo Notifications: Channels – Part 2

In what seems to be an annual traditions for Styling Android, we’er going to look at the changes to Notifications in the latest version of Android which is, at the time of writing, Oreo 8.1 (API 27). While there aren’t widespread changes to Notifications as there have been in previous Android versions, there are some significant one, and we’ll start off by looking at Notification Channels.

In the previous article we discussed what channels are, and why they are important to the user, and in this article we’ll actually implement them. Let’s start with the deprecated Builder constructors which we mentioned in the previous article. For both the Notification and summary Notification we were using NotificationCompat.Builder(Context context), and this has been deprecated in Oreo V8.0 (API 26). It has been replaced by a constructor which takes an additional argument representing the ID of the channel that the Notification should be added to:

The changes that we’ve made seem to be quite a lot, but it’s actually pretty straightforward once we break things down. The first thing that we’re doing is specifying a channelId to the Builder constructors on lines 46 & 66. That gets rid of the deprecation warnings that we saw previously. The channelID strings are defined in the companion object (lines 83-92). It is worthy of note that we create all of the IDs for the supported channels on Oreo, but only create a single channel ID for previous API versions. The reason for this is that I saw that if I used multiple channel IDs on a Nougat device then I would get a notification stating that I had unread notifications, but did not see the notifications themselves. This problem went away if I restricted things to a single channel ID, hence this check.

In the main Notification creation we use a different icon depending on the channel (line 52). I did this so that we had a visual indication of the channel in each notification. This logic is implemented in getIconId() (lines 58-63).

In sendBundledNotifications() we create a random channel ID (line 14), and specify this id for the function calls to create the Notification and summary Notification (lines 15-16).

The final thing in here is the call to ChannelBuilder.ensureChannelsExist() on line 13, and this will require a little explanation. So far we have switched to the correct Builder constructors which accept a channel ID argument, and this will work perfectly well on pre API 26 devices. However, for Oreo 8.0 (API 26) an later we need to actually create the channels corresponding to these IDs. The classes that we require to do this are only available in API 26 and later, so we need to add some OS version checking to ensure that we only attempt this on devices which have those APIs. I have implemented all of the logic to perform the OS version check and create the channels if necessary in NotificationChannelBuilder (which we’ll look at in a moment). NotificationChannelBuilder is completely agnostic about the actual channels and IDs, so is completely reusable. NotificationBuilder itself knows about these, so passes in the list of channel IDs in the NotificationChannelBuilder constructor, and provides a NotificationChannel builder as a function reference to the ensureChannelsExist() function.

createChannel() is this builder function, and it takes a channelId String as an argument and builds the appropriate NotificationChannel. In this example I have used pretty standard defaults based around the levels of Notification importance that are pre-defined, with just a custom description being added to each. However, it is possible to completely customise the default behaviour of the channel using the various properties of https://developer.android.com/reference/android/app/NotificationChannel.html but I’ve kept it fairly simple in order to keep the code more compact an understandable.

One important thing to note is that we have to annotate createChannel() with @TargetApi(Build.VERSION_CODES.O) in order to suppress any OS version warnings (we can omit this if we’re minSdkVersion="26" or higher). The logic inside NotificationChannelBuilder will ensure that this only gets call on API 26 and later devices.

So let’s take a look at NotificationChannelBuilder:

The only public function is ensureChannelsExist() and it is this which initially performs the OS version check and only calls NotificationManager.ensureChannelsExist() if the OS version is API 26 or higher.

The NotificationManager.notificationChannelIds() function returns a list of the channel IDs of channels which already exist for the app, and this is used by NotificationManager.ensureChannelsExist() to filter the list of channelIds that were passed in in to the constructor to obtain a list of those which do not already exist. The forEach block is executed for each of those. In that block the builder function (that we saw defined in NotificationBuilder) will be called to create a NotificationChannel instance for the channel ID, and then we call NotificationManager to actually create the channel.

Only once the channel has been created by NotificationManager can we actually send a notification to that channel – that’s why we need to call ensureChannelsExist() before we attempt to send the notification.

If we run this for a while we’ll get a number of notifications generated. The high priority ones will always be displayed above the normal priority which will always be displayed above the low priority ones. I’ve used different icons so the priority can easily be seen:

This now provides the user with all of the control that we looked at in the previous article.

That’s it for the important notification changes in Oreo. However, we’re not quite done with notifications just yet. In the next article we’ll look at another issue with notifications, but this is more of a legacy one rather than being specific to Oreo.

The source code for this article is available here.

© 2017, Mark Allison. All rights reserved.

CC BY-NC-SA 4.0 Oreo Notifications: Channels – Part 2 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.

1 Comment

Leave a Reply

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