Question Detail

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

1 years ago Views 855 Visit Post Reply

Fatal Exception: java.lang.IllegalArgumentException: in.app.package: Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent.
Strongly consider using FLAG_IMMUTABLE, only use FLAG_MUTABLE if some functionality depends on the PendingIntent being mutable, e.g. if it needs to be used with inline replies or bubbles.
       at android.app.PendingIntent.checkFlags(PendingIntent.java:377)
       at android.app.PendingIntent.getActivityAsUser(PendingIntent.java:460)
       at android.app.PendingIntent.getActivity(PendingIntent.java:446)
       at android.app.PendingIntent.getActivity(PendingIntent.java:410)
       at com.app.package.fcm.FirebaseMessageReceiver.showNewNotification(FirebaseMessageReceiver.java:1)
       at com.google.firebase.messaging.FirebaseMessagingService.dispatchMessage(FirebaseMessagingService.java:46)
       at com.google.firebase.messaging.EnhancedIntentService.lambda$processIntent$0$com-google-firebase-messaging-EnhancedIntentService(EnhancedIntentService.java:24)
       at com.google.firebase.messaging.EnhancedIntentService$$ExternalSyntheticLambda1.run(EnhancedIntentService.java:24)
       at com.google.android.gms.common.api.internal.zza.run$bridge(zza.java:24)
       at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
       at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
       at com.google.android.gms.common.util.concurrent.zza.run(zza.java:6)
       at java.lang.Thread.run(Thread.java:920)


Thread Reply

Hemant Haritash

- 10 months ago

FLAG_IMMUTABLE: This flag indicates that the PendingIntent object is not allowed to be modified or replaced by subsequent calls to the same PendingIntent object.

FLAG_MUTABLE: This flag indicates that the PendingIntent object is allowed to be modified or replaced by subsequent calls to the same PendingIntent object.

When targeting Android 11 (API level 31) and higher, it is required to specify either FLAG_IMMUTABLE or FLAG_MUTABLE when creating a PendingIntent. This is because starting in Android 11, the behavior of PendingIntent has changed to prevent unintended modifications to the original PendingIntent.

Here is an example of how you might use these flags when creating a PendingIntent in Android:

Intent intent = new Intent(context, MyReceiver.class); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_IMMUTABLE);

In this example, the PendingIntent is created with the FLAG_IMMUTABLE flag, which means it cannot be modified or replaced by subsequent calls to the same PendingIntent object.