How to resolve "Missing PendingIntent mutability flag" lint warning in android api 30+?
Asked Answered
H

23

258

As soon as I updated the target SDK to 30+ (Android R or later), a lint warning Missing PendingIntent mutability flag appeared on my PendingIntent.FLAG_UPDATE_CURRENT flag when I want to define PendingIntent.

How should I handle this lint with no effect on the app functionality?

Hosanna answered 11/4, 2021 at 13:34 Comment(1)
https://mcmap.net/q/111264/-android-12-pending-intent Can any one help me with this issueGerta
M
258

You can set your pending intent as

val updatedPendingIntent = PendingIntent.getActivity(
   applicationContext,
   NOTIFICATION_REQUEST_CODE,
   updatedIntent,
   PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT // setting the mutability flag 
)

According to the docs here: https://developer.android.com/about/versions/12/behavior-changes-12#pending-intent-mutability

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.

Choose your flag accordingly.

If you want to read more about this i would suggest that you read this great article here: https://medium.com/androiddevelopers/all-about-pendingintents-748c8eb8619

Morganatic answered 11/4, 2021 at 14:45 Comment(11)
For Android 21+: if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT } else { PendingIntent.FLAG_UPDATE_CURRENT }Ingress
Again, this is another poor design decision by Google. Instead of setting a default behavior and logging a warning, it's crashing the applications...Hauler
I have received the same error, upon seeing various solutions, I set mutability flags like this - ``` if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { pendingIntent = PendingIntent.getActivity (this, 0, intent, PendingIntent.FLAG_IMMUTABLE|PendingIntent.FLAG_UPDATE_CURRENT); } else { pendingIntent = PendingIntent.getActivity (this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); } ```Clarence
This was only a warning until Android 11. In Android 12, they enforced it by crashing the app.Unlay
Yeah No issues here until just a few days ago -- crashes started occurring, I agree this was a poor decision by Google.Effectuate
I have strange issue, when I add PendingIntent.FLAG_NO_CREATE or PendingIntent.FLAG_IMMUTABLE per reequipments, the result is true always...Eightfold
why the operstor be or instead of and. Seem like and means both properties combinedDepredate
or ist a bitwise operator here, and its purpose exactly is to combine the values, @VikasPandey.Ryle
So do I set the flag FLAG_IMMUTABLE if I don't care about this whole mutability thing? I have a few ONE_SHOT intents for Firebase messages and actually this is not used at all...Yabber
@Mayur Gajra - Can you explain how to fix for ReactNative exported project? I don't really identified where to put the PendingIntent, could be that there's other solution for ReactNative? Please advise.Pocketknife
I am trying to use PendingIntent.FLAG_NO_CREATE to check if there is already an existing alarm set or not. but right now this is throwing the error that I need to enforce to use Mutable or Immutable flags only and giving me exception. Now what is way to check if there is an existing alarm or not, if we cannot use Flag_No_CreateAspia
H
97

If you let your app to run in android 12, there is a new PendingIntent mutability flag. If you don't want your PendingIntent to be mutated, use

