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

Sreejith Sreenivasan 691 Reputation points
2022-12-25T18:39:29.087+00:00

I am getting the below error for my xamarin forms application on version 31 and above.

com.mycompanyname.myappname: 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.
[ImeFocusController] onWindowFocus: DecorView@74e918a[MainActivity] softInputMode=STATE_UNSPECIFIED|ADJUST_RESIZE|IS_FORWARD_NAVIGATION
[ImeFocusController] Restarting due to isRestartOnNextWindowFocus as true
[ImeFocusController] onViewFocusChanged, view=DecorView@74e918a[MainActivity], mServedView=null
[ImeFocusController] checkFocus: view=null next=DecorView@74e918a[MainActivity] force=true package=<none>
The thread 0xb has exited with code 0 (0x0).
[AndroidRuntime] FATAL EXCEPTION: pool-2-thread-1
[AndroidRuntime] Process: com.mycompanyname.myappname, PID: 7466
[AndroidRuntime] java.lang.IllegalArgumentException: com.mycompanyname.myappname: Targeting S+ (version 31 and above) >requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent.
[AndroidRuntime] 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.
[AndroidRuntime] at android.app.PendingIntent.checkFlags(PendingIntent.java:375)
[AndroidRuntime] at android.app.PendingIntent.getBroadcastAsUser(PendingIntent.java:645)
[AndroidRuntime] at android.app.PendingIntent.getBroadcast(PendingIntent.java:632)
[AndroidRuntime] at com.google.firebase.iid.zzl.zzd(Unknown Source:18)
[AndroidRuntime] at com.google.firebase.iid.zzl.zzb(Unknown Source:229)
[AndroidRuntime] at com.google.firebase.iid.zzl.zza(Unknown Source:0)
[AndroidRuntime] at com.google.firebase.iid.zzj.zzb(Unknown Source:41)
[AndroidRuntime] at com.google.firebase.iid.zzj.getToken(Unknown Source:56)
[AndroidRuntime] at com.google.firebase.iid.FirebaseInstanceId.getToken(Unknown Source:10)
[AndroidRuntime] at com.google.firebase.iid.FirebaseInstanceId.zzcfy(Unknown Source:4)
[AndroidRuntime] at com.google.firebase.iid.FirebaseInstanceIdService.zza(Unknown Source:148)
[AndroidRuntime] at com.google.firebase.iid.FirebaseInstanceIdService.handleIntent(Unknown Source:122)
[AndroidRuntime] at com.google.firebase.iid.zzc.run(Unknown Source:4)
[AndroidRuntime] at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
[AndroidRuntime] at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
[AndroidRuntime] at java.lang.Thread.run(Thread.java:920)
Thread started: #12
Java.Lang.IllegalArgumentException: 'com.mycompanyname.myappname: 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.'

I am using pendingIntent on my project for FCM.

var intent = new Intent(this, typeof(MainActivity));  
intent.AddFlags(ActivityFlags.ClearTop);  
var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.OneShot);  

I tried FLAG_IMMUTABLE and FLAG_MUTABLE instead of OneShot, but app is breaking. I commented the entire code related to FCM, but after that also, the app is breaking with the same issue.

I tried updating the FCM related Packages, that also don't work. Any other olutions?

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,292 questions
{count} votes

Accepted answer
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 35,121 Reputation points Microsoft Vendor
    2022-12-26T05:39:29.62+00:00

    Hello,

    This should be due to a lack of runtime environment.

    You could add this Nuget package into your project Xamarin.AndroidX.Work.Runtime.

    Best Regards,

    Alec Liu.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    2 people found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. Lakshmi Jayanna 5 Reputation points
    2023-01-18T08:01:43.1466667+00:00

    We are having the same problem. The above solution did not work.

    Xamarin Forms:

    Java.Lang.IllegalArgumentException

    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.

    1 person found this answer helpful.
    0 comments No comments

  2. Angel Cruz 0 Reputation points
    2023-03-02T07:12:54.5633333+00:00

    Just set PendingIntentFlags.OneShot, to PendingIntentFlags.NoCreate or PendingIntentFlags.Inmutable,