Targeting S+(API31+)创建PendingIntent已加FLAG仍报错求助
问题排查:S+设备上PendingIntent标记错误(已指定FLAG_IMMUTABLE仍报错)
错误日志
Exception java.lang.IllegalArgumentException: se.sensoralarm: 针对S+(版本31及以上)开发时,创建PendingIntent必须指定FLAG_IMMUTABLE或FLAG_MUTABLE其中一个标记。 强烈建议使用FLAG_IMMUTABLE,仅当某些功能依赖PendingIntent的可变性时才使用FLAG_MUTABLE,例如需要与内联回复或气泡功能配合使用的场景。 at android.app.PendingIntent.checkFlags (PendingIntent.java:408) at android.app.PendingIntent.getActivityAsUser (PendingIntent.java:491) at android.app.PendingIntent.getActivity (PendingIntent.java:477) at android.app.PendingIntent.getActivity (PendingIntent.java:441) at com.google.firebase.messaging.zza.zzh at com.google.firebase.messaging.FirebaseMessagingService.zzd at com.google.firebase.iid.zzg.run 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 (com.google.android.gms:play-services-basement@@18.1.0:2) at java.lang.Thread.run (Thread.java:1012)
自定义通知实现代码
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(getApplicationContext(), SplashScreenActivity.class), PendingIntent.FLAG_IMMUTABLE|PendingIntent.FLAG_UPDATE_CURRENT); Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.notification) .setContentTitle(title) .setContentText(messageBody) .setAutoCancel(true) .setSound(defaultSoundUri) .setContentIntent(pendingIntent); NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(0, notificationBuilder.build());
项目配置信息
build.gradle基础配置
compileSdkVersion 32 buildToolsVersion "30.0.3" defaultConfig { applicationId "se.sensoralarm" minSdkVersion 23 targetSdkVersion 32 versionCode 32 versionName "1.2.12" testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner' }
依赖配置
implementation 'androidx.legacy:legacy-support-v4:1.0.0' implementation 'com.google.android.gms:play-services-location:15.0.1' implementation 'com.google.android.play:core:1.10.2' testImplementation fileTree(dir: 'libs', include: ['*.jar']) androidTestImplementation('androidx.test.espresso:espresso-core:3.1.0', { exclude group: 'com.android.support', module: 'support-annotations' }) implementation 'androidx.work:work-runtime:2.8.1' implementation 'androidx.work:work-runtime-ktx:2.8.1' implementation 'androidx.appcompat:appcompat:1.0.0' implementation 'com.google.android.material:material:1.0.0' implementation 'androidx.constraintlayout:constraintlayout:1.1.3' implementation 'androidx.exifinterface:exifinterface:1.0.0' implementation 'com.squareup.okhttp3:okhttp:4.9.0' implementation 'com.google.code.gson:gson:2.8.2' implementation 'com.aurelhubert:ahbottomnavigation:2.1.0' implementation 'com.squareup.picasso:picasso:2.5.2' implementation 'com.github.chrisbanes:PhotoView:2.1.3' implementation 'com.jakewharton.threetenabp:threetenabp:1.1.0' implementation 'com.google.firebase:firebase-core:21.1.1' implementation 'com.google.firebase:firebase-messaging:17.3.4' implementation 'com.jakewharton:butterknife:10.2.3' implementation 'org.florescu.android.rangeseekbar:rangeseekbar-library:0.3.0' implementation 'com.github.lukedeighton:wheelview:0.4.2' implementation 'org.florescu.android.rangeseekbar:rangeseekbar-library:0.3.0' implementation 'com.intuit.ssp:ssp-android:1.0.6' implementation 'com.intuit.sdp:sdp-android:1.0.6' implementation 'com.google.firebase:firebase-crashlytics:18.3.7' implementation 'com.google.firebase:firebase-analytics:21.2.0' implementation("com.google.android.gms:play-services-base:18.1.0") annotationProcessor 'com.jakewharton:butterknife-compiler:10.2.3' testImplementation 'junit:junit:4.12'
问题分析与解决办法
从错误栈能明显看出,报错的PendingIntent并非来自你自定义的通知代码,而是Firebase Messaging内部创建的实例。你当前使用的firebase-messaging版本是17.3.4,这个版本发布于2019年,早于Android 12(API31)的推出,因此它内部创建PendingIntent时没有添加S+设备要求的FLAG_IMMUTABLE或FLAG_MUTABLE标记,导致触发错误。
解决步骤:
升级Firebase Messaging依赖版本:将
com.google.firebase:firebase-messaging升级到适配API31+的版本,建议至少升级到23.0.0及以上(该版本开始支持Android 12的PendingIntent标记要求)。示例修改:implementation 'com.google.firebase:firebase-messaging:23.0.0'同时建议统一Firebase相关依赖版本,避免冲突,比如可移除
firebase-core依赖(当前Firebase已用firebase-analytics替代firebase-core功能)。同步并清理项目:修改依赖后,执行
File -> Sync Project with Gradle Files,再通过Build -> Clean Project清理项目,最后重新构建确保新依赖生效。排查其他潜在问题:如果升级后仍报错,检查是否有其他第三方库存在类似未适配API31+的情况,但从当前错误栈来看,核心问题就是Firebase Messaging版本过低。
内容的提问来源于stack exchange,提问作者Kanwarpreet Singh
相关产品推荐
相关产品推荐