PendingIntent pendingIntent;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        pendingIntent = PendingIntent.getActivity(this,
                0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);

    }else {
        pendingIntent = PendingIntent.getActivity(this,
                0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    }

If you want your PendingIntent to be mutated use the following:

PendingIntent pendingIntent;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
        pendingIntent = PendingIntent.getActivity(this,
                0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_MUTABLE);

    }else {
        pendingIntent = PendingIntent.getActivity(this,
                0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    }

In Google documentation says, Strongly consider using FLAG_IMMUTABLE, only use FLAG_MUTABLE if some functionality depends on the PendingIntent being mutable. The change should be straightforward. Also, make sure you add the following work manager dependency if you are using AdMob 20.4.0 or lower in your app:

//Work Manager dependency
implementation 'androidx.work:work-runtime:2.7.1'

Note that currently work manager dependency version is 2.7.1. You can update the version to the latest one if you want.

Heliotaxis answered 4/12, 2021 at 16:0 Comment(11)
Shouldn't it be version S instead of M?Panhellenism
for the FLAG_MUTABLE yes, it should be S as it was added in SDK 31, FLAG_INMUTABLE was added in SDK 23, so M is okTong
Thanks for this, solves my FLAG_MUTABLE requirement on an Intent that loads a URL dynamically from a ListView in a widget.Dorella
It should be safe to use the newer flags on older API levels since the value is inlined. A flag that is not know on the older level, should not interfere with its bitwise checking of the flags it knows about.Boehmite
Unless I'm going crazy, this results in a "Missing PendingIntent mutability flag" linter warning in the else block. We can @SuppressLint("UnspecifiedImmutableFlag") but it seems like the linter should be aware of the code branch, no? Is there a better solution for fixing this warning?Tertias
@Yosidrod, idk why IDE still gives warning on else condition for missing PendingIntent flags. Min SDK version is 19.Moneymaker
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { pendingIntentKill = PendingIntent.getBroadcast(this.getApplicationContext(), randomNumberTap, intentKill, PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT); } else { pendingIntentKill = PendingIntent.getBroadcast(this.getApplicationContext(), randomNumberTap, intentKill, PendingIntent.FLAG_UPDATE_CURRENT); } this is code i have currently.Moneymaker
I'm still getting this on 2.8-alpha. Work Manager version isn't the solution for me.Chungchungking
@Tertias You are right. Lint is starting to get on my nerves with this kind of thing... :/Yabber
Where to add this piece of Code? I'm using ReactNativePocketknife
@RabiaAbuHanna within onCreate method of activity or Fragment or ServicesHeliotaxis
V
84

If you're not using the latest version of WorkManager, you'll see this issue. It's been fixed in version 2.7.0-alpha02:

Make PendingIntent mutability explicit, to fix a crash when targeting Android 12

Keep in mind that 2.7.0-alpha02 is only compatible with the Android 12 Developer Preview 1 SDK. So you may want to wait until it hits the beta or RC.

Update April 21, 2021 -- Adding to this answer for anyone googling the issue, the bug you may encounter may look something like this:

java.lang.IllegalArgumentException: com.myapp.myapp: Targeting S+ (version 10000 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:386)
        at android.app.PendingIntent.getBroadcastAsUser(PendingIntent.java:657)
        at android.app.PendingIntent.getBroadcast(PendingIntent.java:644)
        at androidx.work.impl.utils.ForceStopRunnable.getPendingIntent(ForceStopRunnable.java:174)
        at androidx.work.impl.utils.ForceStopRunnable.isForceStopped(ForceStopRunnable.java:108)
        at androidx.work.impl.utils.ForceStopRunnable.run(ForceStopRunnable.java:86)
        at androidx.work.impl.utils.SerialExecutor$Task.run(SerialExecutor.java:75)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at java.lang.Thread.run(Thread.java:920)

You do not have to be actually directly using WorkManager in your app to see this crash.

The solution, as outlined here, is to add a dependency to your build.gradle file for Android 12 builds:

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

Note that this dependency is different whether you are using Java only, Kotlin + coroutines, RxJava2, GCMNetworkManager, etc. So be sure to check the dox above.

Obviously replace the version number above with the latest. And as mentioned, it is NOT compatible with pre-android-13 builds.

Valval answered 20/4, 2021 at 15:6 Comment(12)
Is there any updates on this? I tried this using -alpha04 and still receiving the error. I only have 1 PendingIntent for my application and not able move forward from this crash. Can third party libraries cause this crash?Sb
i tried with implementation "androidx.work:work-runtime:2.7.0-alpha05". it is working fine with java.Sino
Adam-- it's very likely then that it's another library causing the crash. You can see the full tree of dependencies for your project with "./gradlew :app:dependencies" (in Linux) For example, play-services-ads-lite:20.2.0 depends on androidx.work:work-runtime:2.1.0 and I think that's where lies this issue ( fixed in android.googlesource.com/platform/frameworks/support/+/… )Spann
@Satheesh, yep-- they are as far as 2.7.0.beta-01 in the source but it doesn't seem to be available in the repository for generating builds yet android.googlesource.com/platform/frameworks/support/+/… see also maven.google.com/web/…Spann
I don't use work library and don't have it it build.gradleIngress
this is wrong answer, you have to add PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT for Android 12 as answered here https://mcmap.net/q/109324/-how-to-resolve-quot-missing-pendingintent-mutability-flag-quot-lint-warning-in-android-api-30Ingress
This is not the wrong answer, it just might not be the complete answer. If you are having this crash, but you are using the correct flags in your PendingIntent then this is the solution you need.Ribera
I'm using a androidx.hilt:hilt-work:1.0.0 worker and its crashes in some of samsung S20 device. Any solution for me?Vortumnus
This is so silly. I want to cancel older alarms on the user's device after an update, so I need to re-create the exact old PendingIntent with FLAG_UPDATE_CURRENT. But now it crashes just upon creating the object, so I have no way of doing that on newer devices 🤦‍♂️Pentlandite
Thank you for this! I've added flags to all the PendingIntents in my code and still kept getting this crash. And the crash trace didn't give any indication of where it was originating from.Quartic
Adding the implementation fixed the issue for me. React-native 0.63.3Luben
implementation 'androidx.work:work-runtime-ktx:2.7.0-alpha05' works fine yes i did not even need to edit my java, just add that line to the gradle file.Vaasa
P
29
final int flag =  Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ? PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE : PendingIntent.FLAG_UPDATE_CURRENT;
PendingIntent pendingIntent = PendingIntent.getActivity(context, PENDING_INTENT_REQUEST_CODE, notificationIntent, flag);
Prytaneum answered 2/3, 2022 at 11:34 Comment(2)
for me, best answer because remove warning "Missing PendingIntent mutability flag"; @SuppressLint("UnspecifiedImmutableFlag") not needed - ThanksIncus
I cant actually see how this is an answer to the questionVaasa
M
26

If your app is targeting Android 12 (targetSdkVersion = 31), and uses an older version of the WorkManager directly OR by any of the third-party libraries then you require to update it to the latest to resolve it.

dependencies {
    val work_version = "2.8.1"

    // (Java only)
    implementation("androidx.work:work-runtime:$work_version")

    // Kotlin + coroutines
    implementation("androidx.work:work-runtime-ktx:$work_version")

    // optional - RxJava2 support
    implementation("androidx.work:work-rxjava2:$work_version")        
}
Multifoil answered 14/2, 2022 at 7:20 Comment(2)
This didn't work for me. I'm on 2.8 alpha and am still getting the error.Chungchungking
multiDexEnabled trueVulvitis
T
15

You can update the pending intent like:

val updatedPendingIntent = PendingIntent.getActivity(
   context,
   NOTIFICATION_REQUEST_CODE,
   updatedIntent,
   PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT 
)

You can add PendingIntent.FLAG_IMMUTABLE along with | sign and it will work.

Turpeth answered 14/10, 2022 at 12:24 Comment(5)
the | is not a kotlin syntax, you need to use orAmaleta
Yes, you are right, it's not the exact code it's a hint.Turpeth
@Amaleta Where is "Kotlin" mentioned in the question? "|" is "or" in Java...Yabber
(: unless java added val to it language, this is clearly kotlin syntax...Amaleta
Intent intent = new Intent(context, MainActivity.class); PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent , PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_CANCEL_CURRENT|PendingIntent.FLAG_IMMUTABLE); Here is the java code. :)Turpeth
K
13

