Migrating MediaStyle notifications to support Android O

Make MediaStyle notifications work for you on O


If you are using MediaStyle notifications on API level 25 and lower, this article serves as a migration guide to moving these to Android O. MediaStyle notifications are typically used by bound and started services that allow audio playback to occur in the background.

There are a few major differences in Android O that have to be taken into account.

  1. Background services now have to be started with startForegroundService(Intent) and then a persistent notification must be shown within 5 seconds.
  2. Notification channels must be used in order to display notifications.

The migration to O requires a few short steps that are outlined here.

Step 1 — Change your import statements

Make sure to add the following lines to your import statements:

import android.support.v4.app.NotificationCompat;
import android.support.v4.content.ContextCompat;
import android.support.v4.media.app.NotificationCompat.MediaStyle;

You might have an import statement from v7, that you no longer need:

import android.support.v7.app.NotificationCompat;

In your build.gradle, you now only need to import the media-compat support library. The media-compat library is where the MediaStyle class is located. The media-compat library is where the MediaStyle class is located.

implementation ‘com.android.support:support-media-compat:26.+’

MediaStyle is in the android.support.v4.media package because it is now part of the media-compat dependency. They are specifically not in the support-compat library due to separation of concerns within the support library modules.

Step 2 — Use NotificationCompat with channels

In order to use notifications in O, you must use notification channels. The v4 support library now has a new constructor for creating notification builders:

NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(mContext, CHANNEL_ID);

The old constructor is deprecated as of version 26.0.0 of the Support Library and will cause your notifications to fail to appear once you target API 26 (as a channel is a requirement for all notifications when targeting API 26 or higher):

NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(mContext);

To understand channels better in Android O, please read all about it on developer.android.com. Google Play Music has fine grained controls about what you wish to be notified. For example, if you only care about “Playback” related notifications, you can enable those and disable the rest.

The NotificationCompat class does not create a channel for you. You still have to create a channel yourself. Here’s an example for Android O.

Here’s code that creates a MediaStyle notification with NotificationCompat.

Step 3 — Use ContextCompat in order to startForegroundService()

In Android O, services that are meant to run in the background, such as music playback services, are required to be started using Context.startForegroundService() instead of Context.startService(). In order to do this, you can use the ContextCompat class that automatically does this for you if you are on O, and still uses startService(Intent) on N and prior versions of Android.

That’s it! These are 3 simple steps to get you to migrate your pre-Android O MediaStyle notifications that are tied to background services.

For more information about the change to MediaStyle, read the change log for the support lib.

Android Media Resources