FMC push notification android background

459 views Asked by At

I'm new in push notifications and I have an issue, when app receive a notification and app is open the class "FCMIntentService extends FirebaseMessagingService" execute correctly, but when app is closed or in background don't execute this class. Why? and Which class process the notification?

1

There are 1 answers

0
Gautam Singh On

Firebase cloud Messaging has two concept one is 'notification' and second is 'data'.

enter image description here

In foreground notifications are send to onMessageReceived() method properly, but in case of background, notifications are sent into system tray see the image.

Solution: Use data field to send notifications from the server.

Don't use notification object to send the notifications from the server

{
  "message":{
    "token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
    "notification":{   // Don't use the notification           
      "title":"Portugal vs. Denmark",
      "body":"great match!"
    }
  }
}

Use data field to send the notification from the server it will work in background and foreground as well.

{
      "message":{
        "token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
        "data":{   // Use the data object to send the notification
          "title":"Portugal vs. Denmark",
          "body":"great match!"
        }
      }
    }