If you using Java and ADMOB you experience the PendingIntent Error wtih SDK S or Android 12. Here is a fix so ADMOB uses the correct work-runtime.

implementation 'com.google.android.gms:play-services-ads:19.5.0'
    constraints {
        implementation('androidx.work:work-runtime:2.7.0-alpha05') {
            because 'previous versions have a bug impacting this application'
        }
    }
Kleist answered 24/10, 2021 at 1:15 Comment(1)
This is fixed since version play-services-ads:20.4.0 as the release notes says developers.google.com/admob/android/rel-notesSponsor
P
13

In my case it was also by third party libraries which were using old WorkManager versions, to force the new Android Work version on all dependencies use this in your root build.gradle file:

allproject {
  project.configurations.all {
    resolutionStrategy {
      force 'androidx.work:work-runtime:2.7.0'
    }
  }
}
Popup answered 26/10, 2021 at 12:9 Comment(0)
B
9

This crash is resolved with : implementation 'androidx.work:work-runtime:2.7.1'

Barthol answered 7/12, 2021 at 9:0 Comment(1)
What crash? The question is about a warning.Yabber
N
8

As I had four different PendingIntents in my code, I started by adding FLAG_IMMUTABLE to all of them. However the problem remained. After spending a lot of time analyzing my 4 intents, it dawned on me that the problem might come from one of my libraries.

