paho.mqtt.android
paho.mqtt.android copied to clipboard
Solution of PendingIntent FLAG_IMMUTABLE exception on Android version 31
Like #480 #482 , I'm facing the same thing on Android version 31, but I can't change another repo to use in my project, so this is my solution:
1.Copy all "org.eclipse.paho:org.eclipse.paho.android.service" code one by one file into your project, keep original package path like this:

2.Modify class AlarmPingSender code like this:
3.Delete implementation 'org.eclipse.paho:org.eclipse.paho.android.service:1.1.1'
in build.gradle
4.Add implementation 'androidx.work:work-runtime:2.7.1'
to build.gradle
5.Add <uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM"/>
to manifest
6.Everything ready to go
Excellent solution, thanks. I had been looking for a solution for Android 12 for a while, reviewing my code, until I decided to see each library to see if they had the PendingIntent, and I found it.
Hint: The MqttAndroidClient class, you have to implement the methods that it lacks, by default and it will work fine.
Thanks. Make a pull request if you can, so it stays in the lib code.
Great Solution thanks @huahua8893
Worked very well with a few modifications.
When will there be an official solution to this problem?
Using just a proof-of-concept hack app created with Android Studio Dolphin 2021.3.1 Patch 1, new project by "Empty Compose Activity"; then adding the android mqtt client service source code, as is (i.e org/eclipse/paho/android/service) to the "sample app" here: POC/app/src/main/java/ I had to replace this: import android.support.v4.content.LocalBroadcastManager; with this: import androidx.localbroadcastmanager.content.LocalBroadcastManager; in 2 files: ...\org\eclipse\paho\android\service\MqttAndroidClient.java ...\org\eclipse\paho\android\service\MqttService.java
Without a doubt, this source should be a separate module and built accordingly with proper Gradle config, but I'm an newb and a totally lost puppy in this environment, so just scraping by to get a proof-of-concept running :-0
But most surprisingly, I had to stub out (or actually allow this incredibly intelligent Android Studio stub-out for me) 3 interface functions that were not overridden in MqttAndroidClient.java: @Override public boolean removeMessage(IMqttDeliveryToken token) throws MqttException { return false; }
@Override
public void reconnect() throws MqttException {
}
@Override
public int getInFlightMessageCount() {
return 0;
}
So now in addition to being lost, I am bewildered by how this could ever build before. Nevertheless, would someone please clarify whether or not the two functions which throw an exception should be overridden so as to actually just throw the exception instead of returning as they do above?
Thnx bro @huahua8893. It works for me.
But SCHEDULE_EXACT_ALARM permission is not allowed in android 12 ?
Follow steps to build .aar or .jar file here if needed: https://github.com/doorooful/paho.mqtt.android
@doorooful could you generate a release distributing the jars in your repository with the fixes?
Can not run MqttService in background
Good decision! But I had some problems so I did the following:
-
Installed jdk-1.8
-
setx -m JAVA_HOME "C:\Program Files\Java\jdk-1.8" To check I used: echo %JAVA_HOME% 3) sdk.dir=C:\Users\Volodymyr\AppData\Local\Android\Sdk and then: .\gradlew.bat clean org.eclipse.paho.android.service:assemble org.eclipse.paho.android.service:publish
I wonder why I got: org.eclipse.paho.android.service-1.1.1.jar org.eclipse.paho.client.mqttv3-1.1.0.jar?
But they work great. Thank you!
I have one small doubt here .. how to register the receiver for sdk 34 I used
ContextCompat.registerReceiver(
service,
alarmReceiver,
new IntentFilter(action),
ContextCompat.RECEIVER_EXPORTED
)
but returning null . any solution for that also ?
Using just a proof-of-concept hack app created with Android Studio Dolphin 2021.3.1 Patch 1, new project by "Empty Compose Activity"; then adding the android mqtt client service source code, as is (i.e org/eclipse/paho/android/service) to the "sample app" here: POC/app/src/main/java/ I had to replace this: import android.support.v4.content.LocalBroadcastManager; with this: import androidx.localbroadcastmanager.content.LocalBroadcastManager; in 2 files: ...\org\eclipse\paho\android\service\MqttAndroidClient.java ...\org\eclipse\paho\android\service\MqttService.java
Without a doubt, this source should be a separate module and built accordingly with proper Gradle config, but I'm an newb and a totally lost puppy in this environment, so just scraping by to get a proof-of-concept running :-0
But most surprisingly, I had to stub out (or actually allow this incredibly intelligent Android Studio stub-out for me) 3 interface functions that were not overridden in MqttAndroidClient.java: @OverRide public boolean removeMessage(IMqttDeliveryToken token) throws MqttException { return false; }
@Override public void reconnect() throws MqttException { } @Override public int getInFlightMessageCount() { return 0; }
So now in addition to being lost, I am bewildered by how this could ever build before. Nevertheless, would someone please clarify whether or not the two functions which throw an exception should be overridden so as to actually just throw the exception instead of returning as they do above?
now I face the same question, I had to implements the two method.