On Android, what is a PendingIntent, and when do you create one with FLAG_IMMUTABLE rather than FLAG_MUTABLE?
answer
- a token the system keeps
- runs with your app's identity
- unfilled fields can be filled in
- required flag since API 31
- inline reply needs mutable
basics
~20 sA PendingIntent is a system-held token that lets another app or the system fire your intent later as if your app sent it. Create it with FLAG_IMMUTABLE by default; use FLAG_MUTABLE only when the holder must fill in fields, as for inline replies.
solid answer
~50 sA `PendingIntent` wraps an `Intent` in a token the system keeps, so a notification, a home-screen widget, `AlarmManager` or another app can fire it later with your app's identity, even into components that are not exported. You create it with `PendingIntent.getActivity()`, `getService()` or `getBroadcast()`. Because the holder acts as you, mutability matters: a mutable `PendingIntent` lets the holder fill in fields the base intent left empty through `fillIn()`, so a malicious holder could point it at your private components. Apps targeting Android 12 (API 31) or higher must pass `FLAG_IMMUTABLE` or `FLAG_MUTABLE`, or creation throws `IllegalArgumentException`. Default to `FLAG_IMMUTABLE` with an explicit base intent; reserve `FLAG_MUTABLE` for features where the system adds data, such as notification direct replies, bubbles, location updates or repeating alarms, and still set the component. Add `FLAG_ONE_SHOT` when firing twice would repeat an action.
code
kotlin · 18 lines// Tap on the trip notification: nothing needs filling in, so immutable
val openTrip = Intent(context, TripActivity::class.java).putExtra("tripId", tripId)
val tapAction = PendingIntent.getActivity(
context,
tripId.hashCode(), // one token per trip
openTrip,
PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT,
)
// Inline reply to the driver: the system adds the typed text, so mutable,
// around an explicit intent to the app's own receiver
val reply = Intent(context, DriverReplyReceiver::class.java).putExtra("tripId", tripId)
val replyAction = PendingIntent.getBroadcast(
context,
tripId.hashCode(),
reply,
PendingIntent.FLAG_MUTABLE or PendingIntent.FLAG_UPDATE_CURRENT,
)go deeper
Recall that a PendingIntent lets the system or another app fire your intent later, the three factory methods, and that you pass FLAG_IMMUTABLE unless a feature needs mutable.
Explain fillIn() and why a mutable token around an implicit intent can be redirected, the API 31 rule and its IllegalArgumentException, and how requestCode and extras affect token identity.
Show you can audit an app's tokens: explicit base intents everywhere, FLAG_MUTABLE only for direct reply, bubbles, location or alarms, FLAG_ONE_SHOT for actions that must not repeat.
Treat every PendingIntent handed out as a delegated capability: decide which features may issue mutable ones and make immutable-and-explicit the reviewed default.
## What a PendingIntent is A **`PendingIntent`** is a reference to a token maintained by the Android system. It wraps an ordinary `Intent` plus the kind of component it targets, and it lets someone else trigger that intent later **with your app's identity and permissions**, whether or not your process is still alive. You never call `startActivity()` with it yourself; you create it with the factory method that matches the component type: - `PendingIntent.getActivity()` for an intent that starts an `Activity`; - `PendingIntent.getService()` for one that starts a `Service`; - `PendingIntent.getBroadcast()` for one that reaches a `BroadcastReceiver`. Typical holders are the system's `NotificationManager` (a tap or an action button), the home screen app running your widget, `AlarmManager` at a set time, and other apps you hand a callback to. Because the token carries your identity, the holder can even reach your **non-exported** components, which is exactly why it is useful and why it needs care. ## Why mutability is a security question A **mutable** `PendingIntent` lets its holder modify the inner intent following the rules of `Intent.fillIn()`: fields the base intent left **unset** can be filled in. If you wrap an implicit intent (no component, no package) in a mutable token, a malicious holder can fill in a component of its choosing and fire it with your identity, reaching activities or receivers you never exported. Two habits close that hole: 1. **Make the base intent explicit**: set the component (or at least the package and action) so there is nothing to redirect. 2. **Make the token immutable** with `FLAG_IMMUTABLE` (available since Android 6.0, API level 23) so no field can be filled in at all. ## The Android 12 rule If an app **targets Android 12 (API level 31) or higher**, every `PendingIntent` it creates must state its mutability with `FLAG_IMMUTABLE` or `FLAG_MUTABLE`. Creating one with neither throws an `IllegalArgumentException`, and Logcat explains that targeting S+ (version 31 and above) requires one of the two flags. There is no silent default in either direction, which forces each call site to make the choice. ## When FLAG_MUTABLE is genuinely needed The platform documentation names the features where the system itself must add data to your intent: - **Direct reply** in a notification, where the typed text is added to the intent's clip data; - conversations shown as **bubbles**, where the system applies its own launch flags; - notifications associated with **Android Auto** through `CarAppExtender`; - **location updates** requested with a `PendingIntent`, which receive location events as extras; - **repeating alarms** from `AlarmManager`, which add `EXTRA_ALARM_COUNT`. Even then, wrap an explicit intent, so the only thing a holder can change is the data the feature is meant to add. ## Other flags and how tokens are matched | Flag | Effect | |---|---| | `FLAG_IMMUTABLE` | holders cannot fill in any field | | `FLAG_MUTABLE` | holders may fill in unset fields through `fillIn()` | | `FLAG_ONE_SHOT` | the token can be sent once, which blocks replaying an action | | `FLAG_UPDATE_CURRENT` | if a matching token exists, keep it but replace its extras | The system treats two requests as the **same** token when their intents match (action, data, type, component, categories; extras are **not** compared) and their `requestCode` is equal. That is why notifications for different trips that differ only in an extra all open the same trip: give each one its own `requestCode` or distinct data, and use `FLAG_UPDATE_CURRENT` when the extras should be refreshed. ## What a review looks for - Mutable tokens created "to be safe" where the feature never needs the system to add data. - Notification or widget actions wrapping `Intent(action)` with no component or package. - Every notification using `requestCode` 0, so they collapse into one token. - Tokens that stay valid after the action no longer makes sense: a `PendingIntent` outlives your process, and `PendingIntent.cancel()` withdraws it. ## In a ride-hailing app The "driver arriving" notification opens the trip screen with an immutable, explicit `getActivity()` token, one `requestCode` per trip. Its "reply to driver" action needs a mutable token because the system adds the typed text, so it wraps an explicit intent to the app's own reply receiver. A payment callback token handed to another app is immutable and one-shot, and the receiver confirms the payment with the backend rather than trusting the callback alone.
- Three trip notifications differ only in a tripId extra, yet every tap opens the same trip. Why?The system matched the three requests to one `PendingIntent`: tokens are compared by their intent's action, data, type, component and categories plus the `requestCode`, and extras are ignored. Without `FLAG_UPDATE_CURRENT` the first token's extras stay. Give each trip its own `requestCode` or distinct data URI.
- When would you add FLAG_ONE_SHOT to a PendingIntent you give another app?When firing it twice would repeat something that must happen once, such as confirming a payment or redeeming a code. Without `FLAG_ONE_SHOT` a holder that keeps the token can send it again, which is a replay attack; with it the token works only once.
- Your app still supports API 22. How do you use FLAG_IMMUTABLE safely?`FLAG_IMMUTABLE` was added in Android 6.0 (API 23), so guard it with a `Build.VERSION.SDK_INT >= 23` check and keep the older creation path below that. On every version, set the component and package on the base intent so there is nothing to redirect.
A PendingIntent is like a signed cheque you hand to someone else to cash later. An immutable one has the payee and amount printed; a mutable one leaves fields blank, and whoever holds it can write in a payee you never chose.
- A PendingIntent runs with the identity of the app that fires it
- Without a mutability flag, Android 12 quietly creates an immutable PendingIntent
- FLAG_MUTABLE is safe as long as the PendingIntent is only used in notifications
- Extras distinguish two PendingIntents, so the requestCode can always be 0
- An implicit base intent is fine inside a mutable PendingIntent
Assumes Android 17 (API 37); the explicit FLAG_IMMUTABLE or FLAG_MUTABLE requirement applies to apps targeting Android 12 (API 31) or higher, and FLAG_IMMUTABLE exists from Android 6.0 (API 23).