You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Xamarin.Forms安卓API33后台接收通知崩溃问题求助

解决Android API33下后台通知PendingIntent报错问题

核心原因

Android 12(API31)及以上版本强制要求:创建PendingIntent时必须显式指定FLAG_IMMUTABLE或FLAG_MUTABLE,否则会抛出IllegalArgumentException。你前台接收通知正常是因为前台触发的通知已设置对应flag,但后台触发的通知(比如远程推送、后台任务发起的通知)大概率遗漏了这个设置。

具体修复步骤

  • 全局检查所有PendingIntent创建逻辑
    别只盯着当前贴的SendNotifications方法,应用里所有创建PendingIntent的地方(比如推送服务、后台任务、广播接收器相关的PendingIntent)都要加上对应flag:

    • 不需要修改Intent内容的场景(比如通知跳转固定页面),用PendingIntentFlags.Immutable
    • 需要动态更新Intent参数的场景,用PendingIntentFlags.Mutable
  • 修复当前SendNotifications代码的问题
    你的代码里Intent是空的,点击通知不会有任何响应,同时requestCode固定为0可能导致重复覆盖,建议修改为:

    public void SendNotifications()
    {
        try
        {
            NotificationManager manager = (NotificationManager)GetSystemService(NotificationService);
            // 简化随机id生成逻辑
            int id = new Random().Next(100000, 999999);
            // 替换空白Intent为指向主Activity的Intent,确保点击通知能跳转页面
            var push = new Intent(this, typeof(MainActivity));
            // 根据系统版本兼容设置PendingIntent flag
            PendingIntentFlags flags;
            if (Build.VERSION.SdkInt >= BuildVersionCodes.S)
            {
                flags = PendingIntentFlags.Immutable | PendingIntentFlags.UpdateCurrent;
            }
            else
            {
                flags = PendingIntentFlags.UpdateCurrent;
            }
            var fullScreenPendingIntent = PendingIntent.GetActivity(this, id, push, flags);
    
            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 arrived 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());
        }
        catch (Exception ex)
        {
            Logger.GetInstance().LogCreate("Droid/MainActivity/SendNotifications/Exception: " + ex.Message);
            AppCenterDataProvider.GetInstance().TrackError("Droid/MainActivity/SendNotifications/Exception", "", ex);
        }
    }
    
  • 确保Xamarin环境适配API33
    检查项目的Xamarin.Android版本是否为13.0或更高,同时更新AndroidX相关NuGet包,避免因版本过低导致flag枚举缺失或兼容性问题。

  • 检查后台推送服务的PendingIntent
    如果用了Firebase或其他推送服务,在OnMessageReceived等后台处理方法中创建通知时,也要给PendingIntent加上Immutable/Mutableflag,这是后台报错的常见诱因。


内容的提问来源于stack exchange,提问作者Manthiram

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 02:30:05