skip to content

In an Android app, a NotificationCompat direct-reply action shows its text field, but RemoteInput.getResultsFromIntent returns null in the receiver; why, and how do you fix it?

level: seniorimportance: should knowfreq 35%

answer

  1. the system writes into your intent
  2. who may fill in a PendingIntent
  3. Android 12 made you choose a flag
  4. one request code per conversation
  5. re-post the same ID afterwards

basics

~20 s

The reply action's PendingIntent was created with FLAG_IMMUTABLE, so the system cannot add the typed text to the intent it sends. Create that one PendingIntent with FLAG_MUTABLE on an explicit intent, and re-post the notification after handling the reply.

solid answer

~40 s

Direct reply works by the system filling the user's text into the action's intent, which `RemoteInput.getResultsFromIntent()` then reads. Since Android 12 (API 31) every `PendingIntent` must declare `FLAG_IMMUTABLE` or `FLAG_MUTABLE`, and teams often switch them all to immutable; an immutable one ignores the fill-in, so the receiver fires with no results. The reply action alone gets `FLAG_MUTABLE`, on an explicit intent to my receiver, because apps targeting Android 14 cannot create mutable implicit ones. I also give each conversation its own request code, or replies can land in the wrong thread. After handling the text I call `notify()` again with the same ID, appending the reply with `MessagingStyle.addMessage()` or `setRemoteInputHistory()`, which hides the reply UI and confirms it was received.

code

kotlin · 26 lines
kotlin
fun replyAction(context: Context, visitId: Int): NotificationCompat.Action {
    val remoteInput = RemoteInput.Builder(KEY_REPLY)
        .setLabel("Message the attendant")
        .build()
    val replyIntent = Intent(context, AttendantReplyReceiver::class.java)
        .putExtra(EXTRA_VISIT_ID, visitId)
    val pending = PendingIntent.getBroadcast(
        context, visitId, replyIntent,
        PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_MUTABLE
    )
    return NotificationCompat.Action.Builder(R.drawable.ic_reply, "Reply", pending)
        .addRemoteInput(remoteInput)
        .setSemanticAction(NotificationCompat.Action.SEMANTIC_ACTION_REPLY)
        .setShowsUserInterface(false)
        .build()
}

class AttendantReplyReceiver : BroadcastReceiver() {
    override fun onReceive(context: Context, intent: Intent) {
        val text = RemoteInput.getResultsFromIntent(intent)
            ?.getCharSequence(KEY_REPLY) ?: return
        val visitId = intent.getIntExtra(EXTRA_VISIT_ID, -1)
        enqueueSendToAttendant(context, visitId, text)
        repostVisitConversation(context, visitId, text) // same ID, reply appended
    }
}

go deeper

for a junior

Recall the parts of direct reply: a RemoteInput with a key, an action that carries it, and a receiver that reads the text with RemoteInput.getResultsFromIntent.

for a middle

Explain that the system writes the text into the action's intent, which is why the reply PendingIntent has to be mutable while other notification intents stay immutable.

for a senior

Trace a null result to an immutable PendingIntent from an Android 12 migration, check request codes per conversation, and re-post the notification with the same ID after handling.

for a principal

Make mutability an explicit, reviewed exception in the codebase: one helper builds reply PendingIntents, and every other notification intent stays immutable by default.

