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

Android12(API31/32)Firebase推送报Could not send notification错误

Android 12 环境下 react-native-firebase@5.x 推送收不到问题修复方案

问题根因

Android 12(API Level 31+)系统强制要求:所有创建PendingIntent的场景必须显式指定FLAG_IMMUTABLE或FLAG_MUTABLE标志,未指定时系统会直接抛出IllegalArgumentException导致进程崩溃。
react-native-firebase@5.6.0版本发布早于Android 12正式版,通知模块的DisplayNotificationTask.createIntent方法创建通知跳转用的PendingIntent时未添加适配逻辑,是触发崩溃、通知展示失败的核心原因。

无版本升级适配步骤

  • 定位本地依赖源码:打开路径node_modules/react-native-firebase/android/src/main/java/io/invertase/firebase/notifications/DisplayNotificationTask.java,找到第474行附近创建PendingIntent的代码块。
  • 替换原有PendingIntent创建逻辑,添加版本兼容判断,代码示例如下:
// 原有逻辑一般为 PendingIntent.getActivity(context, notificationId, intent, PendingIntent.FLAG_UPDATE_CURRENT)
int pendingIntentFlags = PendingIntent.FLAG_UPDATE_CURRENT;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
    // 普通通知场景优先使用FLAG_IMMUTABLE,符合官方安全要求
    pendingIntentFlags |= PendingIntent.FLAG_IMMUTABLE;
    // 若存在通知内联回复、气泡通知等需要修改PendingIntent内容的场景,替换为FLAG_MUTABLE即可
    // pendingIntentFlags |= PendingIntent.FLAG_MUTABLE;
}
PendingIntent pendingIntent = PendingIntent.getActivity(context, notificationId, intent, pendingIntentFlags);
  • 全局排查项目中所有自定义创建PendingIntent的逻辑:包括通知动作按钮、本地通知、DeepLink跳转、广播唤起等场景下调用PendingIntent.getActivity/getBroadcast/getService的位置,全部按照上述规则补全flag适配,避免其他链路触发同类崩溃。
  • 补充Manifest配置:Android 12要求所有声明了<intent-filter>的四大组件必须显式配置android:exported属性,找到通知跳转目标Activity、Firebase相关的Service/Receiver组件,不需要外部应用唤起的组件设置android:exported="false",需要接收外部跳转的组件设置android:exported="true"。
  • 持久化修改:直接修改node_modules内的代码会在重新安装依赖时丢失,建议使用patch-package生成补丁:
    1. 安装依赖:npm i patch-package -D
    2. 执行命令生成补丁:npx patch-package react-native-firebase
    3. 在package.json的scripts节点添加脚本:"postinstall": "patch-package",后续所有团队成员安装依赖时会自动应用适配补丁。
  • 清理缓存重新构建:进入android目录执行./gradlew clean清除旧构建产物,重新打包后在Android 12/13设备上验证推送接收、通知点击跳转功能即可。

额外注意事项

  • 若项目targetSdkVersion升级到33,需要额外动态申请android.permission.POST_NOTIFICATIONS运行时权限,否则Android 13及以上设备会默认拦截通知展示。
  • 不要为了省事给所有PendingIntent统一加FLAG_MUTABLE,会带来通知劫持的安全风险,无特殊需求一律使用FLAG_IMMUTABLE。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.26 17:15:41