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

Android 12推送通知报错:createPendingIntent触发IllegalArgumentException

修复Android 12推送通知的PendingIntent Flag错误

问题根源

从堆栈跟踪可以明确,错误并非来自你编写的NavDeepLinkBuilder代码,而是你的Firebase Messaging 20.3.0版本在创建PendingIntent时未指定FLAG_IMMUTABLE或FLAG_MUTABLE,不符合Android 12(API 31+)的强制要求。你使用的Navigation 2.5.1版本确实已经适配了该规则,但本次问题的核心出在Firebase依赖上。

解决方案

1. 升级Firebase Messaging依赖版本

将Firebase Messaging升级到21.0.0或更高版本,该版本开始原生支持Android 12的PendingIntent flag要求。在Module级别的build.gradle中更新依赖:

implementation 'com.google.firebase:firebase-messaging:23.4.0' // 可替换为最新稳定版

推荐使用Firebase BoM统一管理依赖版本,避免版本冲突:

dependencies {
    // 引入Firebase BoM
    implementation platform('com.google.firebase:firebase-bom:32.6.0')

    // 无需手动指定版本,BoM会自动同步兼容版本
    implementation 'com.google.firebase:firebase-messaging'
}

2. 自定义通知的PendingIntent Flag补充(可选)

如果你的自定义通知需要手动指定flag(比如支持内回复、气泡功能时需用FLAG_MUTABLE),可以使用Navigation库提供的带参数的createPendingIntent()方法:

val contentIntent = NavDeepLinkBuilder(this).apply{
   setComponentName(MainActivity::class.java)
   setGraph(R.navigation.nav_graph)
   setDestination(R.id.dialog_custom)
   setArguments(arguments)
}.createPendingIntent(PendingIntent.FLAG_IMMUTABLE) // 手动传入指定flag

不过根据你的堆栈信息,这一步不是必须的,核心修复动作仍是升级Firebase Messaging版本。

验证

升级依赖后,重新构建并运行应用,测试推送通知功能,该错误即可消除。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.04 23:25:19