Android O continues the process of refining the Android experience. For notifications, this means a number of improvements to how notifications are sorted, displayed, and animated. Developers now have a new set of APIs they must leverage to target Android O (API 26), and some new options for engaging users. Users themselves now have more fine-grained control over what notifications an app can display and how exactly it will display them. In addition to this, Google introduced some new APIs for pinning shortcuts that improve on existing implementations and make it easier for users to enjoy your app. Keep reading to learn more about how notifications and shortcuts have improved and the quick steps to keep your app working when targeting Android O.

Android notification sections

Notification Improvements

In Android O, several changes have been made to better organize and prioritize how notifications are displayed. Notifications are now divided into four sections depending on their priority and style.

Major ongoing

  • Reserved for high priority ongoing tasks
    • Phone calls, playing music, active navigation, etc.
    • Notifications using the MediaStyle are automatically colorized based on artwork
    • Developers can opt into this manually using the notification Builder's setColorized(boolean) method. This should not be abused! Colors are only intended for important foreground service notifications.

People to people

General

  • Standard notifications appear in this section the same as they do now

By the way

  • Low priority notifications using IMPORTANCE_MIN will appear here
    • Commonly used for ongoing notifications like weather or traffic - anything that doesn't require immediate attention
  • Collapses to a single line and don't appear on the lock screen

In addition to these sections, some new animations are used when notifications are pulled down and scrolled through. Improvements have also been made to the visual stability of notifications so they won't reorder while a user is viewing them.

Implementing Notifications

Before you target Android O, you will need to make a quick change to how you build your notifications. All it takes is a couple lines of code to ensure your notifications still work. If you target API 26+ and don't do this, your notifications won't display.

Channels

To give users more control over what notifications they receive and how they're displayed, Android O introduces the idea of notification channels. When you target Android O, you must define at least one channel for your notifications. Channels provide a way for developers to differentiate between the types of notifications their app displays.

Things like importance, sound, notification light, vibration, lock screen behavior, overriding do not disturb, and badging (more on badges below) are now controlled at the channel level. Users can modify any of these settings from a new Android system UI, as well as toggle a channel completely on or off. For this reason, you should think carefully about the best way to divide your notifications into their respective channels.

Creating a channel

A channel is defined by a channel ID that is unique within your app plus a name and description that will be visible to the user. When you create your channel, you also declare your desired settings for how that channel will display notifications.

NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

String id = "channel_1";
CharSequence name = getString(R.string.channel_1_name);
String description = getString(R.string.channel_1_description);
int importance = NotificationManager.IMPORTANCE_DEFAULT;

NotificationChannel channel1 = new NotificationChannel(id, name, importance);
channel1.setDescription(description);
// desired channel settings - these can be changed by the user
channel1.enableLights(true);
channel1.setLightColor(Color.BLUE);
channel1.enableVibration(true);
channel1.setShowBadge(true);

notificationManager.createNotificationChannel(channel1);

To further differentiate your notification, you can optionally define groups for your channels. One example of when you may consider doing this is if your app supports multiple user accounts that display similar notifications with the same name. To implement this, just create your notification channel group and set that group on your individual channels.

// create group
String group1Id = "group_1";
CharSequence group1Name = getString(R.string.group_1_name);
notificationManager.createNotificationChannelGroup(new NotificationChannelGroup(group1Id, group1Name));

// create channel
...
channel1.setGroup(group1Id);
notificationManager.createNotificationChannel(channel1);

You can create your groups and channels at any time before you build your notifications. The code is inexpensive and no matter how many times you call createNotificationChannel() or createNotificationChannelGroup(), Android will not create duplicates. However, to be as efficient as possible it would make sense to do this up front.

Tip: The Android device will log an error when an app targeting Android O attempts to display a notification without using a channel. A new setting in developer options will display a Toast whenever this occurs. To enable, go to Settings > Developer options and enable Show notification channel warnings.

Creating a notification

Now that you've created your channel and optionally your channel group, you're ready to build a notification. The only modification needed here is to provide the appropriate channel ID to the notification builder.

Notification notification = new Notification.Builder(mContext, CHANNEL_1_ID)
...
.build();

That's it! You are now ready to target Android O and continue engaging your users with great notifications.