In build.gradle, libraries are normally highlighted when old, but this is not the case for the Firebase BOM.

I had:

implementation platform('com.google.firebase:firebase-bom:26.1.1')

It turned out this was very old. After updating to

implementation platform('com.google.firebase:firebase-bom:29.0.4')

all was fine. No more FLAG_IMMUTABLE errors

Nevertheless answered 2/2, 2022 at 15:38 Comment(0)
H
8

If you let your app to run in android 12, there is a new PendingIntent mutability flag. If you don't want your PendingIntent to be muted, use

Java

PendingIntent updatedPendingIntent = PendingIntent.getActivity(
   applicationContext,
   NOTIFICATION_REQUEST_CODE,
   updatedIntent,
   PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT // setting the mutability flag 
)

Kotlin

val updatedPendingIntent = PendingIntent.getActivity(
   applicationContext,
   NOTIFICATION_REQUEST_CODE,
   updatedIntent,
   PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT // setting the mutability flag 
)

If you want your PendingIntent to be muted use the following:

Java

PendingIntent updatedPendingIntent = PendingIntent.getActivity(
   applicationContext,
   NOTIFICATION_REQUEST_CODE,
   updatedIntent,
   PendingIntent.FLAG_MUTABLE | PendingIntent.FLAG_UPDATE_CURRENT // setting the mutability flag 
)

Kotlin

val updatedPendingIntent = PendingIntent.getActivity(
   applicationContext,
   NOTIFICATION_REQUEST_CODE,
   updatedIntent,
   PendingIntent.FLAG_MUTABLE or PendingIntent.FLAG_UPDATE_CURRENT // setting the mutability flag 
)

At the Last implement this Dependecy

//Work Manager dependency
implementation 'androidx.work:work-runtime:2.7.1'
Harijan answered 17/3, 2022 at 10:21 Comment(1)
logically, should not we use MUTABLE flag if we were previously using flag UPDATE_CURRENT? We clearly wanted to have an updateable/mutable pending intent. And accordingly, if we were using flag CANCEL_CURRENT, shouldn't use IMMUTABLE? I do not understand why to use UPDATE_CURRENT with IMMUTABLE flag, aren't they actually contradicting each other?Myramyrah
A
5

I had crashes like Fatal Exception: java.lang.IllegalArgumentException. Not posted. PendingIntents attached to actions with remote inputs must be mutable.

I wrote this util method, which allows sending mutability as a param. Sometimes its required to get mutable flags, for example for reply actions in notifications.

private fun getPendingIntentFlags(isMutable: Boolean = false) =
    when {
        isMutable && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S ->
            PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_MUTABLE

        !isMutable && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ->
            PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE

        else -> PendingIntent.FLAG_UPDATE_CURRENT
    }

Usage example:

val quickReplyPendingIntent = PendingIntent.getBroadcast(
                context, notificationId, replyIntent,
                getPendingIntentFlags(true)
            )
