0

I use firebase push notification to device token. When app is opening or in foreground I can get notification well. But when app is kill or clear app on current task, I cannot receive notification send.

I have tried on onMessageReceived aleardy at first time work. but now it's not work when killed app.

  • Code Receive Notification:

    class MyFirebaseMessagingService : FirebaseMessagingService() {
          override fun onMessageReceived(p0: RemoteMessage) {
              val data = p0!!.data
              Log.e("AAAAAAAAAAAAA: ","data 
              111111111111111111111111111111111111111111111111:" + data["key1"])
         }
    }
    
  • Post Send notification data: Send to: https://fcm.googleapis.com/fcm/send

  • Data :

    {
    "to" : "token key",
    "data": {
           "key1" : "value1",
           "key2" : "value2",
           "other_key" : true
         }
    }
    

Result, for app is opening, I can receive data well, but when killed I cannot receive data.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
BacII
  • 39
  • 1
  • 6
  • possible duplicate https://stackoverflow.com/questions/24313539/push-notifications-when-app-is-closed – L2_Paver Oct 09 '19 at 02:18
  • Did you follow [this](https://firebase.google.com/docs/cloud-messaging/android/client)? According to it, you only need to handle notifications while in foreground. Background notifications comes for free if everything is configured properly. – Daniel Oct 09 '19 at 02:37

1 Answers1

0

If you're sending data to https://fcm.googleapis.com/fcm/send you're using the legacy http protocol as you can see here

This is not very clear in documentation.

In this protocol to receive the data message when app is in background or closed you should use this payload:

{
"to" : "token key",
"data": {
       "key1" : "value1",
       "key2" : "value2",
       "other_key" : true
     },
"priority" : 10,
"time_to_live" : 60
}

Test first with maximum priority (10) and then downgrade according t your needs. Also adjust time_to_live in seconds according to your needs.

devzeze
  • 302
  • 2
  • 9