Search code examples
xamarin.formsxamarin.android

immutable issues on android 13 using xamarin forms


I am using xamarin.forms for my android project. When I run the code to my mobile android 13, I got the following issue:

java.lang.IllegalArgumentException: com.fd.trucker.app: 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:401) at android.app.PendingIntent.getBroadcastAsUser(PendingIntent.java:671) at android.app.PendingIntent.getBroadcast(PendingIntent.java:658) at com.google.firebase.iid.zzl.zzd(Unknown Source:18) at com.google.firebase.iid.zzl.zzb(Unknown Source:229) at com.google.firebase.iid.zzl.zza(Unknown Source:0) at com.google.firebase.iid.zzj.zzb(Unknown Source:41) at com.google.firebase.iid.zzj.getToken(Unknown Source:56) at com.google.firebase.iid.FirebaseInstanceId.getToken(Unknown Source:10) at com.google.firebase.iid.FirebaseInstanceId.zzcfy(Unknown Source:4) at com.google.firebase.iid.FirebaseInstanceIdService.zza(Unknown Source:148) at com.google.firebase.iid.FirebaseInstanceIdService.handleIntent(Unknown Source:122) at com.google.firebase.iid.zzc.run(Unknown Source:4) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1137) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:637) at java.lang.Thread.run(Thread.java:1012)

But I have already added the immutable to the pending intent which place I have used. 1.

Intent alarmIntent = new Intent(this, typeof(BootCompletedReceiver));
PendingIntent pending = PendingIntent.GetBroadcast(this, 0, alarmIntent, PendingIntentFlags.Immutable);
AlarmManager alarmManager = GetSystemService(AlarmService).JavaCast<AlarmManager>();
alarmManager.SetRepeating(AlarmType.RtcWakeup, BootCompletedReceiver.FirstReminder(), time, pending);
PendingIntent pendingIntent = PendingIntent.GetBroadcast(this, 0, alarmIntent, 0);
NotificationManager manager = (NotificationManager)GetSystemService(NotificationService);
var seed = Convert.ToInt32(Regex.Match(Guid.NewGuid().ToString(), @"\d+").Value);
int id = new Random(seed).Next(000000000, 999999999);
var push = new Intent();
var fullScreenPendingIntent = PendingIntent.GetActivity(this, 0, push, PendingIntentFlags.Immutable);
NotificationCompat.Builder notification;
if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
{
    var chan1 = new NotificationChannel(PRIMARY_CHANNEL, new Java.Lang.String("Primary"), NotificationImportance.High);
    manager.CreateNotificationChannel(chan1);
    notification = new NotificationCompat.Builder(this, PRIMARY_CHANNEL);
}
else
{
    notification = new NotificationCompat.Builder(this, PRIMARY_CHANNEL);
}
notification.SetContentIntent(fullScreenPendingIntent)
         .SetContentTitle("Upgrade Failed")
         .SetContentText(Constants.DBUpgradeFailNotifications)
         .SetLargeIcon(BitmapFactory.DecodeResource(Resources, Resource.Drawable.icon))
         .SetSmallIcon(Resource.Drawable.icon_transparent)
         .SetStyle((new NotificationCompat.BigTextStyle()))
         .SetPriority(NotificationCompat.PriorityHigh)
         .SetColor(0x0a384a)
         .SetAutoCancel(true);
manager.Notify(id, notification.Build());
NotificationManager manager = (NotificationManager)GetSystemService(NotificationService);
var seed = Convert.ToInt32(Regex.Match(Guid.NewGuid().ToString(), @"\d+").Value);
int id = new Random(seed).Next(000000000, 999999999);
var push = new Intent();
var fullScreenPendingIntent = PendingIntent.GetActivity(this, 0, push, PendingIntentFlags.Immutable);
NotificationCompat.Builder notification;
if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
{
    var chan1 = new NotificationChannel(PRIMARY_CHANNEL, new Java.Lang.String("Primary"), NotificationImportance.High);
    manager.CreateNotificationChannel(chan1);
    notification = new NotificationCompat.Builder(this, PRIMARY_CHANNEL);
}
else
{
    notification = new NotificationCompat.Builder(this, PRIMARY_CHANNEL);
}
notification.SetContentIntent(fullScreenPendingIntent)
         .SetContentTitle("Destination Arrival")
         .SetContentText("It appears you've arrivedDestroy at the destination.Please enter the BL & Scale Ticket.")
         .SetLargeIcon(BitmapFactory.DecodeResource(Resources, Resource.Drawable.icon))
         .SetSmallIcon(Resource.Drawable.icon_transparent)
         .SetStyle((new NotificationCompat.BigTextStyle()))
         .SetPriority(NotificationCompat.PriorityHigh)
         .SetColor(0x0a384a)
         .SetAutoCancel(true);
