I have Firebase Notifications set up in my app right now, and when I send a message the message is sent to all users of my app. (I currently send messages through the Firebase console). I'd like another way of sending push notifications that doesn't involve the Firebase console, and I believe that HTTP Post is an easy way of doing this. How do I go about sending a push notification remotely with HTTP Post?
1 Answers
Edited because on comment: Please make sure do NOT include the server-key into your client. There are ways "for not so great people" to find it and do stuff... The Proper way to achieve that is for youre client to instruct youre app-server to send the notification.
You have to send a HTTP-Post to the Google-API-Endpoint.
You need the following headers:
Content-Type: application/json
Authorization: key={your_server_key}
You can obtain youre server key within in the Firebase-Project.
HTTP-Post-Content: Sample
{
"notification": {
"title": "Notification Title",
"text": "The Text of the notification."
},
"project_id": "<your firebase-project-id",
"to":"the specific client-device-id"
}
Example Device ID:
cc6VGMjpIiA:APA91bGLpm5Z2p0NNh7nxttCTVd1tTsL2jObDaS9U8G1YjDjkpwkBlRLjU89ns4ujQ7rFU1Z2NshpUAX2RiQiIDKhHJdB0RtSS3H6nTT-lGEkIpzVtVzJpLIVqzSVbRjmyYlxD3BSLZl
You have to send this request to https://fcm.googleapis.com/fcm/send
.
As of now it is not possible to send a notification to all devices using the API. You have to use the Firebase-Console for that.
I like to use the Chrome-Plugin "Postman" for sending API requests, as you can save your HTTP-Requests. Its very comfortable.
You can use curl as well.
curl
-X POST
-d "{ "notification": {
"title": "Notification Title",
"text": "The Text of the notification."
},
"project_id": "<your firebase-project-id",
"to":"the specific client-device-id"
}"
-H "Content-Type: application/json"
-H "Authorization: key={your_server_key}"
https://fcm.googleapis.com/fcm/send

- 311
- 3
- 9
-
This is indeed a great example of how to POST messages using FCM. Note that since this approach requires the use of your FCM server key, you should not run this code on a client device. Exposing your server key inside the client makes it possible to malicious user to find your key and send message on your behalf. See http://stackoverflow.com/questions/37435750/how-to-send-device-to-device-messages-using-firebase-cloud-messaging – Frank van Puffelen Aug 03 '16 at 03:09
-
Yes - that was kind of misleading. I edited my post accordingly :) – Hannes Aug 03 '16 at 08:12
-
Thanks for the answer! If I wanted to send this request through Swift code, how would I go about doing that? – Bryan Aug 03 '16 at 19:43
-
I never did that in swift code. There has to be some sort of HTTP-Lib which simplifies the problem. For Java/Android there is Volley... You basically have to make the same request. [This might help!](http://stackoverflow.com/questions/26364914/http-request-in-swift-with-post-method) Best of Luck :) – Hannes Aug 04 '16 at 09:36