(KOTLIN) Requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent

Issue

In every new message that comes to my account, the application restarts itself. Then it gives this error.I tried to find the solution but failed.I write the error code on the bottom line.

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.edusoft.teacher.myeducare, PID: 7367
    java.lang.RuntimeException: Unable to start receiver com.onesignal.GcmBroadcastReceiver: java.lang.IllegalArgumentException: com.edusoft.teacher.myeducare: 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.ActivityThread.handleReceiver(ActivityThread.java:4438)
        at android.app.ActivityThread.access$1700(ActivityThread.java:265)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2114)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loopOnce(Looper.java:210)
        at android.os.Looper.loop(Looper.java:299)
        at android.app.ActivityThread.main(ActivityThread.java:8168)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:556)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1037)
     Caused by: java.lang.IllegalArgumentException: com.edusoft.teacher.myeducare: 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:378)
        at android.app.PendingIntent.getBroadcastAsUser(PendingIntent.java:648)
        at android.app.PendingIntent.getBroadcast(PendingIntent.java:635)
        at com.onesignal.GenerateNotification.getNewActionPendingIntent(GenerateNotification.java:195)
        at com.onesignal.GenerateNotification.createGenericPendingIntentsForNotif(GenerateNotification.java:404)
        at com.onesignal.GenerateNotification.showNotification(GenerateNotification.java:388)
        at com.onesignal.GenerateNotification.fromJsonPayload(GenerateNotification.java:116)
        at com.onesignal.NotificationBundleProcessor.ProcessJobForDisplay(NotificationBundleProcessor.java:115)
        at com.onesignal.NotificationBundleProcessor.ProcessFromGCMIntentService(NotificationBundleProcessor.java:98)
        at com.onesignal.GcmBroadcastReceiver.startGCMService(GcmBroadcastReceiver.java:138)
        at com.onesignal.GcmBroadcastReceiver.processOrderBroadcast(GcmBroadcastReceiver.java:129)
        at com.onesignal.GcmBroadcastReceiver.onReceive(GcmBroadcastReceiver.java:70)
        at android.app.ActivityThread.handleReceiver(ActivityThread.java:4425)
            ... 9 more

i found this code but either i did not use it correctly or it does not work properly

val updatedPendingIntent = PendingIntent.getActivity(
   applicationContext,
   NOTIFICATION_REQUEST_CODE,
   updatedIntent,
   PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT // setting the mutability flag 
)

Solution

If your app targets Android 12, you must specify the mutability of each PendingIntent object that your app creates. This additional requirement improves your app’s security.

PendingIntent contentIntent = PendingIntent.getActivity(context,NOTIFICATION_REQUEST_CODE, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);

we’re constructing a standard type of Intent that will open our app, and then simply wrapping that in a PendingIntent before adding it to our notification.In this case, since we have an exact action we know we want to perform, we construct a PendingIntent that cannot be modified by the app we pass it to by utilizing a flag called FLAG_IMMUTABLE.

This change is must required if you are targeting Android S+ and you have integrate FCM in you app.

You can check this blog if you plan to migrate your app compatible with Android 12.

Answered By – krupa parekh

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published