Image from www.pexels.com

Migrate your app to Android Oreo — Part 3 Notifications

Javier Arroyo
ITNEXT
Published in
3 min readAug 17, 2018

--

Android Oreo (8.0) came with many cool new features like Picture in Picture, Smart text selection and much more. But one of the most important changes came into the Notifications field.

  1. Services — Background limitations
  2. BroadcastReceivers
  3. Notifications
  4. RunTime Permissions
  5. Alarms
  6. Firebase Cloud Messaging (deadline April 2019)

Now with Android Oreo, developers can separate the behaviour of notifications by creating different channels for each of them. It is important to say that no channel is specified for a notification, this notification will not appear in the status bar.

All notifications that use the same channel will have the same behaviuour, so it is important to create different channels according to the importance of notifications.

For example we could create a channel for our app’s most urgent notifications, where each notification is announced with vibration, an alert sound and a notification light. If our app has another kind of notification with less importance, we can create a channel quieter that only show the notification in the status bar without sound and light.

Here you can see the notification channels of Gmail:

Gmail notification channels

How to show a Notification in Android Oreo?

  • Before Android Oreo when we wanted to create a notification we had to do something like this:
Display notifications before Android Oreo

How you can see before Android Oreo we did not specify nothing about channels. So all notifications had the same behaviour.

  • Now with Android Oreo, to display one notification we will have to do something like this:
Display notifications with Android Oreo

So if you analyze the code in detail the difference is that we have to create a channel for Android devices with versions ≥ Android Oreo.

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { 
val channel = NotificationChannel(channelId,
BuildConfig.NOTIFICATION_CHANNEL_ALARMS_NAME,
NotificationManager.IMPORTANCE_DEFAULT)
notificationManager.createNotificationChannel(channel)
}

Another change introduced in Android Oreo was the importance of the notification, in the next table you can check the different importances for your notifications:

Notification impotance

If you’d like to further customize your channel’s default notification behaviors, you can call methods such asenableLights(), setLightColor(), and setVibrationPattern() on the NotificationChannel.

👮 IMPORTANT 👮

But remember that once you create the channel, you cannot change these settings and the user has final control of whether these behaviors are active.

How to DELETE a Notification Channel?

In the future, you may have created a notification channel, but from now on we do not want to use it anymore. The following sample code demonstrates how to delete a notification channel:

SERVICES HAVE TO SHOW NOTIFICATION

In previous post we explained the limitations of using services in Android Oreo. How we said, if we want to use a service in the background we have to warn the user through a notification in the status bar.

So if we do not want that this notification to sound or vibrate, we recommend creating a “silent” channel and with an importance:

NotificationManager.IMPORTANCE_LOW

This is the minimun importance that we can set to a notification when we start a service, if you use “Importance_min” a exception will be thrown when you start it.

If you want more info, you can check the official documentation here.

I hope this post helps you. Happy coding!

👏😃

--

--