Can't send push notifications using the server API
Asked Answered
T

4

9

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!

Trypsin answered 19/5, 2016 at 19:28 Comment(6)
When you manually send and then send using the Firebase website are they both using the same Apple push environment? There is a dev and a prod environment, check the one you are using via Firebase is the same as the one you used manually.Checkerboard
I don't see anywhere in the docs mentioning this? Sending from the site doesn't give me a dev or prod option and neither does the rest API? I've only uploaded a dev apns certificate so far...Trypsin
I'm not familiar with Firebase but the underlying fundaments of Apple push are invariable. The prod environment is not compatible with the dev certificate and vice versa.The Apple dev environment is called the sandbox. Its url is ssl://gateway.sandbox.push.apple.com:2195. If you verify you're using the Apple sandbox environment when using Firebase.Checkerboard
Also how are you obtaining the Apple push token from the device? It can and does change so its essential to retrieve it every time the app launches. Using a stale push token will result in failed pushes.Checkerboard
If I try and send a notification through the website to a specific token, it gets delivered. If I try and send it straight after that through the API it is never received. There's no way to specify whether it is to be sent via the dev or prod apple services tho?Trypsin
There has to be a way of the switching the api between dev and prod, the two are simply just not compatible. Unless the API is intended only for use with production - have you tried uploading a production certificate for use with the API?Checkerboard
R
34

On iOS the priority field seems mandatory.

{   
  "to": "cHPpZ_s14EA:APA91bG56znW...",
  "priority": "high",
  "notification" : {
    "body" : "hello!",
    "title": "afruz",
    "sound": "default"
  }
} 
Retortion answered 31/5, 2016 at 15:38 Comment(5)
Have you tested this with sandbox tokens?Parotic
For me, it only worked with a priority of "high". "normal" was never delivered.Phalanx
I'm also having the same issue. If I use the dashboard, the push is delivered, but it does not work from the API. Im using a dev push certificate.Baptistery
I'm not supposed to say "thanks me too" but ... thanks. This really needs to be in the docs.Sherri
This is completely insane! Lost two days reviewing .p12 certificates again and again when all I need was this priority thing! priority did the trick!Mcilroy
T
1

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

Touchdown answered 20/5, 2016 at 6:9 Comment(0)
L
1

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.

Linus answered 4/9, 2016 at 1:24 Comment(0)
S
1

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

Here is a recap: enter image description here

Samal answered 20/12, 2016 at 9:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.