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

TargetSdkVersion31下PendingIntent未指定FLAG致应用崩溃求助

TargetSdkVersion 31 下PendingIntent崩溃问题解决

问题背景

将应用targetSdkVersion升级到31后出现随机崩溃,报错提示:

Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent

已在MyFirebaseMessagingService中设置PendingIntent.FLAG_IMMUTABLE,并添加Work-runtime依赖,但问题仍未解决。

崩溃日志

--------- beginning of crash
2023-03-03 15:33:10.259 11639-12705/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
    Process:  PID: 11639
    java.lang.RuntimeException: An error occurred while executing doInBackground()
        at android.os.AsyncTask$4.done(AsyncTask.java:415)
        at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:381)
        at java.util.concurrent.FutureTask.setException(FutureTask.java:250)
        at java.util.concurrent.FutureTask.run(FutureTask.java:269)
        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)
     Caused by: java.lang.IllegalArgumentException:  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:402)
        at android.app.PendingIntent.getBroadcastAsUser(PendingIntent.java:672)
        at android.app.PendingIntent.getBroadcast(PendingIntent.java:659)
        at com.google.android.gms.gcm.zzm.<init>(Unknown Source:17)
        at com.google.android.gms.gcm.GcmNetworkManager.zzd(Unknown Source:28)
        at com.google.android.gms.gcm.GcmNetworkManager.schedule(Unknown Source:8)
        at com.quickblox.messages.services.SubscribeTaskManager.a(Unknown Source:58)
        at com.quickblox.messages.services.SubscribeService.c(Unknown Source:24)
        at com.quickblox.messages.services.SubscribeService.a(Unknown Source:150)
        at com.quickblox.messages.services.SubscribeService.a(Unknown Source:136)
        at com.quickblox.messages.services.SubscribeService.e(Unknown Source:12)
        at com.quickblox.messages.services.SubscribeService.b(Unknown Source:18)
        at com.quickblox.messages.services.SubscribeService.onHandleWork(Unknown Source:51)
        at androidx.core.app.JobIntentService$CommandProcessor.doInBackground(JobIntentService.java:396)
        at androidx.core.app.JobIntentService$CommandProcessor.doInBackground(JobIntentService.java:387)
        at android.os.AsyncTask$3.call(AsyncTask.java:394)
        at java.util.concurrent.FutureTask.run(FutureTask.java:264)
            ... 3 more
2023-03-03 15:33:10.263 1768-2337/? E/OplusNetworkStackManager: callback len = 6

问题分析

从崩溃栈可以明确看到,崩溃并非出自自己编写的MyFirebaseMessagingService代码,而是**QuickBlox SDK内部的SubscribeService**在调用GcmNetworkManager.schedule时创建了未指定FLAG的PendingIntent,不符合Android 12+的要求。

当前使用的QuickBlox SDK版本为3.9.2,该版本未适配Android 12的PendingIntent规则;同时依赖的旧版play-services-gcm也未处理此问题。

解决方案

方案1:升级QuickBlox SDK到适配版本

将QuickBlox SDK升级到最新稳定版(建议升级到4.x系列),新版本已修复Android 12+的PendingIntent适配问题。修改app/build.gradle中的依赖:

// 替换原QuickBlox依赖为最新版
implementation "com.quickblox:quickblox-android-sdk-chat:4.0.0"
implementation "com.quickblox:quickblox-android-sdk-content:4.0.0"
implementation "com.quickblox:quickblox-android-sdk-messages:4.0.0"
implementation "com.quickblox:quickblox-android-sdk-customobjects:4.0.0"
implementation "com.quickblox:quickblox-android-sdk-core:4.0.0"
implementation "com.quickblox:quickblox-android-sdk-videochat-webrtc:4.0.0"
implementation "com.quickblox:quickblox-android-sdk-conference:4.0.0"

方案2:强制兼容的Play Services GCM版本

若暂时无法升级QuickBlox SDK,可通过Gradle的依赖强制策略,使用已适配Android 12的play-services-gcm版本:
在app/build.gradle的defaultConfig中添加:

defaultConfig {
    // ... 原有配置
    project.configurations.all {
        resolutionStrategy {
            force 'com.google.android.gms:play-services-gcm:17.0.0'
            force 'androidx.work:work-runtime-ktx:2.8.0'
        }
    }
}

方案3:添加AndroidX Core兼容库

添加AndroidX Core库,它会自动为旧代码处理PendingIntent的FLAG兼容:

implementation 'androidx.core:core:1.8.0'

验证

修改后重新编译应用,在Android 12+设备上测试,确认崩溃不再出现。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.29 22:08:11