manager.Notify(id, notification.Build());
NotificationManager manager = (NotificationManager)GetSystemService(NotificationService);
var seed = Convert.ToInt32(Regex.Match(Guid.NewGuid().ToString(), @"\d+").Value);
int id = new Random(seed).Next(000000000, 999999999);
var intent = new Intent(this, typeof(MainActivity));
intent.PutExtra("title", title);
intent.PutExtra("body", body);
intent.PutExtra("tripid", tripid);
intent.AddFlags(ActivityFlags.ClearTop | ActivityFlags.SingleTop);
intent.SetData(Android.Net.Uri.Parse("custom://" + SystemClock.CurrentThreadTimeMillis()));
var fullScreenPendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.Immutable);
NotificationCompat.Builder notification;
if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
{
    var chan1 = new NotificationChannel(PRIMARY_CHANNEL, new Java.Lang.String("Primary"), NotificationImportance.High);
    manager.CreateNotificationChannel(chan1);
    notification = new NotificationCompat.Builder(this, PRIMARY_CHANNEL);
}
else
{
    notification = new NotificationCompat.Builder(this, PRIMARY_CHANNEL);
}
notification.SetContentIntent(fullScreenPendingIntent)
         .SetContentTitle(title)
         .SetContentText(body)
         .SetLargeIcon(BitmapFactory.DecodeResource(Resources, Resource.Drawable.icon))
         .SetSmallIcon(Resource.Drawable.icon_transparent)
         .SetStyle((new NotificationCompat.BigTextStyle()))
         .SetPriority(NotificationCompat.PriorityHigh)
         .SetColor(0x0a384a)
         .SetAutoCancel(true)
         .SetOngoing(false);
manager.Notify(Convert.ToInt32(tripid), notification.Build());
NotificationManager manager = (NotificationManager)GetSystemService(NotificationService);
var seed = Convert.ToInt32(Regex.Match(Guid.NewGuid().ToString(), @"\d+").Value);
int id = new Random(seed).Next(000000000, 999999999);
var push = new Intent();
var fullScreenPendingIntent = PendingIntent.GetActivity(this, 0, push, PendingIntentFlags.Immutable);
NotificationCompat.Builder notification;
if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
{
    var chan1 = new NotificationChannel(PRIMARY_CHANNEL, new Java.Lang.String("Primary"), NotificationImportance.High);
    chan1.LightColor = Color.Green;
    manager.CreateNotificationChannel(chan1);
    notification = new NotificationCompat.Builder(this, PRIMARY_CHANNEL);
}
else
{
#pragma warning disable CS0618 // Type or member is obsolete
    notification = new NotificationCompat.Builder(this);
#pragma warning restore CS0618 // Type or member is obsolete
}
notification.SetContentIntent(fullScreenPendingIntent)
         .SetContentTitle(message.GetNotification().Title)
         .SetContentText(message.GetNotification().Body)
         .SetLargeIcon(BitmapFactory.DecodeResource(Resources, Resource.Drawable.icon))
         .SetSmallIcon(Resource.Drawable.icon_transparent)
         .SetStyle((new NotificationCompat.BigTextStyle()))
         .SetPriority(NotificationCompat.PriorityHigh)
         .SetColor(0x0a384a)
         .SetAutoCancel(true);
manager.Notify(id, notification.Build());
NotificationManager manager = (NotificationManager)GetSystemService(NotificationService);
var seed = Convert.ToInt32(Regex.Match(Guid.NewGuid().ToString(), @"\d+").Value);
int id = new Random(seed).Next(000000000, 999999999);
string title = "";
string body = "";
var intent = new Intent(this, typeof(MainActivity));
var i = p0 as Intent;
var bundle = i.Extras;
title = bundle.GetString("gcm.notification.title");
body = bundle.GetString("gcm.notification.body");
var tripid = bundle.GetString("key2");
intent.PutExtra("title", title);
intent.PutExtra("body", body);
intent.PutExtra("tripid", tripid);
intent.AddFlags(ActivityFlags.ClearTop | ActivityFlags.SingleTop);
intent.SetData(Android.Net.Uri.Parse("custom://" + SystemClock.CurrentThreadTimeMillis()));
var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.Immutable);
NotificationCompat.Builder notification;
if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
{
    var chan1 = new NotificationChannel(PRIMARY_CHANNEL, new Java.Lang.String("Primary"), NotificationImportance.High);
    chan1.LightColor = Color.Green;
    manager.CreateNotificationChannel(chan1);
    notification = new NotificationCompat.Builder(this, PRIMARY_CHANNEL);
}
else
{
#pragma warning disable CS0618 // Type or member is obsolete
    notification = new NotificationCompat.Builder(this);
#pragma warning restore CS0618 // Type or member is obsolete
}
notification.SetContentIntent(pendingIntent)
         .SetContentTitle(title)
         .SetContentText(body)
         .SetLargeIcon(BitmapFactory.DecodeResource(Resources, Resource.Drawable.icon))
         .SetSmallIcon(Resource.Drawable.icon_transparent)
         .SetStyle((new NotificationCompat.BigTextStyle()))
         .SetPriority(NotificationCompat.PriorityHigh)
         .SetColor(0x0a384a)
         .SetAutoCancel(true)
         .SetOngoing(false);
manager.Notify(Convert.ToInt32(tripid), notification.Build());  

Please give me the solution for this one.


Solution

  • I did some research about the error. Some people how met the issue: Its crashed due to lower version of Firebase Plugins and after upgrading the Android Dependencies and Flutter plugins it got resolved.

    You can check this link about Android 12 support for PendingIntents with the Immutable Flag.

    In addition, you can check this comment about make the sdk version lower, it also fix the problem.