2

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?

Jonas
  • 534
  • 8
  • 16
  • Can you share your `onMessageReceived()` code? – Abdul Kawee May 26 '17 at 04:53
  • 1
    See my answer [here](https://stackoverflow.com/a/39505298/4625829). – AL. May 26 '17 at 04:55
  • @AbdulKawee In my onMessageReceived(), I just wrote a log and wrote something to the SharedPreferences. – Jonas May 26 '17 at 05:32
  • 1
    Lots of good info in @AL's answer regarding [Stopped State](https://stackoverflow.com/a/44053217/4815718). When you "close" the app, are you doing it in a way and on a device that might put the app into Stopped State? – Bob Snyder May 26 '17 at 06:00
  • @BobSnyder By closing, I mean I just swipe it horizontally away in the task view. Does it matter? – Jonas May 26 '17 at 06:14
  • 1
    On some phones, swiping from the task view ("recents") puts the app in Stopped state and it will not receive FCM messages until the user opens it. – Bob Snyder May 26 '17 at 06:27
  • @BobSnyder So this "stopped state" is different from the `onStop()` being called when an app moves out of the foreground? – Jonas May 26 '17 at 07:20
  • Yes, in this context stopped state means more than just being in the background with onStopped() called. – Bob Snyder May 26 '17 at 13:09

0 Answers0