What's the best way to create a multimedia style notification for Android Lollipop, given backward compatibility?

I am looking to create a media style notification for a music application I created, but I want to also retain users of older Android (ICS). I would like it to contain play / pause controls.

What is the best way I can do this? Should I create my own notification for any below Android Lollipop and use the media style notification for Android Lollipop? Or should I just go and make a special notice for both? Using NotificationCompat is not possible after several experiments.

Any examples are welcome.

+6
source share
1 answer

While everyone will work, I would suggest using the first approach. It doesn't look like MediaStyle NotificationCompat support will be added in the near future, based on the note in here that states:

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

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

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

When the time comes to make a notification, I just check it and either use my own notification or create a MediaStyle notification based on the version of Android the user is running on. To support screen lock controls, I use the latest support libraries and interact with MediaSessionCompat, which will still use RemoteControlClient under the hood when required on pre-Lollipop devices. You can transfer the token for your MediaSessionCompat to a MediaStyle notification, which makes it easier to update the lock screen image based on the media being played.

+1
source

Source: https://habr.com/ru/post/976982/


All Articles