Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I've implemented Firebase in my app and I'm sending push with extra data. When my app is in foreground I'm handling data properly and showing my own notification, but I have a problem with fetching data, when Firebase shows Notification "automatically" when app was "homed" (not killed). According DOCS Activity should get new Intent with extras fulfilled with my values, instead app just get back to front, old state is restored.

Scenario:

  1. opening app, pressing Home
  2. sending push through Firebase console, Firebase is creating Notification WITHOUT calling onMessageReceived (according to table in docs, it should?)
  3. when user pick notification app will be brought to front in same state as was "homed", and Intent is fulfilled with "original" extras used for open Activity on top

I have logs in onCreate, onNewIntent, onResume (Activity) and in onMessageReceived (Service), only onResume is called, in which I'm printing extras like below:

if (getIntent().getExtras() != null) {
        for (String key : getIntent().getExtras().keySet()) {
            Object value = getIntent().getExtras().get(key);
            Log.d("Activity onResume", "Key: " + key + " Value: " + value);
        }
    }
like image 595
snachmsm Avatar asked Aug 24 '18 09:08

snachmsm


1 Answers

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.

like image 164
Gary Robottom Avatar answered Sep 18 '22 21:09

Gary Robottom