With FCM on Android, why does a backgrounded app never see a notification-plus-data message in onMessageReceived, and where does the data go?
answer
- the SDK decides before your code
- foreground means unlocked and visible
- the tap intent carries the pairs
- click_action, then link, then launcher
- read extras in onCreate and onNewIntent
basics
~20 sWhen the app is not in the foreground, the FCM SDK displays the notification part itself and stops, so onMessageReceived never runs; the data pairs are copied into the extras of the intent the notification opens when tapped.
solid answer
~40 sBefore calling my code, the FCM SDK checks whether the message has a notification part. If it does and the app is not in the foreground, which for the SDK also includes a locked screen, it builds and posts the notification itself and returns, so `onMessageReceived` is skipped. The data pairs are not lost: the SDK puts them into the extras of the tap's intent, which opens the activity matching `click_action` if one is set, otherwise the launcher activity. In a parcel-tracking app, the `parcelId` of a "courier nearby" alert is therefore read from `intent` in the activity's `onCreate`, and in `onNewIntent` if that activity is reused. In the foreground the SDK displays nothing and the whole message, notification and data, goes to `onMessageReceived`, so the app must post the alert itself.
code
kotlin · 27 linesimport android.content.Intent
import android.os.Bundle
import androidx.activity.ComponentActivity
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
openParcelFrom(intent)
}
// Called instead of onCreate when an existing instance is reused (for example singleTop).
override fun onNewIntent(intent: Intent) {
super.onNewIntent(intent)
openParcelFrom(intent)
}
private fun openParcelFrom(intent: Intent) {
// The FCM SDK copied the message's data pairs into the tap intent's extras.
val parcelId = intent.getStringExtra("parcelId") ?: return
showParcel(parcelId)
}
private fun showParcel(parcelId: String) {
// Navigate to the parcel screen.
}
}go deeper
Remember the trap: with the app in the background, a message that has a notification part is shown by the SDK and onMessageReceived does not run.
Walk through the SDK's order of work: foreground check, self-display, the tap intent built from click_action or the launcher, and the data copied into its extras.
Show the debugging story: the locked-screen case, onNewIntent for a reused activity, and a three-state test that would have caught a broken tap before release.
Decide which messages the team lets the SDK display and which must run app code, and make the server and app share one documented list of data keys.
## The decision the SDK makes before your code runs Take a parcel-tracking app whose server sends a **combined message** when a courier is close: a `notification` block ("Courier nearby", "Your parcel is two stops away") plus a `data` block with `parcelId`. Developers often put the navigation logic in `onMessageReceived` and are then surprised that, with the app in the background, tapping the alert opens the home screen and the handler never ran. The reason is the order of work inside `FirebaseMessagingService`. When a message arrives, the FCM SDK first checks whether it carries a notification part. If it does, the SDK asks one question: **is the app in the foreground?** - **Not in the foreground**: the SDK builds the notification from the payload and the manifest defaults, posts it to the system tray, and returns. `onMessageReceived` is **not** called for that message. - **In the foreground**: the SDK displays nothing and passes the whole message to `onMessageReceived`. The SDK's test for "foreground" is stricter than "the process is alive". It returns false when the **keyguard is showing** (screen off or lock screen), and otherwise checks that the app's process has foreground importance. So a message that arrives while the app is open but the phone is locked is also displayed by the SDK, not delivered to your code. One more display path exists. On Android 10 and later, with a recent Google Play services, high-priority notification messages sent through the HTTP v1 API may be **proxied**: Google Play services displays them on the app's behalf without starting the app at all (notification delegation, enabled by default and switchable with `setNotificationDelegationEnabled` or a per-message `proxy` setting). Either way the conclusion is the same: a notification part in the background is displayed for you, and your service does not see it. ## Where the data of a combined message goes The data pairs travel with the notification the SDK posted. When it builds the tap action, the SDK: 1. picks the target: an intent with the message's `click_action` as its action, restricted to your package, if one was sent; otherwise the message's link, if any; otherwise your app's **launcher intent**; 2. copies the message's data pairs into that intent's **extras**, leaving out its own reserved `google.c.` and `google.n.` keys; 3. adds `FLAG_ACTIVITY_CLEAR_TOP` and wraps it in a one-shot `PendingIntent`. When the user taps, the target activity starts with `parcelId` in `intent.extras`. If the user swipes the notification away instead, the app never sees that data: FCM gives the app no dismiss callback. Anything the app must know regardless of a tap, such as the latest parcel status, has to reach it another way, typically a sync the next time it opens. ## Reading the data in the parcel app - Read the extras in the target activity's `onCreate`, where `intent` is the tap intent. - Read them in `onNewIntent` as well when that activity can be reused rather than recreated, for example a `singleTop` launcher activity. - To land on a parcel screen directly, send `click_action` in the Android notification options and declare an intent filter with that action on the parcel activity. - In the foreground path, read the same keys from `message.data` in `onMessageReceived` and post the notification yourself, which on Android 13 and later needs the user's notification permission. If the app must run code for every message, whatever its state, the server sends a data-only message and the app builds the notification itself. When to choose that design belongs with push design for mobile apps; the FCM fact is only that a notification part hands display to the SDK in the background. ## How to recognise the bug | Symptom | Cause | |---|---| | Alert shows, tap opens the home screen, no parcel opened | Activity never reads `intent` extras | | Works while debugging with the app open, fails in the field | Foreground path tested only; background path is the SDK's | | Alert shows while the app is open but the phone is locked, handler logs nothing | Keyguard showing counts as not foreground | | Nothing shows while the user is inside the app | Foreground notification messages are not displayed by the SDK | A reliable test sends the same combined message three times: app in the foreground and unlocked, app in the background, and phone locked with the app open. Each run should either log in `onMessageReceived` or open the right screen from the extras. ## What to say in an interview - The SDK, not your service, decides who displays a notification message. - The SDK does not discard the data of a combined message; it waits in the tap intent until the user taps. - "Foreground" for the SDK means unlocked and at foreground importance.
- What happens to a notification message that arrives while the app is open but the phone is locked?The SDK's foreground check returns false while the keyguard is showing, so it posts the notification to the tray itself and `onMessageReceived` is not called. Only when the screen is unlocked and the app's process has foreground importance does the message go to `onMessageReceived` instead.
- How do you make the tap open the parcel screen instead of the launcher activity?Send `click_action` in the message's Android notification options and give the parcel activity an intent filter with that action. The SDK then builds the tap intent with that action, restricted to your package, and still copies the data pairs into its extras.
- Does high priority make a background notification message reach onMessageReceived?No. Priority affects how promptly FCM delivers the message and whether it may wake the device; it does not change who displays it. A notification part with the app in the background is displayed for the app, by the SDK or, when proxied, by Google Play services, and is never handed to `onMessageReceived`.
A courier with a parcel and a note: if you are home, the courier hands both to you and you decide what to do (onMessageReceived). If you are out, the courier leaves a card at the door and tapes the note to it; you only read the note when you pick up the card (the tap intent's extras).
- Put the tap navigation in onMessageReceived; it runs for every message
- The data part of a combined message is dropped when the app is backgrounded
- The SDK shows a notification message even while the user is inside the app
- High priority makes a background notification message reach onMessageReceived
- If the process is alive, the SDK treats the app as foreground even when locked