Bawpotter Bawpotter - 4 months ago 191
HTTP Question

Firebase Notifications: Send Message Through HTTP Post

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?

Answer

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
Comments