0

If the user click on the notification (which is sent by firebase cloudmessing), by default it opens my application (My main activity) and I can get the payload by this code in main activity

if (getIntent().getExtras() != null) {
    for (String key : getIntent().getExtras().keySet()) {
        String value = getIntent().getExtras().getString(key);
        Log.d(TAG, "Key: " + key + " Value: " + value);
        startOtherActivity();
    }
}

But I don't want to get the payload in main activity because my main activity is splash activity and it only lives for 3 seconds then it will jump to another activity. How I can get the payload in other activity? (not main activity)

Cœur
  • 37,241
  • 25
  • 195
  • 267
trna ha minh
  • 243
  • 2
  • 11

2 Answers2

0

The opening up of the main activity is the default behaviour.

The best way to handle notification is to handle them in background, parse the payload and then do accordingly. You can see how to handle the notification in background here.

But, if you just want to specify which activity to start in the sent notification itself : you can refer to this.

So, inside the onMessageReceived() once you parse the payload and you know what activity to start, simply create an Intent for that activity and start it.

zeekhuge
  • 1,594
  • 1
  • 13
  • 23
  • I can parse it to the object. But my main activity only lives for 3 seconds because it is splash activity. So I dont want to parse the object there – trna ha minh Aug 16 '17 at 12:48
0

To receive messages, use a service that extends FirebaseMessagingService. Your service should override the onMessageReceived and onDeletedMessages callbacks.

There you should handle what happens when the user clicks on the notification etc.

Besides that, you should probably change your android manifest settings. Your splash screen is not your main activity, if it is only shown for 3 seconds. Use categories to handle specific entry-points.

see this question for a good overview

fogx
  • 1,749
  • 2
  • 16
  • 38