Home > Back-end >  MediaSessionCompat: Targeting S (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_
MediaSessionCompat: Targeting S (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_

Time:09-26

i got this error in music player notification.

Targeting S (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent.

i got solution to add FLAG_MUTABLE in to PendingIntent but that not working for me.

i was try this solution from here

public void crateNotification(){
        Intent notificationIntent = new Intent(this, SplashActivity.class);
        notificationIntent.setAction(Intent.ACTION_MAIN);
        notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        notificationIntent.putExtra("isNotification", true);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
                notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_MUTABLE);

        String NOTIFICATION_CHANNEL_ID = "onlinemp3_ch_1";
        notification = new NotificationCompat.Builder(this)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.app_icon))
                .setContentTitle(getString(R.string.app_name))
                .setPriority(Notification.PRIORITY_LOW)
                .setContentIntent(pendingIntent)
                .setSmallIcon(R.drawable.ic_notification)
                .setTicker(Constant.arrayList_play_book.get(Constant.playPos).getChapterTitle())
                .setChannelId(NOTIFICATION_CHANNEL_ID)
                .setOnlyAlertOnce(true);

        NotificationChannel mChannel;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            CharSequence name = getString(R.string.app_name);// The user-visible name of the channel.
            mChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, name, NotificationManager.IMPORTANCE_LOW);
            mNotificationManager.createNotificationChannel(mChannel);

            mMediaSession = new MediaSessionCompat(getApplicationContext(), getString(R.string.app_name));
            mMediaSession.setFlags(
                    MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS |
                            MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
    //            mMediaSession.setPlaybackState(mPlaybackState);


            mMediaSession.setMetadata(new MediaMetadataCompat.Builder()
                    .putString(MediaMetadata.METADATA_KEY_TITLE, Constant.arrayList_play_book.get(Constant.playPos).getChapterTitle())
                    .build());

            notification.setStyle(new androidx.media.app.NotificationCompat.MediaStyle()
                            .setMediaSession(mMediaSession.getSessionToken())
                            .setShowCancelButton(true)
                            .setShowActionsInCompactView(0, 1, 2)
                            .setCancelButtonIntent(
                                    MediaButtonReceiver.buildMediaButtonPendingIntent(getApplicationContext(), PlaybackStateCompat.ACTION_STOP)))
                    .addAction(new NotificationCompat.Action(
                            R.mipmap.ic_noti_previous, "Previous",
                            ppreviousIntent))
                    .addAction(new NotificationCompat.Action(
                            R.mipmap.ic_noti_pause, "Pause",
                            pplayIntent))
                    .addAction(new NotificationCompat.Action(
                            R.mipmap.ic_noti_next, "Next",
                            pnextIntent))
                    .addAction(new NotificationCompat.Action(
                            R.mipmap.ic_noti_close, "Close",
                            pcloseIntent));

          
        }
    } 

CodePudding user response:

It's not a proper pendingintent flags error.

it's a MediaSessionCompat() error. i was fixed this error some days ago.

For sdk 32 or above, change in Your MediaSessionCompat() old code

mMediaSession = new MediaSessionCompat(getApplicationContext(), getString(R.string.app_name));

to

ComponentName componentName = new ComponentName(getPackageName(), 
MediaButtonIntentReceiver.class.getName());

mMediaSession = new 
MediaSessionCompat(getApplicationContext(),BuildConfig.APPLICATION_ID, 
componentName,pendingIntent);

And, in your notification.setStyle() remove this code

.setCancelButtonIntent(                           
MediaButtonReceiver.buildMediaButtonPendingIntent(getApplicationContext(), 
PlaybackStateCompat.ACTION_STOP))
  • Related