Notifications on Android O

Published Jan 31, 2018Last updated Feb 02, 2018
Notifications on Android O

Starting Android-O, users will have more control over notifications. As being a developer, the process of creating notifications is different from earlier.
In this post we will learn how to create a simple notification for our apps running on Android-O.

1. Creating Notification Channel

A notification channel represents a type of notification your app posts, so that users can pick settings specifically for that type of notification. When creating channels, you typically first identify each distinct type of notification your app sends.

NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Create the channel object with the unique ID.
NotificationChannel myChannel =
       new NotificationChannel(
               YOUR_CHANNEL_ID,
               "Your Channel Name",
               NotificationManager.IMPORTANCE_DEFAULT);

// Configure the channel's initial settings.
myChannel.setLightColor(Color.GREEN);
myChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 200, 500});

// Submit the notification channel object to the notification manager.
mNotificationManager.createNotificationChannel(myChannel);

First we created a notification channel object with channel Id, Channel name and its importance. Then we set the initial channel settings and finally submit the channel to notification manager.

Users can change these setting by going to notification settings of your app and modify these settings as per their convenience.

2. Sending Notification

The "Notification.Builder(getApplicationContext())" is deprecated now. For Android O and beyond, all notifications need a notification channel. You need to add the appropriate channel id for each notification.

Notification.Builder notification =
                    new Notification.Builder(this,String.valueOf(YOUR_CHANNEL_ID))
                    .setContentTitle("Notification Title")
           			.setContentText("Notification Body")
           			.setSmallIcon(R.mipmap.ic_launcher) 
           			.setAutoCancel(true);
mNotificationManager.notify(id, notification.build());      

3. Notification Badges

The new changes in Android O for notifications include support for notification badging, also know as notification dots. Notification badges visually show that there are undismissed notifications by appearing on app launcher icons. When you long click on a launcher icon that has a notification badge, a long click menu is shown that allows you to swipe through undismissed notifications.

You can explicitly set whether or not to show notification badges on a per notification channel basis by using NotificationChannel's setShowBadge method. Badges are shown by default when the following is true:

a. Your device's launcher supports badging.
b. You or your user haven't turned badging off for the channel.
c. You turn on badging by going to Settings and turn notification access on for your launcher. This is the default for some launchers.

So that was a short overview on notifications for Android-O.
Happy Coding!!!

Discover and read more posts from Lovish Jain
get started