Asel answered 10/3, 2022 at 13:55 Comment(0)
P
4

This is issue with Work library. Even the latest version is affected 2.7.0-alpha04

https://issuetracker.google.com/issues/194108978

As temporary workaround - comment out including "work" dependency in gradle and remove using that class through the project. At least in this way you may run app normally and work on another features and areas....

Pirozzo answered 20/7, 2021 at 16:11 Comment(0)
W
3

in my project this line worked

PendingIntent pendingIntent = PendingIntent.getActivity(this,0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);

Wolk answered 29/4, 2022 at 13:8 Comment(0)
I
2

This issue comes when you upgrade your project and target android version 12, Android Run App On Android 12. The solution is used you can Update your All Pending Intern

Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent enter image description here

Used Below Code

 PendingIntent pendingIntent = null;
        if (Build.VERSION.SDK_INT>= Build.VERSION_CODES.S){
             pendingIntent = stackBuilder.getPendingIntent(1, PendingIntent.FLAG_MUTABLE);

        }else {
             pendingIntent = stackBuilder.getPendingIntent(1, PendingIntent.FLAG_UPDATE_CURRENT);

        }

And Also implement this Dependency Work if you are using a receiver in your project

//Work Manager dependency
implementation 'androidx.work:work-runtime:2.7.1'
Ivatts answered 24/5, 2022 at 13:36 Comment(2)
HI, I get this error on Android 12 but only when the app is backgrounded. I am setting FLAG_IMMUTABLE but it still crashes. Any ideas? Thank you.Dehumidify
Same here. Only crashes when in the background.Chungchungking
O
1

here is my use case to move from 30 to 33 in KOTLIN.

1. Add media dependency

implementation "androidx.media:media:1.4.1"

2. Update work manager

 implementation "androidx.work:work-runtime-ktx:2.7.0"

3. Update Immutable

fun getImmutableFlag() = if(isAndroidAPI31())  PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT else 0

fun isAndroidAPI31() = android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S

private fun createOpenAppIntent(context: Context): PendingIntent {
        val intent = Intent(context, MainActivity::class.java).apply {
            flags = Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP
        }
        return PendingIntent.getActivity(context, 0, intent, getImmutableFlag())
    }

4. Add exported tag in manifest if not added in all activity, services, provider, receiver

android:exported="true"

Hope this will work, have a good day.

Oviposit answered 2/10, 2022 at 4:8 Comment(2)
Just wondering, what does the media dependency do?Tushy
@TerryWindwalker Yes, Seems like no role but its not working without this so I have no option so far.Oviposit
C
1

i wanna share a bit from my case. i changed the flag to FLAG_IMMUTABLE but still got the error only when the app is on background. i got it solved from here : https://github.com/firebase/firebase-android-sdk/issues/3115

the root cause is because i retrieve the FCM token in the deprecated way :

FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener(activity,  new OnSuccessListener<InstanceIdResult>() {
        @Override
        public void onSuccess(InstanceIdResult instanceIdResult) {
            String newToken = instanceIdResult.getToken();
       
        }
    });

then i update the dependency :

FirebaseMessaging.getInstance().getToken().addOnCompleteListener(task -> {
            String newToken = task.getResult();
        });
Chiropteran answered 31/10, 2022 at 14:46 Comment(0)
V
1

Add these lines:

defaultConfig {
    
    multiDexEnabled true
}

and also

dependencies {
def work_version = "2.8.1"
// (Java only)
implementation("androidx.work:work-runtime:$work_version")
// Kotlin + coroutines
implementation("androidx.work:work-runtime-ktx:$work_version")
// optional - RxJava2 support
implementation("androidx.work:work-rxjava2:$work_version")
}
Vulvitis answered 25/8, 2023 at 7:26 Comment(2)
where to add that ?Cerellia
In build.gradle (module: app) fileVulvitis
E
0

I updated my work-runtime-ktx version to 2.7.1

