当针对S+ (版本31及以上)时,创建PendingIntent时需要指定FLAG_IMMUTABLE或FLAG_MUTABLE中的一个。关于AlarmPingSender。

57

问题

定位到S+(版本31及以上)需要在创建PendingIntent时指定FLAG_IMMUTABLE或FLAG_MUTABLE中的一个。我在将目标SDK更新为31后遇到了这个问题。错误始终出现在AlarmPingSender之后。但我不知道任何使用AlarmPingSender类的地方。


2021-10-31 10:43:04.990 17031-17341/com.app.mobile D/AlarmPingSender: Register alarmreceiver to MqttServiceMqttService.pingSender.com.app.mobile-2e24ccbde048f2e91635651784
2021-10-31 10:43:04.993 17031-17341/com.app.mobile E/AndroidRuntime: FATAL EXCEPTION: MQTT Rec: com.app.mobile-2e24ccbde048f2e91635651784
    Process: com.app.mobile, PID: 17031
    java.lang.IllegalArgumentException: com.app.mobile: 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:375)
        at android.app.PendingIntent.getBroadcastAsUser(PendingIntent.java:645)
        at android.app.PendingIntent.getBroadcast(PendingIntent.java:632)
        at org.eclipse.paho.android.service.AlarmPingSender.start(AlarmPingSender.java:76)
        at org.eclipse.paho.client.mqttv3.internal.ClientState.connected(ClientState.java:1150)
        at org.eclipse.paho.client.mqttv3.internal.ClientState.notifyReceivedAck(ClientState.java:987)
        at org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java:118)
        at java.lang.Thread.run(Thread.java:920)

我已完成的工作

  • 将WorkManager升级到2.7.0版本
  • 将AllProject设置为强制使用WorkManager 2.7.0
  • 更改所有现有PendingIntent以使用FLAG_IMMUTABLE标志
  • 有些旧代码仍在使用gcm,需要禁用它
  • 更新所有Firebase包(一些人说这是由于分析后续版本的原因)

使用的库

  • OneSignal
  • Qiscus
  • Firebase
  • WorkManager

2
错误信息显示你的错误是由org.eclipse.paho.android.service.AlarmPingSender.start(AlarmPingSender.java:76)引起的,这似乎与你正在使用的MQTT库有关。 - ianhanniballake
是的,我也这么认为,但我无法弄清楚使用了哪个MQTT库,我的猜测是Qiscus,但我尝试更新到最新版本,问题仍然存在。 - Joseph Sanjaya
你可以查看Gradle的构建扫描,以查看哪个依赖项引入了Eclipse Paho库。 - Lubos Mudrak
我认为这段代码https://github.com/eclipse/paho.mqtt.android/blob/master/org.eclipse.paho.android.service/src/main/java/org/eclipse/paho/android/service/AlarmPingSender.java(第76行)需要更新以符合新的Android限制。 https://github.com/eclipse/paho.mqtt.android/issues/465 - Cassio Landim
只需将Firebase Messaging库版本更新到最新版本(23或更高版本),并执行所需的更改,问题就会得到解决。但是,在应用程序代码中,确保将FLAG_IMMUTABLE添加到您的PendingIntent标志中。 - Harpreet
8个回答

44

请将以下代码添加到您的build.gradle(app)依赖项中。

dependencies {
  // For Java
  implementation 'androidx.work:work-runtime:2.7.1' 

  // For Kotlin
  implementation 'androidx.work:work-runtime-ktx:2.7.1'
}

对我来说,我原本使用的是2.6.1版本,尝试升级到最新的2.8.1版本,并将API级别升至33,但是出现了相同的错误。当我改回2.7.1版本时,问题得到解决。谢谢。 - Baskar PC
我正在使用Java,但对我来说不起作用。不是在2.7.1和2.8.1版本下 :( - undefined

22

可能的解决方案

将 Google Analytics 升级到 Firebase Analytics。希望这能解决您的问题。同时,升级您使用的所有库。

对于我来说,以下解决方案解决了问题。

在待定意图中添加 PendingIntent.FLAG_IMMUTABLE 标记。

这是一个示例 -

PendingIntent pendingIntent = PendingIntent.getActivity(this, alarmID, notificationIntent, PendingIntent.FLAG_IMMUTABLE);

欲了解更多信息,请参阅此链接 - https://developer.android.com/reference/android/app/PendingIntent#FLAG_IMMUTABLE


3
我已经尝试过这个,但问题仍然存在。 - Joseph Sanjaya
更新 Firebase 库解决了我的问题。 - Paweł
如何将Google Analytics升级到Firebase Analytics? - Benazir Sh
1
对于使用PendingIntent从第三方库调用方法的人们,需要注意一点……您可以在第四个参数中使用Kotlin中的“or”运算符或Java中的“|”运算符来发出多个标志。 - varun

9

我遇到了这个崩溃问题,但是没有第三方库。添加此导入并使用最新版本将解决此问题。

implementation 'androidx.work:work-runtime-ktx:2.7.0-beta01'

1
在我的情况下可行。我更改为:implementation("androidx.work:work-runtime-ktx:2.7.1")。 - Cube

4
除了其他人的答案,我还做了以下修改:
implementation 'com.google.firebase:firebase-messaging:20.0.0'

收件人:

implementation 'com.google.firebase:firebase-messaging:23.0.0'

现在它正在运作。


2

根据他人的答案,这可能是其中之一。或者通过移除此库:

implementation 'androidx.hilt:hilt-work:1.0.0'

1

需要避免在Android 12 API 31上崩溃

 implementation 'androidx.work:work-runtime-ktx:2.7.1'

1
提供的答案实现'androidx.work:work-runtime-ktx:2.7.1'仅适用于Kotlin。如果使用Java,您应该使用实现'androidx.work:work-runtime:2.7.1'。确保将此行放置在build.gradle(:app)文件中。完整条目应如下所示:dependencies {implementation 'androidx.work:work-runtime:2.7.1'}(适用于Java)或dependencies {implementation 'androidx.work:work-runtime-ktx:2.7.1'}(适用于Kotlin) - ONE

1

1
如果您在项目中使用WorkManager,请将其更新到最新版本。如果没有,请将所有Firebase库和Google库更新到最新版本。我遇到了同样的问题,我一个一个地更新库。然后我发现将com.google.android.gms:play-services-ads更新到最新版本解决了这个问题。

非常感谢你,伙计!你救了我的一天! - Sourav Rana

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接