Badges

Along with channels, Android O also introduces notification badges as a channel setting. When enabled, badges display as a dot on app icons in the launcher. When a dot is shown, users can long-press the app icon to access a hovering menu containing the notification. Users can dismiss or act on the notification from this menu just as they can in the notification drawer. Like the other channel settings, badges can be manually disabled by the user. One thing to note is that the user's default launcher must support these features or the badge and/or menu will not display.

Android launcher badges and menu

If you aren't targeting Android O, your app will automatically display a badge for every notification. Once you target O, you can customize this behavior. Something to consider is that badges may not be appropriate for all notifications. Badging only makes sense for important notifications that users would want to act on immediately, such as a chat message. Ongoing notifications like those for background processing or media playback controls should not display badges.

Customizing badges

As was mentioned above, once you're targeting API 26 or greater you can customize what channels display a badge and some additional details about that badge. To opt in or out of badging for a channel, simply call setShowBadge(boolean) when you are building your channel.

NotificationChannel channel1 = new NotificationChannel(id, name, importance);
… 
channel1.setShowBadge(true);

If a badge represents multiple notifications within a channel, Android will automatically display and increment a counter in the top-right of the hovering long-press menu. You can customize this value by calling setNumber(int) on the Notification Builder.

You can also modify which icon (large or small) is displayed in the hovering menu by calling setBadgeIconType(int) and passing either BADGE_ICON_NONE, BADGE_ICON_SMALL, or BADGE_ICON_LARGE (default).

If the notification represented by your badge duplicates an existing app shortcut, you can temporarily remove the shortcut by providing its ID to setShortcutId(int). Speaking of shortcuts, keep reading below to learn about the new API for app shortcut pinning introduced with Android O.

Pinning Shortcuts

Android Nougat (API 25) introduced the app shortcut APIs last year. App shortcuts are deep-links into apps accessed by long-pressing the app icon in the launcher to display the hovering list of shortcuts. These can be declared statically or dynamically depending on your app's needs.

In API 25 these shortcuts could be pinned to the launcher as unique icons by broadcasting the INSTALL_SHORTCUT action along with the title, icon, and Intent for the app icon. With Android O, a new API has been created for this problem and the INSTALL_SHORTCUT solution will no longer work.

Android app shortcuts and pinning

Implement Shortcut Pinning

In Android O, you can pin a shortcut to a supported launcher in three easy steps. First, use ShortcutManager to ensure that the user's default launcher supports shortcut pinning. Next, initialize a ShortcutInfo object with the details of your shortcut. Finally, use ShortcutManager to request that the shortcut be pinned to the launcher.

ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);

// ensure the default launcher supports shortcut pinning
if (shortcutManager.isRequestPinShortcutSupported()) {
 // create ShortcutInfo with details of shortcut
 // if 'uniqueShortcutId' already existed, we could just access it using the Id
 ShortcutInfo shortcutInfo = ShortcutInfo.Builder(mContext, "uniqueShortcutId")
 .setIcon(Icon.createWithResource(mContext, R.mipmap.ic_launcher))
 .setShortLabel(getString(R.string.shortcut_short_label))
 .setLongLabel(getString(R.string.shortcut_long_label))
 .setIntent(...your Intent...)
 .build();

 shortcutManager.requestPinShortcut(shortcutInfo, null);
}

The example above does just enough to pin a shortcut to the launcher. There are several other options and features you can utilize when using this API. If you want to be notified when the shortcut is pinned successfully, you can create a PendingIntent and pass it as the second parameter in requestPinShortcut(ShortcutInfo, PendingIntent). If you'd like to let your user customize their pinned shortcut, you can create a custom Activity and register it in your AndroidManifest with an IntentFilter for the ACTION_CREATE_SHORTCUT action. There are also ways to update and disable existing pinned shortcuts. In addition to pinning shortcuts, you can use new similar APIs to pin widgets from your app to the launcher - a long requested feature. Check out the Android documentation for a deep dive into all of these features.

Conclusion

As you can see above, it's quick and easy to get your applications ready for the notification improvements introduced with Android O. These new APIs benefit everyone. They give the end-user more control of their Android experience while introducing new ways for developers to increase engagement with their apps.

Portions of this page are reproduced from work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.