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

升级targetSDK至31后AlarmPingSender触发PendingIntent标识缺失报错

问题根因

从错误堆栈可以直接定位:崩溃来自Eclipse Paho MQTT Android服务库的AlarmPingSender类,该类在创建PendingIntent时未指定Android 12(API 31+)强制要求的FLAG_IMMUTABLE或FLAG_MUTABLE标识。你之前修改自身项目代码不生效的原因是该逻辑封装在第三方依赖的编译产物中,不属于你项目的自有代码。

依赖溯源

你的项目依赖中,Qiscus即时通讯SDK默认依赖Paho MQTT库实现消息传输,OneSignal也可能存在间接引入的情况。你可以通过以下命令排查具体的引入来源:
在Android Studio终端执行:

  • Mac/Linux:./gradlew app:dependencies
  • Windows:gradlew app:dependencies
    执行后搜索org.eclipse.paho关键字即可看到是哪个上层依赖引入了未适配的Paho版本。
可用解决方案

方案1:升级上层依赖

如果是Qiscus引入的问题,直接将Qiscus SDK升级到适配Android 12的最新稳定版即可,官方后续版本已经修复了Paho库的PendingIntent适配问题。

方案2:自定义PingSender替换默认实现

官方Paho Android服务库未在正式版本中修复该问题,你可以自己实现自定义PingSender替换默认的AlarmPingSender:

  1. 复制原有AlarmPingSender的全部代码到你的项目中
  2. 修改创建PendingIntent的代码行,添加适配标识:
// 原有错误代码
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
// 修改后适配Android 12的代码
int flags = Build.VERSION.SDK_INT >= Build.VERSION_CODES.S ? PendingIntent.FLAG_IMMUTABLE : 0;
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, flags);
  1. 初始化MQTT客户端时,指定使用你自定义的PingSender实现,替换默认的AlarmPingSender。

方案3:全项目强制指定适配的依赖版本

在项目根目录的build.gradle中添加全局依赖强制配置,覆盖所有子依赖的Paho版本:

allprojects {
    configurations.all {
        resolutionStrategy {
            // 强制使用修复了Android 12适配问题的MQTT版本
            force 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.2.5'
        }
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.28 07:48:03