Search code examples
androidfirebasepush-notificationfirebase-cloud-messagingandroid-push-notification

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);
        }
    }

Solution

  • 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.