AndroidAppsDeveloper tools5 min· Alexey Bulatnikov

Android push notification opens the app but not the right screen: why, and the fix

The push notification arrives, the customer taps it, and the app opens on its home screen instead of the order, the message or the offer the notification was about. In testing it worked. We lost an hour to this across three Android apps on the same day, and the cause was the same in all three. It comes from how Firebase Cloud Messaging treats a message when the app is not on screen.

Two kinds of message, two paths

A Firebase message can carry a notification block (title and text that Android shows), a data block (your own keys, such as the screen to open), or both. Most apps send both. What happens next depends on where the app is at that moment.

  • App open on screen: the message is handed to your FirebaseMessagingService in onMessageReceived. Your code builds the notification, and your code decides what a tap does.
  • App in the background or closed: Firebase shows the notification itself. onMessageReceived is not called at all.

That second path is the surprise. Your carefully built notification, with its tap action and its custom extra, is never created. The system creates its own.

What a tap does on the second path

A tap starts the app's launcher activity. If that activity uses the standard launch mode, Android creates a new instance of it. Every key of the data block is copied onto the launch Intent as a string extra. Nothing else is added. If your activity only looks for the extra that your own service would have set, it finds nothing and shows the home screen.

The fix: read both

In the launcher activity, look for your own extra first and for the raw data keys second, and do it in onCreate and in onNewIntent, because an activity that is already running receives the tap through onNewIntent.

private fun screenFromIntent(intent: Intent?): String? {
    if (intent == null) return null
    // set by our own service when the app was on screen
    intent.getStringExtra(EXTRA_SCREEN)?.let { return it }
    // copied from the data block by Firebase when the app was not
    return intent.getStringExtra("screen")
}

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    screenFromIntent(intent)?.let(::open)
}

override fun onNewIntent(intent: Intent) {
    super.onNewIntent(intent)
    setIntent(intent)
    screenFromIntent(intent)?.let(::open)
}

Here "screen" is the name of the key in the data block of the message. Use whatever your server sends.

The other fix: data-only messages

If the server sends only a data block, with no notification block, every message goes through onMessageReceived, whatever state the app is in, and your code builds every notification. That gives full control. The cost is that delivery to a sleeping phone then depends on the message priority, so send these with high priority and test on a phone that has been idle for a while.

Test it the right way

  • Press Home and send the message. That is the background case.
  • Swipe the app away from the recent apps list and send again. That is the closed case.
  • Do not use Force stop in Settings, and do not use adb shell am force-stop. Android puts a force-stopped app into a stopped state in which it receives no Firebase messages at all until someone opens it again. The test will fail for a reason your customers never meet.

Write the three states into your release checklist: on screen, in the background, closed. A notification feature is not tested until a tap has opened the right screen in all three.

If your app has this problem

It is a small fix once the cause is known, and it is worth making: a notification that opens the wrong screen costs the sale it was sent for. We build and repair iPhone and Android apps at fixed prices.

See app services and prices