paho.mqtt.android icon indicating copy to clipboard operation
paho.mqtt.android copied to clipboard

Solution of PendingIntent FLAG_IMMUTABLE exception on Android version 31

Open huahua8893 opened this issue 2 years ago • 13 comments

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:

image

2.Modify class AlarmPingSender code like this: image

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

huahua8893 avatar Sep 25 '22 08:09 huahua8893

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.

ghost avatar Sep 28 '22 16:09 ghost

Great Solution thanks @huahua8893

exaland avatar Oct 14 '22 13:10 exaland

Worked very well with a few modifications.

gbenna avatar Oct 14 '22 18:10 gbenna

When will there be an official solution to this problem?

TAlkogolik avatar Nov 07 '22 00:11 TAlkogolik

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?

srlohr avatar Nov 27 '22 19:11 srlohr

Thnx bro @huahua8893. It works for me.

Housemates-app avatar Nov 29 '22 13:11 Housemates-app

But SCHEDULE_EXACT_ALARM permission is not allowed in android 12 ?

AshutoshGrab1990 avatar Dec 10 '22 08:12 AshutoshGrab1990

Follow steps to build .aar or .jar file here if needed: https://github.com/doorooful/paho.mqtt.android

doorooful avatar Dec 16 '22 06:12 doorooful

@doorooful could you generate a release distributing the jars in your repository with the fixes?

EzequielMessore avatar Feb 09 '23 18:02 EzequielMessore

Can not run MqttService in background

gelbertgel avatar Mar 06 '23 12:03 gelbertgel

Good decision! But I had some problems so I did the following:

  1. Installed jdk-1.8

  2. 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!

VolodymyrFVI avatar May 01 '23 09:05 VolodymyrFVI

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 ?

paulmathew avatar Sep 26 '23 17:09 paulmathew

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.

qilin02811 avatar Apr 23 '24 07:04 qilin02811