Firebase API is not sending push notifications whe

2019-01-08 01:18发布

问题:

I'm migrating from Parse to Firebase and facing a problem with our ios app. The Firebase API does not send push notifications to the ios app. This is what im sending to https://fcm.googleapis.com/fcm/send

{ 
 "to: "<registration token>",
 "priority": "high",
 "data": {
     "customId": "<my custom id>",
     "badge": 1,
     "sound": "cheering.caf",
    "alert": "New data is available"
  }
}

And the server is returning success

{
    "multicast_id":6917019914327105115,
    "success":1,
    "failure":0,
    "canonical_ids":0,
    "results":[{"message_id":"0:1468961966677585%f0f84693f9fd7ecd"}]
}

But the push is not delivered. If I sent the push using the Firebase Dashboard, the pushes are delivered, even if I targeting directly with the Token.

I saw another developer complaining in another Stackoverflow question Can't send push notifications using the server API

I tried their solution of adding the "priority": "high", it did not fixed the issue. But it gave me a clue: They are also using the dev/sandbox push certificate.

My suspicion is that the Dashboard can use the ios Development certificate, but the API can not. The problem only happen on the ios device, since the android app are getting the pushes through API.

Is anyone able to send pushes using the API and the development certificate?

回答1:

I got contacted by Firebase support and was able to find out what is wrong

My push payload was missing a notification object

{ 
 "to": "<registration token>",
 "priority": "high",
 "notification": {
    "title": "Your Title",
    "text": "Your Text"
  }
 "data": {
     "customId": "<my custom id>",
     "badge": 1,
     "sound": "cheering.caf",
    "alert": "New data is available"
  }
}

I hope that helps someone else



回答2:

The object you send to https://fcm.googleapis.com/fcm/send with Firebase API should look like this :

{
  "notification":{
    "title":"Notification title",  //Any value
    "body":"Notification body",  //Any value
    "sound":"default", //If you want notification sound
    "click_action":"<activity_name>",  //Must be present for Android
    "icon":"fcm_push_icon"  //White icon Android resource
  },
  "data":{
    "param1":"value1",  //Any data to be retrieved in the notification callback
    "param2":"value2"
  },
    "to":"/topics/topicExample", //Topic or single device
    "priority":"high", //If not set, notification won't be delivered on completely closed iOS app
    "restricted_package_name":"" //Optional. Set for application filtering
}

Please if your problem has been solved don't forget to mark it as such.