Can't send push notifications using the server

2019-01-15 02:01发布

问题:

Im using the new Firebase platform. Am trying to get a push notification sent by my app server and delivered to my iPhone.

I have the setup working where I manually send the message with the Firebase notifications area on the website, but when I try and send the message with a POST to https://fcm.googleapis.com/fcm/send I get no message being delivered to the device.

I'm sending the following (with auth headers)

{ "notification": {
    "title": "Portugal vs. Denmark",
    "text": "5 to 1"
  },
  "to" : "<registration token>"
  }

I'm getting a 200 response from the POST with the following body:

    {
  "multicast_id": 5511974093763495964,
  "success": 1,
  "failure": 0,
  "canonical_ids": 0,
  "results": [
    {
      "message_id": "0:1463685441784359%3ad254b53ad254b5"
    }
  ]
}

If I try and send to this device directly through the Firebase website it works, but the above form post doesn't. No idea where to go from here!

回答1:

On iOS the priority field seems mandatory.

{   
  "to": "cHPpZ_s14EA:APA91bG56znW...",
  "priority": "high",
  "notification" : {
    "body" : "hello!",
    "title": "afruz",
    "sound": "default"
  }
} 


回答2:

If the API returns you a message_id it means that your message has been correctly accepted and it will eventually be delivered to the device.

  • On Android, messages are delivered as soon as possible (provided that the device is connected of course).

  • On Apple devices, IF the application is closed or in background, the notification is sent through Apple infrastructure and can be delayed accordingly with Apple documentation.

To reduce the delay of the prioritymessages sent to Apple device you can use the priority parameter. More details: https://firebase.google.com/docs/cloud-messaging/concept-options#setting-the-priority-of-a-message



回答3:

I found the message field to be required, as well as the priority field to deliver a message with a POST.

message is not required (and labeled as optional) in the Firebase Console.



回答4:

I resolved adding the notification tag in request body without priority.

Here is a recap: