Notification channels and badges are part of the many updates in Android O. Starting in Android O, you divide all of your notifications into different notification channels, depending on the type of notification your app is sending. Using channels, you can easily differentiate between different types of communication and provide different default sounds, importance, icons, and more. Once a notification channel exists on the device, users have full control over the settings per notification channel.

Why learn about Notification Channels?

All apps targeting Android O must have a notification channel. Apps that do not properly define and use notification channels will not display any notifications. When you create channels for your notification, this gives your users:

The notification framework for Android O manages this all automatically for you. In addition, notifications are the basis for notification badges, which are small dots on the launcher icon of the app that appear when there are undismissed notifications:

What you'll build

In this codelab, you implement notifications for an app that simulates social media notifications. Your app will:

  • Allow you to explore the user experience and flow of O notifications.
  • Create two different notification types which are therefore part of two different channels.
  • Have notification badges.

What you'll learn

What you'll need

Get the Code

In this step, you download the code for the entire codelab and then run the simple example app.

Click the following button to download all the code for this codelab.

Download the Code

  1. Unzip the code, and then open the project using Android Studio version 2.4 or newer.
  2. Run the code and confirm that it compiles. Note that the buttons currently should not do anything. You'll be adding that functionality!

The sample app

The app is meant to simulate a variety of different social media messages. The four notifications you can simulate are:

The only difference between these notifications is what's in the text:

You need to change the code so that it works with O's NotificationChannels. Until then, the notifications will not work. Let's see how to add notification channels.

Choosing your Notification Channels

You start by creating a 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. In the sample app, there are two types of notifications: notifications about who's following you and direct message notifications. Therefore you make two notification channels.

Creating a Notification Channel

The NotificationHelper.java class contains all of the code for creating and sending notifications. The constructor is where you set up your channels. The code for creating a NotificationChannel for follow and unfollow notifications is already there, it looks like this:

// Create the channel object with the unique ID FOLLOWERS_CHANNEL.
NotificationChannel followersChannel =
       new NotificationChannel(
               FOLLOWERS_CHANNEL,
               getString(R.string.notification_channel_followers),
               NotificationManager.IMPORTANCE_DEFAULT);

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


// Submit the notification channel object to the notification manager.
getNotificationManager().createNotificationChannel(followersChannel);

This code does three things:

  1. Creates the channel object with the unique ID FOLLOWERS_CHANNEL, the name "Follower Notification Channel", and default importance.
  2. Configures the channel's initial settings.
  3. Submits the channel to the NotificationManager.

Your turn

Now you've seen how to make a channel, it's your turn to make a "Direct Messages" notification channel. When creating and configuring the channel, feel free to choose configurations you think make sense or you can follow what we did:

  1. Set the importance to high.
  2. Set the light color to blue.

When you're done, go to your app's settings. You can do this by either pressing the button in the app which says Go to Settings or by navigating to Settings > Apps & notifications > App info > NotificationChannels > App notifications. You should see your new channel:


You can view the location of the code you'll update by looking at these TODO comments.

You can view the solution diff here.

You may have noticed that the notification simulating app doesn't actually send notifications. This is because the code is using the now deprecated notification builder Notification.Builder(getApplicationContext()). For Android O and beyond, all notifications need a notification channel.

The getNotificationFollower and getNotificationDM are two methods that make a notification for followers and direct messages respectively. Modify the method and add the appropriate channel id for each. For example, the getNotificationFollower should look like:

public Notification.Builder getNotificationFollower(String title, String body) {
   return new Notification.Builder(getApplicationContext(), FOLLOWERS_CHANNEL)
           .setContentTitle(title)
           .setContentText(body)
           .setSmallIcon(getSmallIcon())
           .setAutoCancel(true)
           .setContentIntent(getPendingIntent());
}

Notice the usage of the FOLLOWERS_CHANNEL id, which is the same id that was used when making the follower channel. These must match.

Once you're finished, clicking on any of the buttons to send notifications should now send a notification.

You can view the location of the code you'll update by looking at these TODO comments.

View the solution diff here if you get stuck.

The User Experience

Now that you've created these channels, you might be wondering what the actual user experience is. Navigate to notification settings on the device running the app. You can do this two ways: press the Go to Settings button in the app, or if you long press on a notification, you can select the ALL CATEGORIES option to be taken to the app's notification settings screen.

You should see a screen that looks like this:

Below the word categories are each of your notification channels. Select the Follower Notification Channel. Here, you can change settings specifically for the follower notification channel. Try going in and changing the sound that's played for this channel:

If you trigger a follower notification, you should notice the new sound playing. The direct message notification sound has not changed. Now let's say your social media profile is hugely popular and you get tons of followers. Perhaps you don't want to be notified every time a new person follows or un-follows you? In that case, you can turn the entire channel off.

If you re-run your app from Android Studio, you may notice that the settings you picked are NOT changed. As you can see, all user selected settings override the defaults you choose as a developer. This allows users to choose how they want to receive your notifications.

Add a Button to Open Settings

As you've seen, a user's notification settings are controlled by a settings screen in the device's settings. Because of this, it might be helpful to direct your users, from the app, to this setting screen. The code in MainActivity's goToNotificationSettings does just that; it opens the notification settings screen for the app. This method is called when the Go to Settings button is pressed and it looks like this:

Intent i = new Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS);
i.putExtra(Settings.EXTRA_APP_PACKAGE, getPackageName());
startActivity(i);

This code creates an intent with the action Settings.ACTION_APP_NOTIFICATION_SETTINGS; this opens up the notification settings screen for a single app. The package name is included as an intent extra to specify which app the settings screen is for.

Your Turn

Add the code to go to a specific channel settings screen. You should modify the goToNotificationChannelSettings method. It will look similar to the intent for opening an app's notification settings. You need to modify the action of the intent and also include the channel id as an extra. Take a look at the Settings documentation for the appropriate action and channel id extra. Note that the channel id should be passed into goToNotificationChannelSettings. The method is called from onClick in the MainActivity. You should pass either FOLLOWERS_CHANNEL if you press the Follower Notification Settings button, or DIRECT_MESSAGE_CHANNEL if you press the DM Notification Settings button. When you're finished, if you select the DM Notification Settings, it should take you to the settings screen specifically for direct message notifications.

You can view the location of the code you'll update by looking at these TODO comments.

You can look at the solution diff here if you get stuck.

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. The UI for this is shown below:


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:

  1. Your device's launcher supports badging.
  2. You or your user haven't turned badging off for the channel.
  3. You turn on badging by going to Settings > Apps & notifications > Advanced > Special app access > Notification access and turn notification access on for your launcher. This is the default for some launchers:

Your Turn

If you are using a device launcher that supports badging (such as a Pixel phone running Android O), explicitly add badging to your direct message channel and remove app badging from your followers channel using the setShowBadge method. Once you're done, you should see a notification badge on your app's launcher, if there are non dismissed direct message notifications. You should not see a badge if there are no notifications or only follower notifications. Try long pressing your app icon to see and interact with the long press menu:

If they do not, make sure your launcher has notification access as described above.

You can view the location of the code you'll update by looking at these TODO comments.

If you get stuck, check out the solution diff here.