YiYi
YiYi

Reputation: 25

pendingIntent mutability issues

i already know if sdk versions over S, must be set mutability when using PendingIntent

so i coded like this,

val pendingIntent = PendingIntent.getActivity(this, 0, intent, 
PendingIntent.FLAG_UPDATE or PendingIntent.FLAG_CANCEL_CURENT or PendingIntent.FLAG_IMMUTABLE)

that is only place where i using PendingIntent class and somtime, firebase analytics messages sending to me

Fatal Exception: java.lang.IllegalArgumentException MY_APP_PACKAGE: Targeting S+ (version 31 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.

i searched many questions for this message but there is no one got stucked like me

is there any reason to get this message in my code?

or any other problem can cause this error message?

Upvotes: 0

Views: 1442

Answers (1)

Sarah
Sarah

Reputation: 164

When targeting S+ (version 31 or later), it is an error report that either FLAG_IMMUTABLE or FLAG_MUTABLE must be specified when creating a PendingIntent. Google Docs strongly consider using FLAG_IMMUTABLE and tell you to use FLAG_MUTABLE only if some features rely on changeable PendingIntent.

[How to fix the error]

If you add the following library to the app-level build.gradle file and run app, the error will be solved. If your app uses AdMob 20.4.0 or earlier, you must add the following Task Manager dependencies.

implementation 'androidx.work:work-runtime:2.7.1'

However, if there is a code that is using PendingIntent on the source code, it is necessary to specify a flag value.

I'll give you my relevant code. This worked for me.

PendingIntent sPpendingIntent;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
    sPpendingIntent = PendingIntent.getActivity(context,
            NOTIFICATION_ID, sIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
}else {
    sPpendingIntent = PendingIntent.getActivity(context, NOTIFICATION_ID, sIntent, PendingIntent.FLAG_UPDATE_CURRENT);
}

Upvotes: 1

Related Questions