246

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?

Amir Hossein Ghasemi
  • 20,623
  • 10
  • 57
  • 53

22 Answers22

251

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

Mayur Gajra
  • 8,285
  • 6
  • 25
  • 41
  • 84
    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 }` – user924 Sep 30 '21 at 13:50
  • 145
    Again, this is another poor design decision by Google. Instead of setting a default behavior and logging a warning, it's crashing the applications... – doctorram Oct 07 '21 at 15:19
  • 1
    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); } ``` – Zarar Mahmud Apr 18 '22 at 10:47
  • This was only a warning until Android 11. In Android 12, they enforced it by crashing the app. – Peter Aug 29 '22 at 11:14
  • 2
    Yeah No issues here until just a few days ago -- crashes started occurring, I agree this was a poor decision by Google. – gattsbr Aug 31 '22 at 12:35
  • I have strange issue, when I add `PendingIntent.FLAG_NO_CREATE or PendingIntent.FLAG_IMMUTABLE` per reequipments, the result is true always... – Pavel Poley Nov 03 '22 at 20:25
  • why the operstor be or instead of and. Seem like and means both properties combined – Vikas Pandey Dec 26 '22 at 16:20
  • 1
    `or` ist a bitwise operator here, and its purpose exactly is to combine the values, @VikasPandey. – Michael Piefel Jan 12 '23 at 09:56
  • 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... – The incredible Jan Jan 27 '23 at 10:56
  • @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. – Rabia Abu Hanna Apr 21 '23 at 10:15
90

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.

Yosidroid
  • 2,053
  • 16
  • 15
  • Shouldn't it be version S instead of M? – StainlessSteelRat Jan 06 '22 at 22:56
  • 11
    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 ok – jcesarmobile Feb 04 '22 at 18:56
  • Thanks for this, solves my FLAG_MUTABLE requirement on an Intent that loads a URL dynamically from a ListView in a widget. – automaton Feb 15 '22 at 01:59
  • 1
    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. – mtotschnig Apr 30 '22 at 11:47
  • 2
    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? – ksparrow Jun 07 '22 at 23:25
  • 1
    @Yosidrod, idk why IDE still gives warning on else condition for missing PendingIntent flags. Min SDK version is 19. – Anukool srivastav Sep 04 '22 at 04:39
  • 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. – Anukool srivastav Sep 04 '22 at 04:40
  • I'm still getting this on 2.8-alpha. Work Manager version isn't the solution for me. – Marty Miller Sep 24 '22 at 01:23
  • @ksparrow You are right. Lint is starting to get on my nerves with this kind of thing... :/ – The incredible Jan Jan 30 '23 at 10:35
  • Where to add this piece of Code? I'm using ReactNative – Rabia Abu Hanna Apr 21 '23 at 10:16
  • @RabiaAbuHanna within onCreate method of activity or Fragment or Services – Yosidroid Apr 21 '23 at 13:56
83

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.

fattire
  • 6,823
  • 3
  • 25
  • 38
Noel
  • 7,350
  • 1
  • 36
  • 26
  • 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? – Adam Gardner Jul 01 '21 at 21:14
  • 5
    i tried with ```implementation "androidx.work:work-runtime:2.7.0-alpha05"```. it is working fine with java. – satheesh Aug 18 '21 at 21:12
  • 1
    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 https://android.googlesource.com/platform/frameworks/support/+/8656febfe4ffbd1de91299f9ae9b9c6147e723d6 ) – fattire Aug 21 '21 at 22:54
  • @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 https://android.googlesource.com/platform/frameworks/support/+/307b7ee8e79d34397a055b95b216f9394bd77597 see also https://maven.google.com/web/index.html?q=work-runtime#androidx.work:work-runtime-ktx – fattire Aug 21 '21 at 23:07
  • I don't use work library and don't have it it build.gradle – user924 Sep 30 '21 at 13:47
  • this is wrong answer, you have to add `PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT` for Android 12 as answered here https://stackoverflow.com/a/67046334/7767664 – user924 Sep 30 '21 at 13:48
  • 2
    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. – Khantahr Nov 03 '21 at 21:01
  • 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? – Jatin May 10 '22 at 11:23
  • 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 ‍♂️ – Big_Chair Jun 10 '22 at 16:27
  • 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. – zzazzles Jul 27 '22 at 16:07
  • Adding the implementation fixed the issue for me. React-native 0.63.3 – Mathias Bradiceanu Dec 15 '22 at 19:44
  • 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. – 68060 Apr 19 '23 at 10:49
28
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);
beginner
  • 2,366
  • 4
  • 29
  • 53
  • for me, best answer because remove warning "Missing PendingIntent mutability flag"; @SuppressLint("UnspecifiedImmutableFlag") not needed - Thanks – Cor Jan 20 '23 at 10:35
  • I cant actually see how this is an answer to the question – 68060 Apr 19 '23 at 10:01
25

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")        
}
Nikunj
  • 3,937
  • 19
  • 33
12

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'
    }
  }
}
Niklas
  • 23,674
  • 33
  • 131
  • 170
12

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.

Austin France
  • 2,381
  • 4
  • 25
  • 37
Kamran Manzoor
  • 121
  • 1
  • 8
11

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'
        }
    }
RRiVEN
  • 2,519
  • 2
  • 14
  • 13
  • 1
    This is fixed since version `play-services-ads:20.4.0` as the release notes says https://developers.google.com/admob/android/rel-notes – ilansas Dec 29 '21 at 13:53
9

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

Benoit Canonne
  • 445
  • 6
  • 6
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

j3App
  • 1,510
  • 1
  • 17
  • 26
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'
Rehan Khan
  • 1,031
  • 13
  • 10
  • 2
    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? – qkx Oct 05 '22 at 10:32
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)
            )
Vasily Kabunov
  • 6,511
  • 13
  • 49
  • 53
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....

Aleksandr
  • 51
  • 3
3

in my project this line worked

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

1

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'
Codeplayon
  • 429
  • 1
  • 6
  • 16
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.

Yogendra
  • 4,817
  • 1
  • 28
  • 21
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();
        });
icgoogo
  • 401
  • 5
  • 7
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.

Nihas Nizar
  • 619
  • 8
  • 15
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.

Stav Bodik
  • 2,018
  • 3
  • 18
  • 25
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
    }
}
Veniamin
  • 774
  • 5
  • 12
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.

canerkaseler
  • 6,204
  • 45
  • 38
0

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")
}
Tariq Mahmood
  • 147
  • 15