Ensuring onMessageReceived() is invoked when a FCM data message is sent even when App is closed

726 Views Asked by At

I am trying to send FCM data messages from my server. This is how a message request looks like:

POST https://fcm.googleapis.com/fcm/send
Content-Type:application/json
Authorization:key=AIzaSyZ-1u...0GBYzPu7Udno5aA

{ "data": {
    "score": "5x1",
    "time": "15:10"
  },
  "to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1..."
}

or, when I use PyFcm:

push_service = FCMNotification(api_key=MY_API_KEY)
data_payload = {"score": "5x1", "time": "15:10"}
push_service.notify_single_device(registration_id="bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...", data_message=data_payload)

I made sure it is a data message by leaving out the notification part. In this way, according to the FCM guide, onMessageReceived() of a FirebaseMessagingService will be invoked when an app is in the background or the foreground.

However when the app is closed, sometimes onMessageReceived() is invoked, and sometimes it is not. Is there any way for me to make sure it is always invoked, even when the app is closed?

0

There are 0 best solutions below