Where is FCM Intent with data delivered, when app is in background?

So I was looking for this answer and read the same. It has to be some where. I know this is a year old but they still haven't really made it clear. In the pendingActivity associated with the notification ( in my case it was MainActivity ) I added the following. Please note I was working through the Google Codelabs com.example.android.eggtimernotifications not that this was in the answers.

    override fun onResume() {
        super.onResume()
        intent?.run {
            val keys = this.extras?.keySet()
            if (!keys.isNullOrEmpty()) {
                keys.forEach { key ->
                    when (this.extras!![key]) {
                        is Long -> println("$key = ${this.getLongExtra(key, 0L)}")
                        is Int -> println("$key = ${this.getIntExtra(key, 0)}")
                        is String -> println("$key = ${this.getStringExtra(key)}")
                        is Boolean -> println("$key = ${this.getBooleanExtra(key, false)}")
                        else -> println("unkonwn Type")
                    }
                }
            }
        }
    }

This resulted in the following.

  • I/System.out: google.delivered_priority = high
  • I/System.out: google.sent_time = 1585284914411
  • I/System.out: google.ttl = 2419200
  • I/System.out: google.original_priority = high
  • I/System.out: cereal = Shreddies
  • I/System.out: from = /topics/breakfast
  • I/System.out: milk = 1 cup
  • I/System.out: sugar = none
  • I/System.out: google.message_id = 0:1585284914700957%f6ddf818f6ddf818
  • I/System.out: collapse_key = com.example.android.eggtimernotifications

The data keys were cereal, from, milk and sugar.


According to documentation Cloud messaging docs message can be delivered in 3 states:

  1. Notification
  2. Data
  3. Notification & Data

Background handling:

For 2. onMessageReceived will be called without UI and additional handling. Later you can show something custom.

When it comes to 3. android will show notification and hold data until there is user interaction. After click it's just resuming your existing activity. Your onNewIntent is not triggered because your launch mode is not FLAG_ACTIVITY_SINGLE_TOP.

You should also check your logcat output for: Google Play services out of date.


Messages with both notification and data payload, both background and foreground. In this case the data payload is delivered to extras of the intent of your launcher activity. If you want to get it on some other activity, you have to define click_action on the data payload. So get the intent extra in your launcher activity.

Edit:-

From the documentation :- When in the background, apps receive the notification payload in the notification tray, and only handle the data payload when the user taps on the notification.

So check the launcher activity's oncreate() method with getIntent() extras upon click of notification.