7

My custom mediastyle notification no longer works in Android 11 for my music app. It works fine in Android 10 and previous versions.

Is there any other code I need to add so that it works in Android 11.

I should add that getting rid of the " .setMediaSession(mediaSessionCompat.getSessionToken())) " line gives me a notification, but its not an Oreo notification with the full background color, etc.

Here is my code for creating notifications:

public static final String CHANNEL_ID = "Channel1";

//public static final String ACTION_PREVIOUS = "actionprevious";
public static final String ACTION_PLAY = "actionplay";
public static final String ACTION_EXIT = "actionexit";
//public static final String ACTION_NEXT = "actionnext";

public static Notification notification;

public static void createNotification(Context context, Track track, int playbutton, int exitApp, int pos, int size) {

    if (Build.VERSION.SDK_INT >=Build.VERSION_CODES.O) {

        NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(context); 
        MediaSessionCompat mediaSessionCompat = new MediaSessionCompat(context, "tag"); /

        mediaSessionCompat.setActive(true);

        Bitmap icon = BitmapFactory.decodeResource(context.getResources(),R.drawable.half); 

        Intent intentPlay = new Intent(context, NotificationActionService.class)
                .setAction(ACTION_PLAY);
        PendingIntent pendingIntentPlay = PendingIntent.getBroadcast(context, 0,
                intentPlay, PendingIntent.FLAG_UPDATE_CURRENT);

        Intent intentExit = new Intent(context, NotificationActionService.class)
                .setAction(ACTION_EXIT);
        PendingIntent pendingIntentExit = PendingIntent.getBroadcast(context, 0,
                intentExit, PendingIntent.FLAG_UPDATE_CURRENT);

        exitApp = R.drawable.ic_close_black;

    
        Intent intentOpenApp = new Intent(context, MainActivity.class);
        PendingIntent pendingIntentOpenApp = PendingIntent.getActivity(context,0,
                intentOpenApp, 0);

      

         Notification.MediaStyle style = new Notification.MediaStyle();
        androidx.core.app.NotificationCompat.Builder builder = new androidx.core.app.NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
                //NotificationCompat.Builder builder = new NotificationCompat.Builder( this, NOTIFICATION_CHANNEL_ID );
                .setSmallIcon(R.drawable.ic_audiotrack)
                .setVisibility(androidx.core.app.NotificationCompat.VISIBILITY_PUBLIC) 
                .setLargeIcon(icon)
                .setContentTitle( "TEST" )
                .setContentText(notificationText)
                .setContentIntent(pendingIntentOpenApp) 
                .setShowWhen(false) 
                .setOngoing(true)           .setBadgeIconType(androidx.core.app.NotificationCompat.BADGE_ICON_NONE) 
                .setOnlyAlertOnce(true)
                .addAction(action)
                .addAction(generateAction(R.drawable.ic_close_black, "Exit", ACTION_EXIT))
                .setStyle(new NotificationCompat.MediaStyle()
                        .setShowActionsInCompactView(0,1)
                        .setMediaSession(mediaSession.getSessionToken())); 


        mediaSession.setMetadata
            (new MediaMetadataCompat.Builder()
                .putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART,icon)
                .putString(MediaMetadata.METADATA_KEY_TITLE, "TEST TITLE")
                .putString(MediaMetadata.METADATA_KEY_ARTIST, "TEST ARTIST")
                .build()
        );



        startForeground(1, builder.build()); 
    }


}
janu935
  • 71
  • 1
  • 4

5 Answers5

10

You now need to add metadata to your media session as well:

mediaSessionCompat.setMetadata(
    new MediaMetadataCompat.Builder()
        .putString(MediaMetadata.METADATA_KEY_TITLE, track.getTitle())
        .putString(MediaMetadata.METADATA_KEY_ARTIST, track.getArtist())
        .build()
    );
skvalex
  • 186
  • 1
  • 7
  • Thank you for your comment. I tried adding this to my code but it still doesn't generate the mediastyle notification. I should add that getting rid of the " .setMediaSession(mediaSessionCompat.getSessionToken())) " line gives me a notification, but its not an Oreo notification with the full background color, etc. – janu935 Oct 20 '20 at 01:15
  • @janu935 try to set metadata before you create MediaStyle. – skvalex Oct 20 '20 at 23:20
  • @janu935 try to remove setting METADATA_KEY_ALBUM_ART. As I remember, it didn't work for me. I had to use METADATA_KEY_ALBUM_ART_URI, but it also worked if no album art provided. – skvalex Oct 23 '20 at 15:58
  • I tried commenting out the "METADATA_KEY_ALBUM_ART" line but didn't work – janu935 Oct 24 '20 at 16:06
  • have you got any solution – Waqas Yousaf Apr 26 '21 at 06:41
  • Is that explained anywhere in the documentation? – barteks2x Sep 02 '22 at 08:00
  • It worked for me, on both (virtual device & physical device)!! But I wonder why before adding the meta data it wasn't working on physical device but was showing on emulator(virtual device). – V Surya Kumar Sep 14 '22 at 07:55
2

I had same issue and was able to resolve it only once I set compileSdkVersion to 30.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Hani
  • 21
  • 2
0

Just had the same problem. Turns out .setOngoing(true) doesn't seem to work right with Mediastyle notifications. Once I removed it, the notification showed up correctly.

michpohl
  • 852
  • 8
  • 30
0

There is a simple solution to it. Just add these lines after mediaSessionCompat declaration.

 mediaSessionCompat=new MediaSessionCompat(this,"tag");



        mediaSessionCompat.setMetadata(
                new MediaMetadataCompat.Builder()
                .putString(MediaMetadata.METADATA_KEY_TITLE,"Song Title")
                .putString(MediaMetadata.METADATA_KEY_ARTIST,"Artist")
                .build());

Hope this helps.

0

Works for me : Should add "mMediaSession.setMetadata(..)" after setStyle(..)

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        ...
            mBuilder.setStyle(new androidx.media.app.NotificationCompat.MediaStyle()
                            .setMediaSession(mMediaSession.getSessionToken())
                            .setShowCancelButton(true)
                            .setShowActionsInCompactView(0, 1, 2)
                            .setCancelButtonIntent(
                                    MediaButtonReceiver.buildMediaButtonPendingIntent(
                                            context, PlaybackStateCompat.ACTION_STOP)))
                            .addAction(new NotificationCompat.Action(
                                    R.drawable.ic_noti_previous, "Previous",
                                    ppreviousIntent))
                            .addAction(new NotificationCompat.Action(
                                    R.drawable.ic_noti_pause, "Pause",
                                    pplayIntent))
                            .addAction(new NotificationCompat.Action(
                                    R.drawable.ic_noti_next, "Next",
                                    pnextIntent))
                            .addAction(new NotificationCompat.Action(
                                    R.drawable.ic_noti_close, "Close",
                                    pcloseIntent));
            mMediaSession.setMetadata(
                new MediaMetadataCompat.Builder()
                    .putString(MediaMetadata.METADATA_KEY_TITLE,"Song Title")
                                    .putString(MediaMetadata.METADATA_KEY_ARTIST,"Artist")
                                    .build());
    
else
{
    ...
}