eclipse / paho.mqtt.android

MQTT Android
Other
2.88k stars 862 forks source link

Solution of PendingIntent FLAG_IMMUTABLE exception on Android version 31 #485

Open huahua8893 opened 1 year ago

huahua8893 commented 1 year ago

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

ghost commented 1 year ago

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.

exaland commented 1 year ago

Great Solution thanks @huahua8893

gbenna commented 1 year ago

Worked very well with a few modifications.

TAlkogolik commented 1 year ago

When will there be an official solution to this problem?

srlohr commented 1 year ago

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?

Housemates-app commented 1 year ago

Thnx bro @huahua8893. It works for me.

AshutoshGrab1990 commented 1 year ago

But SCHEDULE_EXACT_ALARM permission is not allowed in android 12 ?

doorooful commented 1 year ago

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

EzequielMessore commented 1 year ago

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

gelbertgel commented 1 year ago

Can not run MqttService in background

VolodymyrFVI commented 1 year ago

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!

paulmathew commented 9 months ago

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 ?

qilin02811 commented 2 months ago

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.