Search code examples
androidnotificationsmediasession

Notification setStyle not working in sdk30


enter image description here

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

When I set .style(), notification is not output. Is there anything in the mediaSessionCompat that you need to do other than setting context and tag?


Solution

  • If you use the setStyle method on a notification, you need to provide the MediaSession with Metadata.

    private void updateMetadata () {
    mediaSession.setMetadata(new MediaMetadataCompat.Builder()
                                         .putString(MediaMetadataCompat.METADATA_KEY_ARTIST, song.getArtistTitle())
                                         .putString(MediaMetadataCompat.METADATA_KEY_ALBUM_ARTIST, song.getArtistTitle())
                                         .putString(MediaMetadataCompat.METADATA_KEY_ALBUM, song.getAlbumTitle())
                                         .putString(MediaMetadataCompat.METADATA_KEY_TITLE, song.getTitle())
                                         .putLong(MediaMetadataCompat.METADATA_KEY_DURATION, song.getDuration())
                                         .putLong(MediaMetadataCompat.METADATA_KEY_TRACK_NUMBER, getPosition())
                                         .putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART, null)
                                         .build());
    }
    
    

    Don't forget to call this method, for example when your data changes. I don't know how you handle that so I can't provide a correct place for it. I'd say the same place you update the notification when a song changes and it's only a suggestion.