## How direct reply moves the text Direct reply, available since **Android 7.0 (API 24)**, lets the user type into a notification without opening the app. The pieces: 1. A `RemoteInput`, built with `RemoteInput.Builder(KEY_REPLY).setLabel(...)`. The key is how you find the text later. 2. A `NotificationCompat.Action` built with `Action.Builder(icon, title, pendingIntent).addRemoteInput(remoteInput)`, usually also `setSemanticAction(NotificationCompat.Action.SEMANTIC_ACTION_REPLY)` and `setShowsUserInterface(false)`. 3. A `PendingIntent` to a `BroadcastReceiver` (or a service or activity) in your app. When the user sends, the **system** adds the result to the intent before sending the `PendingIntent`; androidx's `RemoteInput` documents that the results are placed in the intent's `ClipData`, not in ordinary extras. The receiver reads them with `RemoteInput.getResultsFromIntent(intent)?.getCharSequence(KEY_REPLY)`; non-text input such as an image comes from `RemoteInput.getDataResultsFromIntent(intent, KEY_REPLY)`. ## Why the result is null The fill-in only works if the `PendingIntent` lets the sender modify its intent: | Flag | Can the system add the reply? | Note | |---|---|---| | `FLAG_IMMUTABLE` | **No**: the additional intent passed to `send()` is ignored | Recommended default for every other notification intent | | `FLAG_MUTABLE` | **Yes** | The platform names inline reply and bubbles as the cases that need it | | Neither | Creation throws `IllegalArgumentException` for apps targeting API 31+ | The message itself tells you to choose | The usual history: an app moving to target Android 12 hits the `IllegalArgumentException`, adds `FLAG_IMMUTABLE` to every `PendingIntent` in one sweep, and replies silently stop arriving. The reply UI still appears, because it comes from the `RemoteInput` on the action, but the receiver gets an intent without results. How `PendingIntent` mutability works in general, and why immutable is the safe default, is covered with Android intents; here the point is the one exception the notification API creates. ## The fix, in detail - Create **only the reply action's** `PendingIntent` with `FLAG_MUTABLE`; keep tap and other action intents immutable. - Make its intent **explicit**: set the receiver class. A mutable `PendingIntent` wrapping an implicit intent is blocked for apps targeting **Android 14 (API 34)**. - Use a **distinct request code per conversation** (or a distinct data URI). The direct reply guide warns that a reused `PendingIntent` can make the user reply to the wrong conversation, because extras such as the conversation ID are ignored when intents are compared. - Keep the receiver short; send the message to the server from background work. ## After the reply: update the notification The guide asks you to call `NotificationManagerCompat.notify()` again **with the same ID (and tag)** once the text is processed; that hides the direct-reply UI and confirms that the reply was received. - For a chat-like thread, rebuild with `NotificationCompat.MessagingStyle`, `addMessage()` the user's own text under the `Person` you passed to the style, and do not cancel the notification, so the user can reply again. `MessagingStyle.extractMessagingStyleFromNotification()` recovers the existing style from a posted notification. - For a one-off reply, `setRemoteInputHistory()` shows the sent text under the notification; the platform only shows that history on notifications with at least one `RemoteInput` action. ## MessagingStyle details that matter for replies - Since **Android 9 (API 28)** the guide asks for the `Person` class for optimal rendering of messages and avatars; build the style with `MessagingStyle(Person)` and add messages with `addMessage(text, timestamp, person)`, since the overload that takes a plain `CharSequence` sender is deprecated. - For a conversation with more than two people, `setConversationTitle()` keeps the notification from looking like a one-to-one chat with the latest sender. - `addHistoricMessage()` adds earlier messages as context for the reply without showing them as new. ## Testing the fix Reproduce the bug before fixing it: build the reply `PendingIntent` with `FLAG_IMMUTABLE`, send a reply from the shade and log the result of `getResultsFromIntent()`; it is `null`. Switch that one intent to `FLAG_MUTABLE` and the same reply arrives as a `CharSequence` under your key. Then post two notifications for two visits and reply to the older one, to prove the request codes keep them apart. ## A short checklist for review 1. `addRemoteInput()` on the action, with a stable result key. 2. `FLAG_MUTABLE` on that action's `PendingIntent`, explicit intent, unique request code. 3. Receiver reads `getResultsFromIntent()`, never `getStringExtra()`. 4. Notification re-posted with the same ID after handling. 5. `setAllowGeneratedReplies()` left on or turned off deliberately, since it controls whether the platform may suggest replies.

  • Replies sometimes reach the wrong visit's conversation; what is wrong with the PendingIntents?
    Every reply `PendingIntent` was created with the same request code and an intent that differs only in its extras. Extras are ignored when the system decides whether two pending intents are the same, so a later notification reused or overwrote the earlier one. Use the visit or conversation ID as the request code, or put it in the intent's data URI.
  • Why does the reply UI keep showing after the receiver handled the text?
    The notification was never re-posted. The direct reply guide asks you to call `NotificationManagerCompat.notify()` with the same ID and tag after processing, which hides the reply UI and confirms receipt. In a messaging notification, append the sent text with `MessagingStyle.addMessage()` and keep the notification rather than cancelling it.

  • FLAG_IMMUTABLE is fine for every notification PendingIntent, reply included
  • The typed reply arrives as a normal string extra on the intent
  • A missing reply means the RemoteInput was not added to the action
  • One shared request code is fine because the extras differ
  • Cancel the notification after a reply so the user sees it went through

Assumes API 37 and androidx.core 1.19.1; PendingIntent mutability must be declared from Android 12 (API 31), and mutable implicit PendingIntents are blocked for apps targeting Android 14 (API 34).