Android 12+设备FCM推送失效,TargetSdkVersion31报PendingIntent标志错误求助
问题概述
- 应用集成FCM推送通知功能,Android 12及以下设备正常工作
- 将
targetSdkVersion设置为31或更高时,无法接收通知,触发PendingIntent flag相关错误 targetSdkVersion设为30时功能正常,但不符合Google Play上架要求
错误日志
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:401)at android.app.PendingIntent.getBroadcastAsUser(PendingIntent.java:671)at android.app.PendingIntent.getBroadcast(PendingIntent.java:658)at com.google.firebase.iid.zzl.zzd(Unknown Source:18)at com.google.firebase.iid.zzl.zzb(Unknown Source:230)at com.google.firebase.iid.zzl.zza(Unknown Source:0)at com.google.firebase.iid.zzj.zzb(Unknown Source:42)at com.google.firebase.iid.zzj.getToken(Unknown Source:58)at com.google.firebase.iid.FirebaseInstanceId.getToken(Unknown Source:10)at com.google.firebase.iid.FirebaseInstanceId.zzcfy(Unknown Source:4)at com.google.firebase.iid.FirebaseInstanceIdService.zza(Unknown Source:156)at com.google.firebase.iid.FirebaseInstanceIdService.handleIntent(Unknown Source:303)at com.google.firebase.iid.zzc.run(Unknown Source:4)at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1137)at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:637)at java.lang.Thread.run(Thread.java:1012)
解决方案
1. 升级Firebase依赖版本
你当前使用的Firebase Messaging(11.4.2)和Firebase Core(11.4.2)版本过低,未适配Android 12的PendingIntent flag强制要求。需要升级到至少21.0.0及以上版本(该版本开始支持Android 12的相关规范)。
修改Module级build.gradle中的依赖:
// 替换旧的Firebase依赖为最新稳定版 implementation 'com.google.firebase:firebase-messaging:23.4.0' implementation 'com.google.firebase:firebase-core:21.1.1' // 推荐移除全量play-services依赖,按需引入拆分模块(比如仅引入用到的功能) // 移除:implementation 'com.google.android.gms:play-services:11.4.2' // 示例:如需定位功能,单独引入:implementation 'com.google.android.gms:play-services-location:21.0.1'
2. 升级Google Services插件版本
项目级build.gradle中的google-services插件版本(3.2.0)与新版Firebase依赖不兼容,需升级到4.3.10及以上:
classpath 'com.google.gms:google-services:4.3.15' // 或最新稳定版
3. 替换已弃用的FirebaseInstanceIdService
旧的FirebaseInstanceIdService已被官方弃用,改用FirebaseMessagingService的onNewToken方法处理token更新:
public class MyFirebaseMessagingService extends FirebaseMessagingService { @Override public void onNewToken(String token) { super.onNewToken(token); // 将新token上传到你的服务器 sendRegistrationToServer(token); } private void sendRegistrationToServer(String token) { // 实现你的服务器上传逻辑 } }
同时在AndroidManifest.xml中更新服务声明:
<service android:name=".MyFirebaseMessagingService" android:exported="false"> <intent-filter> <action android:name="com.google.firebase.MESSAGING_EVENT" /> </intent-filter> </service>
4. 自定义通知时添加正确的PendingIntent Flag
如果应用自行创建通知的PendingIntent,需根据场景添加FLAG_IMMUTABLE或FLAG_MUTABLE:
- 普通通知场景推荐使用
PendingIntent.FLAG_IMMUTABLE - 需要支持内回复、气泡交互等功能时使用
PendingIntent.FLAG_MUTABLE
示例代码:
Intent intent = new Intent(this, MainActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity( this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE );
5. 同步编译配置
- 将
buildToolsVersion升级到与compileSdkVersion匹配的版本(比如compileSdkVersion 33对应buildToolsVersion '33.0.2') - 保持
targetSdkVersion为31或更高,满足Google Play上架要求
内容的提问来源于stack exchange,提问作者Anubhab Das