After the above change i got into another error

java.lang.IllegalStateException: SimpleTypeImpl should not be created for error type: ErrorScope{Error scope for class <ERROR CLASS> with arguments: org.jetbrains.kotlin.types.IndexedParametersSubstitution@14ac19e7}

Look how i solved the above error by updating kotlin-gradle-plugin version here.

Epos answered 20/1, 2022 at 7:16 Comment(0)
T
0

From :

https://developer.android.com/reference/android/app/PendingIntent#FLAG_MUTABLE

"Up until Build.VERSION_CODES.R, PendingIntents are assumed to be mutable by default, unless FLAG_IMMUTABLE is set. Starting with Build.VERSION_CODES.S, it will be required to explicitly specify the mutability of PendingIntents on creation with either (@link #FLAG_IMMUTABLE} or FLAG_MUTABLE. It is strongly recommended to use FLAG_IMMUTABLE when creating a PendingIntent. FLAG_MUTABLE should only be used when some functionality relies on modifying the underlying intent, e.g. any PendingIntent that needs to be used with inline reply or bubbles."

To keep same behavior like to day change anything to "PendingIntent.FLAG_MUTABLE | anything "

When creating/retrieving a pending intent/ activity, services, provider, receiver

Places to look for :

PendingIntent.getBroadcast(...

.getPendingIntent(...

PendingIntent.getService

PendingIntent.getActivity

Also if your app using androidx.work make sure to upgrade to atleast :

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

There was a bug they fixed in alpha02 related to all this changes in SDK 12.

Teerell answered 5/12, 2022 at 18:40 Comment(0)
R
0

I created the PendingIntentCompat.kt that abstracts PendingIntent logic in a separate class.

object PendingIntentCompat {

    @JvmStatic
    @JvmOverloads
    fun getActivity(
        context: Context,
        requestCode: Int,
        intent: Intent,
        flags: Int,
        isMutable: Boolean = false
    ): PendingIntent {
        return PendingIntent.getActivity(
            context,
            requestCode,
            intent,
            addMutabilityFlags(isMutable, flags)
        )
    }

    @JvmStatic
    @JvmOverloads
    fun getService(
        context: Context,
        requestCode: Int,
        intent: Intent,
        flags: Int,
        isMutable: Boolean = false
    ): PendingIntent {
        return PendingIntent.getService(
            context,
            requestCode,
            intent,
            addMutabilityFlags(isMutable, flags)
        )
    }

    @JvmStatic
    @JvmOverloads
    @RequiresApi(Build.VERSION_CODES.O)
    fun getForegroundService(
        context: Context,
        requestCode: Int,
        intent: Intent,
        flags: Int,
        isMutable: Boolean = false
    ): PendingIntent {
        return PendingIntent.getForegroundService(
            context,
            requestCode,
            intent,
            addMutabilityFlags(isMutable, flags)
        )
    }

    /**
     * https://developer.android.com/about/versions/12/behavior-changes-12#pending-intent-mutability
     */
    private fun addMutabilityFlags(isMutable: Boolean, flags: Int): Int {
        var updatedFlags = flags

        if (isMutable) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
                updatedFlags = flags or PendingIntent.FLAG_MUTABLE
            }
        } else {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                updatedFlags = flags or PendingIntent.FLAG_IMMUTABLE
            }
        }
        return updatedFlags
    }
}
Roma answered 19/1, 2023 at 18:4 Comment(0)
G
0

If you have this problem about Notification, DeepLink and Navigation, do not forget to update your navigation dependency version:

implementation 'androidx.navigation:navigation-ui-ktx:2.5.3'
implementation 'androidx.navigation:navigation-fragment-ktx:2.5.3'

I had this problem because of old version navigation dependency.

Gastrotomy answered 1/3, 2023 at 18:4 Comment(0)
W
-1

Update Firebase to latest versión:

implementation 'com.google.firebase:firebase-messaging:23.4.0'
Whodunit answered 30/1 at 9:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.