Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the best way to create a media style notification for Android Lollipop while considering backwards compatibility?

I'm looking to create a media style notification for a music app i'm creating but i want to keep older Android users (ICS up) in mind also. I would like it to contain play/pause controls.

What's the best way that i can do this? Should i create a custom notification for anyone below Android Lollipop and use a media style notification for Android Lollipop? Or should i just go ahead and make a custom notification for both? Using NotificationCompat doesn't seem possible after a bit of experimentation.

Any examples would be greatly appreciated.

like image 860
Mike Scamell Avatar asked Oct 20 '14 13:10

Mike Scamell


1 Answers

While either will work, I'd suggest using the first approach. It doesn't look like there is an intention of adding MediaStyle support to NotificationCompat any time soon, based on the note listed here, which states:

Note: The template and the addAction() method are not included in the support library, so these features run in Android 5.0 and higher only.

In my case, I have a boolean that identifies the API level:

    boolean mIsLollipopOrAbove = Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP;

When it comes time to make the notification, I just check that and either use a custom notification or build the MediaStyle notification based on the version of Android the user is running. To support lock screen controls, I'm using the latest version of the support libraries and interacting with MediaSessionCompat, which will still use the RemoteControlClient under the hood when required on pre-Lollipop devices. You can pass the token for your MediaSessionCompat to the MediaStyle notification, which facilitates updating the lock screen image based on the media that is playing.

like image 135
Ted Cannelongo Avatar answered Oct 13 '22 19:10

Ted Cannelongo