Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solution of PendingIntent FLAG_IMMUTABLE exception on Android version 31 #485

Open
huahua8893 opened this issue Sep 25, 2022 · 13 comments
Open

Comments

@huahua8893
Copy link

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
Copy link

ghost commented Sep 28, 2022

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
Copy link

exaland commented Oct 14, 2022

Great Solution thanks @huahua8893

@gbenna
Copy link

gbenna commented Oct 14, 2022

Worked very well with a few modifications.

@TAlkogolik
Copy link

TAlkogolik commented Nov 7, 2022

When will there be an official solution to this problem?

@srlohr
Copy link

srlohr commented Nov 27, 2022

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
Copy link

Housemates-app commented Nov 29, 2022

Thnx bro @huahua8893. It works for me.

@AshutoshGrab1990
Copy link

But SCHEDULE_EXACT_ALARM permission is not allowed in android 12 ?

@doorooful
Copy link

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

@EzequielMessore
Copy link

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

@gelbertgel
Copy link

Can not run MqttService in background

@VolodymyrFVI
Copy link

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!

antonio-openroad added a commit to adafruit/Bluefruit_LE_Connect_Android_V2 that referenced this issue May 9, 2023
@paulmathew
Copy link

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
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests