Creating Notification Channels on Android Oreo | With Example

Spread knowledge

Starting from Oreo (Android Version 8.0)  Android introduced the concept of Notification Channels which  groups notifications into channels and allows developers to implement common behavior for the complete channel. This behavior will be followed by all the notifications which are a part of that channel. The main purpose of this was to give more control of the notification behavior to the user. Once our notifications are a part of a channel users can override their behavior set by you . Following are some features of Notification Channels

  • On and above Android 8.0 (Oreo) it is mandatory for all the notifications to be a part of any one Notification Channel.
  • A single application can have multiple notification channels.
  • Every Notification Channel has a unique identifier,  user visible name and description.
  • Once a Notification Channel is created, you cannot do any changes to its behavior. Even deleting and recreating a channel with the same ID wont help. Only user can  override the behavior set by you
  • The behavior you set on a Notification Channel overrides the behavior set on individual notifications.
  • Notification Channels are not a part of support library. Therefore they should be created and will work only on Android 8.0 and above

notification_

Creating Notification Channels

Before beginning lets first discuss what are all the mandatory parameters required to create a Notification Channel. They are

  1. Channel ID
    This is unique string identifier which is used to keep reference of the created Notification Channel. This is what will be used to add a Notification to a particular Channel
  2. Channel Name
    This is a user visible name of your channel. Recommended max length is 40 characters
  3. Importance
    This is similar to setting priority to individual Notifications, except that this will be applicable for all the notifications in this Channel. This will decide how interruptive a notification posted from this channel will be. User will have to select any of the predefined constants
    IMPORTANCE_HIGH : Makes noise and peeks . May use full screen intents
    IMPORTANCE_DEFAULT : Shows everywhere makes noise but does not visually intrude
    IMPORTANCE_LOW : Shows everywhere but is not intrusive
    IMPORTANCE_MIN  : Shows only in shade below the fold

notification_importance

Using the above three parameters Notification Channel is defined as shown below

// This is the Notification Channel ID. More about this in the next section
public static final String NOTIFICATION_CHANNEL_ID = "channel_id";

//User visible Channel Name
public static final String CHANNEL_NAME = "Notification Channel";

// Importance applicable to all the notifications in this Channel
int importance = NotificationManager.IMPORTANCE_DEFAULT;

//Notification channel should only be created for devices running Android 26
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

    NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, CHANNEL_NAME, importance);

    //Boolean value to set if lights are enabled for Notifications from this Channel
    notificationChannel.enableLights(true);

    //Boolean value to set if vibration is enabled for Notifications from this Channel
    notificationChannel.enableVibration(true);

    //Sets the color of Notification Light
    notificationChannel.setLightColor(Color.GREEN);

    //Set the vibration pattern for notifications. Pattern is in milliseconds with the format {delay,play,sleep,play,sleep...}
    notificationChannel.setVibrationPattern(new long[] {
        500,
        500,
        500,
        500,
        500
    });

    //Sets whether notifications from these Channel should be visible on Lockscreen or not
    notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
}

 

  • Once the Notification Channel object is defined we can create the Channel as shown below
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(notificationChannel);
  • We can now create a notification and add it to the channel create above. If you want to read more about creating notification read this
//We pass the unique channel id as the second parameter in the constructor
NotificationCompat.Builder notificationCompatBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);

//Title for your notification
notificationCompatBuilder.setContentTitle("This is title");

//Subtext for your notification
notificationCompatBuilder.setContentText("This is subtext");

//Small Icon for your notificatiom
notificationCompatBuilder.setSmallIcon(R.id.icon);

//Large Icon for your notification
notificationCompatBuilder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.id.icon));
  • As you can see we pass the the unique string  channel id as the second paramater in the constructor while creating Notification. This will add the notification to that particular Notification Channel
  • We use NotificationCompatBuilder for backward compatibility. Also Notification Channel work only for Android 8.0 i.e. Channel ID passed in the constructor is ignored for versions below Oreo(8.0)
  • Once you have created the notification you can issue with the following
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID, notificationCompatBuilder.build());

Reading Notification Channel Setting

As previously mentioned user has completely control over notification behavior when using Notification Channel. This basically means user can override the behavior set by you anytime. You cannot reset the behavior set by the user but you can read the current behavior and remind him to change it. In the below code snippet we fetch various behavior details of the Notification Channel

NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

//You just need the Channel ID to get reference of the Notification Channel
NotificationChannel notificationChannel = notificationManager.getNotificationChannel(NOTIFICATION_CHANNEL_ID);

//Fetching the user visible name of the Channel
CharSequence name = notificationChannel.getName();

//Fetching the lockscreen visibilty of the notifications in this Channel
int lockscreenVisibility = notificationChannel.getLockscreenVisibility();

//Fetching the light color set for this Channel
int color = notificationChannel.getLightColor();

// Fetching vibration pattern for this channel
long[] pattern = notificationChannel.getVibrationPattern();

 

Redirecting user to Notification Channel Setting

Since you don’t have complete control of the notification behavior you might need to redirect user to the notification channel page inside device’s setting to change some of the notification’s behavior.

notification_channels
Notification Setting page of YouTube
Intent intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS);
//Unique Channel ID of the the particular Notification Channel
intent.putExtra(Settings.EXTRA_CHANNEL_ID, NOTIFICATION_CHANNEL_ID);
//Package Name
intent.putExtra(Settings.EXTRA_APP_PACKAGE, getPackageName());

startActivity(intent);

Deleting Notification Channel

Deleting the notification is much simpler than creating one. You just need have the unique Channel ID for that particular Notification Channel

NotificationManager notificationManager = getSystemService(NOTIFICATION_SERVICE);
notificationManager.deleteNotificationChannel(NOTIFICATION_CHANNEL_ID);

 


Spread knowledge

1 Reply to “Creating Notification Channels on Android Oreo | With Example”

Leave a Reply

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