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

Xamarin Forms适配Android12(API31+)PendingIntent崩溃问题求助

问题解决:Xamarin Forms Android 12上Firebase推送崩溃(PendingIntent Flags错误)

问题描述

用Xamarin Forms默认模板开发的应用,在Android 12以下设备运行正常,但Android 12(API 31+)设备上:

  • 发布模式直接无显示崩溃
  • 调试模式抛出异常:

Java.Lang.IllegalArgumentException: 'com.infinity.estrats: 针对S+(版本31及以上)创建PendingIntent时,必须指定FLAG_IMMUTABLE或FLAG_MUTABLE。强烈建议使用FLAG_IMMUTABLE,仅当功能依赖可变PendingIntent时(如需要配合内联回复或气泡功能)才使用FLAG_MUTABLE。'

使用Plugin.Firebasepushnotifcation处理通知,自行编写的配置代码无效。

解决方案

1. 优先升级插件到兼容Android 12的版本

旧版本的Plugin.Firebasepushnotifcation未适配Android 12对PendingIntent的强制要求,直接把插件升级到最新稳定版(建议v4.5.0及以上版本),多数情况下升级后即可解决问题。

2. 正确配置插件的PendingIntent Flags

如果升级后仍有问题,在MainApplication的OnCreate方法中,直接设置插件的PendingIntentFlags属性,让插件内部创建PendingIntent时带上合规的Flags(原有代码无效是因为未关联到插件逻辑):

protected override void OnCreate()
{
    base.OnCreate();

    // 初始化Firebase推送通知
    FirebasePushNotificationManager.Initialize(this, true);

    // 针对Android 12及以上版本配置
    if (Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.S)
    {
        // 设置默认通知通道
        FirebasePushNotificationManager.DefaultNotificationChannelId = "DefaultChannel";
        FirebasePushNotificationManager.DefaultNotificationChannelName = "General";
        // 设置PendingIntent Flags,推荐使用IMMUTABLE
        FirebasePushNotificationManager.PendingIntentFlags = PendingIntentFlags.UpdateCurrent | PendingIntentFlags.Immutable;
    }
}

3. 发布模式需配置混淆规则(ProGuard/R8)

如果发布模式仍崩溃,需确保混淆工具未移除Firebase相关类,在proguard.cfg中添加以下规则:

-keep class com.google.firebase.** { *; }
-keep class plugin.firebasepushnotification.** { *; }

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.08 21:50:24