Android paho mqtt 崩溃 Android 12 - Targeting S+(版本 31 及以上)需要 FLAG_IMMUTABLE 或 FLAG_MUTABLE 之一

问题描述 投票:0回答:6

我正在为 mqtt 服务使用“org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.2.5”,该应用程序在 android 12 设备上不断崩溃,并显示以下崩溃日志

java.lang.IllegalArgumentException: app id: 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:382)
        at android.app.PendingIntent.getBroadcastAsUser(PendingIntent.java:673)
        at android.app.PendingIntent.getBroadcast(PendingIntent.java:660)
        at org.eclipse.paho.android.service.AlarmPingSender.start(AlarmPingSender.java:76)
        at org.eclipse.paho.client.mqttv3.internal.ClientState.connected(ClientState.java:1214)
        at org.eclipse.paho.client.mqttv3.internal.ClientState.notifyReceivedAck(ClientState.java:1050)
        at org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java:151)

这是我正在使用的图书馆:

implementation 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.2.5'
implementation 'org.eclipse.paho:org.eclipse.paho.android.service:1.1.1'
java kotlin android-pendingintent paho android-12
6个回答
8
投票

Eclipse Paho MQTT 库 未针对 Android 12 待定 Intents 进行更新。在那之前,我们可以改用这个 MQTT 客户端。 我建议使用 gradle dependancies,而不是使用 myself's answer 中建议的 jar 文件。

在App Gradle中,注释这个eclipse服务依赖:

implementation 'org.eclipse.paho:org.eclipse.paho.android.service:1.1.1'

& 在 3.3.5 是当前版本的地方添加这些:

//new mqtt library that supports android 12
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'com.github.hannesa2:paho.mqtt.android:3.3.5'

不要删除eclipse客户端依赖,

implementation 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.2.5

对于导入,去掉这个eclipse import

import org.eclipse.paho.android.service.MqttAndroidClient;

& 用新的替换它:

import info.mqtt.android.service.MqttAndroidClient;

对于 MQTT android 客户端,像这样添加最后一个 Ack 参数

client = new MqttAndroidClient(context, serverURI, clientId, Ack.AUTO_ACK);

此外,如果您将 try catch 与

MqttException
一起使用,那么您可以对其进行评论,因为新库不需要相同的内容。


3
投票

如果您使用的是 MQTT 库,它们尚未针对 Android 12 进行更新。因此,当您使用 Android 12 作为目标版本时,它会在 PendingIntent 中引发错误。 作为临时解决方案,我找到了一个他们为与 Android 12 兼容而升级的库。 MQTT服务库

下载“serviceLibrary-release.aar”并将其添加到您的项目中。然后从 Gradle 中删除“'org.eclipse.paho:org.eclipse.paho.android.service:1.1.1”依赖项。在任何地方使用“import info.mqtt.android.service.MqttAndroidClient”。

如何导入aar文件

这解决了我的 MQTT 库问题。


0
投票

您需要将“PendingIntent.FLAG_ONE_SHOT”或“PendingIntent.FLAG_UPDATE_CURRENT”替换为“PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE”

例子:

alarmIntent = PendingIntent.getBroadcast(context, 0, new Intent(context, AutostartReceiver.class), PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);


0
投票

这是从paho.mqtt.android库修改而来的Java库。它已针对 Android 12

修复

0
投票

解决这个问题的最好方法是编辑源代码。为此,将所有库文件复制到您的项目中,然后打开 AlarmPingSender.java 文件并在第 75 行附近找到下面的代码:

pendingIntent = PendingIntent.getBroadcast(service, 0, new Intent(
                action), PendingIntent.FLAG_UPDATE_CURRENT)

用以下内容替换代码:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
        pendingIntent = PendingIntent.getBroadcast(service, 0, new Intent(
                action), PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
    } else {
        pendingIntent = PendingIntent.getBroadcast(service, 0, new Intent(
                action), PendingIntent.FLAG_UPDATE_CURRENT);

    }

现在用新文件替换您的“导入”。这应该可以解决问题。


-1
投票

更新firebase库:

实现平台('com.google.firebase:firebase-bom:29.1.0') 实施 'com.google.firebase:firebase-messaging'

并删除实施 'com.google.firebase:firebase-messaging:23.0.0'

© www.soinside.com 2019 - 2024. All rights reserved.