Skip to content Skip to sidebar Skip to footer

Firebase Notifications When App Is Closed

I have implemented Firebase notification in my Android application. When my app is running, notification is displayed with my custom layout, but when application is not running, th

Solution 1:

Your problem is you using it with notification tray.

See this link

Messages with both notification and data payload, both background and foreground. In this case, the notification is delivered to the device’s system tray, and the data payload is delivered in the extras of the intent of your launcher Activity.

If you using {data:"something"}(data-message) with {notification:"something"}(display-message) while your app is in background the data payload will delivered to extras of the intent but not to the onMessageReceived() method.I assume you implement your code for showing notification, so when your app is in foreground onMessageReceived() is trigger and it display the desire notification you want but when it is not onMessageReceived() no get trigger instead android system will handle it with your notification payload. You just have remove {notification:"something"}(display-message/notification tray) from your server side code to always ensure onMessageReceived().

For anyone keep mention onMessageReceived() will always trigger no matter wether it is not foreground or background please visit this link

Solution 2:

There are two types of FCM messages

  1. Notification message
  2. Data message

Messages which get sent from Firebase console are Notification message. In order to get message in onMessageReceived() use Data Message. Use below code at server side to send Data Notification message

{
   "to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
   "data" : {
     "Nick" : "Mario",
     "body" : "great match!",
     "Room" : "PortugalVSDenmark"
   },
 }

Reference https://firebase.google.com/docs/cloud-messaging/concept-options#notifications_and_data_messages

Solution 3:

Your code should never be triggered when your app is closed or running in the background. See this:Firebase Notification

What you need to do is check how the app start up, tap the notification or tap the launcher icon. To do this, some data should be add into the notification then retrieve them in the first start activity in your app. If you can retrieve them successfully, it means your app is launched by tapping the notification, then you can do what you want to do.

Post a Comment for "Firebase Notifications When App Is Closed"