From the documentation of 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 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.
In summary, you should add the FLAG_IMMUTABLE
flag to your PendingIntent when targeting API 31 or later, unless you need your PendingIntent to be mutable, in which case you need to use FLAG_MUTABLE
.
Because FLAG_IMMUTABLE
was introduced in API 23, you have to use FLAG_MUTABLE
as a fallback for lower versions.
val flag =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) PendingIntent.FLAG_IMMUTABLE
else PendingIntent.FLAG_MUTABLE
You can combine this with your existing intent flags, if you have any, using a bitwise or operation. For example:
val flags = flag or PendingIntent.FLAG_ONE_SHOT