Notification Channels

Android O brought the idea of notification channels. This is a quick post about how to setup the notification channel and what it gives the user.

Tl; DR; https://github.com/aasierra/acg

To get started you of course will need to be on Android 2.4 as well as have a device running Android O. I will be using a Pixel device. The instructions can be found here

Once that is setup, go ahead and startup a normal project like you would and select an empty activity project.

From there you will need to modify your build.gradle file to target Android O. You can do that following the instructions here After that you will finally have the ability to use the NotificationChannel class. So to get started let us edit our Activity.

  	
  		package com.anthonysierra.notificationchannels;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    private static final String notificationChannelId = "com_anthonysierra_NotificationChannels_01";
    NotificationManager notificationManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        createNotificationChannel();
    }

    /**
     * Sends a notification to the user via the the channel.
     *
     * @param view
     */
    public void sendNotification(final View view) {
        final Notification notification = new Notification.Builder(this)
                .setContentTitle("Fake notification")
                .setContentText("Some useful text")
                .setSmallIcon(android.R.drawable.alert_light_frame)
                .setChannel(notificationChannelId)
                .build();
        notificationManager.notify(1, notification);
    }

    /**
     * Creates the notification channel that will show up in the settings page for this app.
     */
    private void createNotificationChannel() {
        notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        final NotificationChannel channel = new NotificationChannel(notificationChannelId, "Hello", NotificationManager.IMPORTANCE_LOW);
        notificationManager.createNotificationChannel(channel);
    }
}

Above is the activity code. As you can see we have a notification channel id defined. This is how we allow the notification we send to differentiate channels.

The code contains two methods, one for settings up a notification channel and one for actually sending a notification. The important part of createNotificationChannel to take not of is The two channel lines below.

	
		final NotificationChannel channel = new NotificationChannel(notificationChannelId, "Hello", NotificationManager.IMPORTANCE_LOW);
        notificationManager.createNotificationChannel(channel);
	

These two lines will as it looks create a notification channel and register it with the notification manager. This is what allows the user to have fine grain control over notifications. As you can see in the image below, the notification channel “Hello” has the option to be ignored.

Now that we have our channel setup, we can send a notification to it. The below code does just that.

	
        final Notification notification = new Notification.Builder(this)
                .setContentTitle("Fake notification")
                .setContentText("Some useful text")
                .setSmallIcon(android.R.drawable.alert_light_frame)
                .setChannel(notificationChannelId)
                .build();
        notificationManager.notify(1, notification);
	

The important part of the code to notice though is the setChannel call.

That is what will make the notification adhere to the rules defined by our notification channel.

Just for completeness and as you can see in the github repository linked in the TL;DR. The sendNotification code is called via an onclick setup in the xml from a button.

Hope you enjoyed, this and can start to utilize notification channels in the future.