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

React Native Android12启动崩溃求助:PendingIntent未指定FLAG问题

问题描述

React Native应用在部分Android 12用户设备上启动崩溃,Crashlytics已捕获错误但本地无法复现。错误提示targetSdkVersion 31及以上版本要求创建PendingIntent时必须指定FLAG_IMMUTABLE或FLAG_MUTABLE,怀疑是react-native-firebase库的问题。

错误堆栈
Fatal Exception: java.lang.IllegalArgumentException: com.halkhata: 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:382)
       at android.app.PendingIntent.getActivityAsUser(PendingIntent.java:465)
       at android.app.PendingIntent.getActivity(PendingIntent.java:451)
       at android.app.PendingIntent.getActivity(PendingIntent.java:415)
       at com.google.firebase.messaging.CommonNotificationBuilder.createContentIntent(CommonNotificationBuilder.java:26)
       at com.google.firebase.messaging.CommonNotificationBuilder.createNotificationInfo(CommonNotificationBuilder.java:69)
       at com.google.firebase.messaging.CommonNotificationBuilder.createNotificationInfo(CommonNotificationBuilder.java:34)
       at com.google.firebase.messaging.DisplayNotification.handleNotification(DisplayNotification.java:28)
       at com.google.firebase.messaging.FirebaseMessagingService.dispatchMessage(FirebaseMessagingService.java:36)
       at com.google.firebase.messaging.FirebaseMessagingService.passMessageIntentToSdk(FirebaseMessagingService.java:130)
       at com.google.firebase.messaging.FirebaseMessagingService.handleMessageIntent(FirebaseMessagingService.java:12)
       at com.google.firebase.messaging.FirebaseMessagingService.handleIntent(FirebaseMessagingService.java:89)
       at com.google.firebase.messaging.EnhancedIntentService.lambda$processIntent$0$EnhancedIntentService(EnhancedIntentService.java:1)
       at com.google.firebase.messaging.EnhancedIntentService$$Lambda$0.run(EnhancedIntentService.java:6)
       at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1137)
       at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:637)
       at com.google.android.gms.common.util.concurrent.zza.run(zza.java:6)
       at java.lang.Thread.run(Thread.java:1012)
解决方法
  • 升级react-native-firebase依赖:这个问题是旧版本@react-native-firebase/messaging(及底层Google Firebase Messaging SDK)未适配Android 12的PendingIntent规则导致的。直接升级到@react-native-firebase/messaging v14.0.0及以上版本,同时同步更新Firebase BOM到兼容Android 12的版本(如com.google.firebase:firebase-bom:29.0.0及以上)。

  • 强制替换原生SDK版本:如果暂时无法升级react-native-firebase,可在android/app/build.gradle中添加依赖强制替换规则,直接使用修复了该问题的Firebase Messaging原生库:

configurations.all {
    resolutionStrategy {
        force 'com.google.firebase:firebase-messaging:23.0.0' // 可按需选择更高兼容版本
    }
}
  • 确认Android项目配置:确保项目的targetSdkVersion和compileSdkVersion都设置为31及以上,这是Android 12适配的基础要求。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.07 08:40:40