handle push notification (FCM) while app is in background / kill - Android

时光毁灭记忆、已成空白 提交于 2019-12-06 10:12:38

Solution:

  1. If you are sending push notification from firebase console & your app is in background or killed then MyFirebaseMessagingService won't get called. (This is not mentioned in firebase documentation)

  2. If you are sending push notification from firebase console & your app is in foreground then MyFirebaseMessagingService will get called.

  3. If you are sending push notification from your server(I mean back-end i.e a website or some web based dashboard) & your app is in background or kill or in foreground then MyFirebaseMessagingService will get called.

Well, I landed here looking for a solution to a different question but I can answer your question half-way.

If you are sending notification with data payload, then you can check the intent for specific extras. For example if in your console (firebase) you have a key ('custom_message') with some value ("This is my message") as shown in image below

You can listen to Intent's extras in the your launch activity which can be a splashscreen or mainactivity.

 Intent intent = getIntent(); 
    if (intent.getStringExtras("custom_message") != null) {
      //go to specific activity when notification is opened
    }
   else{
     //open open default launch activity 
    }

Now, earlier I said I will half-answer your question. Why? This is because I do not know how you would achieve that when the notification has no data payload. Hoping this helps someone.

@Maulik Dodia info is also very useful. Appreciated

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!