Notification Channels in Android O

There is no way a user can turn off a specific type of Notifications unless a way is explicitly provided in the app. The only way around is to turn off all the app Notifications. With Android O, Developers can create different channels for different types of notifications. Users can modify following characteristics from settings:

  • Importance
  • Notification Sound (Tones)
  • Blink Light
  • Vibration
  • Lock Screen
  • Do Not Disturb

 

 

device-2017-12-24-224115
Channel Settings

 

 

Creating Notification Channel

A Notification Channel can be created by instantiating a NotificationChannel reference. A Notification channel takes channel id, channel name, and importance.

The channel id is different and has to be unique for each channel.

NotificationChannel channel_1 = new NotificationChannel("sports","Sports", NotificationManager.IMPORTANCE_DEFAULT);
channel_1.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.createNotificationChannel(channel_1);

Here ‘sports’ is the channel id and ‘Sports’ is the channel name.

Creating Notification

We can add notification to a channel by making a slight change in our old way of creating a notification.

Notification.Builder builder1 = new Notification.Builder(getApplicationContext(), "sports")
        .setContentTitle("Sports")
        .setContentText("India V/s New Zealand match postponed due to rain")
        .setSmallIcon(R.drawable.ic_launcher_background)
        .setChannelId("sports") //Specifying channel id of notification
        .setAutoCancel(true);

Just by setting the channel id in the notification, It will be shown in its corresponding channel.

 

 

device-2017-12-24-224029
Notifications from different channels

You can download the sample code for this blog from here.

 

Leave a comment