不可变问题在使用Xamarin Forms的Android 13上。

huangapple go评论69阅读模式
英文:

immutable issues on android 13 using xamarin forms

问题

我正在为我的Android项目使用Xamarin.Forms当我在我的移动Android 13上运行代码时出现了以下问题

> java.lang.IllegalArgumentException: com.fd.trucker.app目标为S+版本31及以上要求在创建PendingIntent时指定FLAG_IMMUTABLE或FLAG_MUTABLE之一强烈考虑使用FLAG_IMMUTABLE仅在一些功能依赖于PendingIntent可变时使用FLAG_MUTABLE例如如果需要与内联回复或气泡一起使用

但是我已经在我使用的地方将immutable添加到PendingIntent

1.

Intent alarmIntent = new Intent(this, typeof(BootCompletedReceiver));
PendingIntent pending = PendingIntent.GetBroadcast(this, 0, alarmIntent, PendingIntentFlags.Immutable);
AlarmManager alarmManager = GetSystemService(AlarmService).JavaCast();
alarmManager.SetRepeating(AlarmType.RtcWakeup, BootCompletedReceiver.FirstReminder(), time, pending);
PendingIntent pendingIntent = PendingIntent.GetBroadcast(this, 0, alarmIntent, 0);

2.

NotificationManager manager = (NotificationManager)GetSystemService(NotificationService);
var seed = Convert.ToInt32(Regex.Match(Guid.NewGuid().ToString(), @"\d+").Value);
int id = a new Random(seed).Next(000000000, 999999999);
var push = a new Intent();
var fullScreenPendingIntent = PendingIntent.GetActivity(this, 0, push, PendingIntentFlags.Immutable);
NotificationCompat.Builder notification;
if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
{
var chan1 = a new NotificationChannel(PRIMARY_CHANNEL, a new Java.Lang.String("Primary"), NotificationImportance.High);
manager.CreateNotificationChannel(chan1);
notification = a new NotificationCompat.Builder(this, PRIMARY_CHANNEL);
}
else
{
notification = a 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((a new NotificationCompat.BigTextStyle()))
.SetPriority(NotificationCompat.PriorityHigh)
.SetColor(0x0a384a)
.SetAutoCancel(true);
manager.Notify(id, notification.Build());

3.

NotificationManager manager = (NotificationManager)GetSystemService(NotificationService);
var seed = Convert.ToInt32(Regex.Match(Guid.NewGuid().ToString(), @"\d+").Value);
int id = a new Random(seed).Next(000000000, 999999999);
var push = a new Intent();
var fullScreenPendingIntent = PendingIntent.GetActivity(this, 0, push, PendingIntentFlags.Immutable);
NotificationCompat.Builder notification;
if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
{
var chan1 = a new NotificationChannel(PRIMARY_CHANNEL, a new Java.Lang.String("Primary"), NotificationImportance.High);
manager.CreateNotificationChannel(chan1);
notification = a new NotificationCompat.Builder(this, PRIMARY_CHANNEL);
}
else
{
notification = a 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((a new NotificationCompat.BigTextStyle()))
.SetPriority(NotificationCompat.PriorityHigh)
.SetColor(0x0a384a)
.SetAutoCancel(true);
manager.Notify(id, notification.Build());

4.

NotificationManager manager = (NotificationManager)GetSystemService(NotificationService);
var seed = Convert.ToInt32(Regex.Match(Guid.NewGuid().ToString(), @"\d+").Value);
int id = a new Random(seed).Next(000000000, 999999999);
var intent = a 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 = a new NotificationChannel(PRIMARY_CHANNEL, a new Java.Lang.String("Primary"), NotificationImportance.High);
manager.CreateNotificationChannel(chan1);
notification = a new NotificationCompat.Builder(this, PRIMARY_CHANNEL);
}
else
{
notification = a 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((a new NotificationCompat.BigTextStyle()))
.SetPriority(NotificationCompat.PriorityHigh)
.SetColor(0x0a384a)
.SetAutoCancel(true)
.SetOngoing(false);
manager.Notify(Convert.ToInt32(tripid), notification.Build());

5.

NotificationManager manager = (NotificationManager)GetSystemService(NotificationService);
var seed = Convert.ToInt32(Regex.Match(Guid.NewGuid().ToString(), @"\d+").Value);
int id = a new Random(seed).Next(000000000, 999999999);
var push = a new Intent();
var fullScreenPendingIntent = PendingIntent.GetActivity(this, 0, push, PendingIntentFlags.Immutable);
NotificationCompat.Builder notification;
if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
{
var chan1 = a new NotificationChannel(PRIMARY_CHANNEL, a new Java.Lang.String("Primary"), NotificationImportance.High);
chan1.LightColor = Color.Green;
manager.CreateNotificationChannel(chan1);
notification = a new NotificationCompat.Builder(this, PRIMARY_CHANNEL);
}
else
{
#pragma warning disable CS0618 // Type or member is obsolete
notification = a 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((a new NotificationCompat.BigTextStyle()))
.SetPriority(NotificationCompat.PriorityHigh)
.SetColor(0x0a384a)
.SetAutoCancel(true);
manager.Notify(id, notification.Build());

6.

NotificationManager manager = (NotificationManager)GetSystemService(NotificationService);
var seed = Convert.ToInt32(Regex.Match(Guid.NewGuid().ToString(), @"\d+").Value);
int id = a new Random(seed).Next(000000000, 999999999);
string title = "";
string body = "";
var intent = a new Intent(this, typeof(MainActivity));
var i = p0 as Intent;
var bundle = i.Extras;
title = bundle.GetString("gcm

英文:

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);

2.

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());

3.

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());

4.

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());

5.

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());

6.

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.

答案1

得分: 1

我做了一些关于这个错误的研究。有些人遇到了这个问题:由于Firebase插件的较低版本以及升级Android依赖和Flutter插件后,问题得以解决。

你可以查看这个链接,了解有关Android 12支持带有不可变标志的PendingIntents的信息。

此外,你还可以查看有关降低SDK版本的评论,这也可以解决问题。

英文:

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.

huangapple
  • 本文由 发表于 2023年4月10日 22:13:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75977864.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定