Firebase : Send notification with REST API

72,338

Solution 1

this may help - https://firebase.google.com/docs/cloud-messaging/http-server-ref where sample message you can find here - https://firebase.google.com/docs/cloud-messaging/downstream

from Firebase console you can get Server Key as an authorization you put in the http header, in the tab Cloud messaging.

Solution 2

Just for helping,

If anyone wants to use REST POST API, here it is, use the Postman with below configuration

URL:
https://fcm.googleapis.com/fcm/send

Header:

"Content-Type": "application/json",
"Authorization": "key=<Server_key>"

BODY:

{
    "to": "<Device FCM token>",
    "notification": {
      "title": "Check this Mobile (title)",
      "body": "Rich Notification testing (body)",
      "mutable_content": true,
      "sound": "Tri-tone"
      },

   "data": {
    "url": "<url of media image>",
    "dl": "<deeplink action on tap of notification>"
      }
}

That's it. Thanks!!!

If you want to get more details about Rich Notification with FCM, you can check my article on Medium Rich Push Notification with Firebase Cloud Messaging (FCM) and Pusher on iOS platform

Solution 3

I used the below rest API to send notification.

curl -X POST \
  https://fcm.googleapis.com/fcm/send \
  -H 'Authorization: key=AAAAG-oB4hk:APA91bFUilE6XqGzlqtr-M-LRl1JisWgEaSDfMZfHuJq3fs7IuvwhjoGM50i0YgU_qayJA8FKk15Uvkuo7SQtQlVt4qdcrrhvnfZyk_8zRGAskzalFUjr2nA2P_2QYNTfK6X8GbY0rni' \
  -H 'Content-Type: application/json' \
  -H 'Postman-Token: c8af5355-dbf2-4762-9b37-a6b89484cf07' \
  -H 'cache-control: no-cache' \
  -d '{
    "to": "ey_Bl_xs-8o:APA91bERoA5mXVfkzvV6I1I8r1rDXzPjq610twte8SUpsKyCuiz3itcIBgJ7MyGRkjmymhfsceYDV9Ck-__ObFbf0Guy-P_Pa5110vS0Z6cXBH2ThnnPVCg-img4lAEDfRE5I9gd849d",
    "data":{
        "body":"Test Notification !!!",
        "title":"Test Title !!!"
    }

}'

Authorization : key=AAAAG-oB4hk:APA91bFUilE6XqGzlqtr-M-LRl1JisWgEaSDfMZfHuJq3fs7IuvwhjoGM50i0YgU_qayJA8FKk15Uvkuo7SQtQlVt4qdcrrhvnfZyk_8zRGAskzalFUjr2nA2P_2QYNTfK6X8GbY0rni

where key is web_server_key from the console and you need to specify the unique registration key which you will get from the app.

enter image description here

"to": "ey_Bl_xs-8o:APA91bERoA5mXVfkzvV6I1I8r1rDXzPjq610twte8SUpsKyCuiz3itcIBgJ7MyGRkjmymhfsceYDV9Ck-__ObFbf0Guy-P_Pa5110vS0Z6cXBH2ThnnPVCg-img4lAEDfRE5I9gd849d" is the FCM registration token from device. Please refer to the below link.

https://firebase.google.com/docs/cloud-messaging/android/client?authuser=0

Solution 4

Using ARC For Sending Request to Firebase Console To Send Notification Using ARC For Sending Request to Firebase Console To Send Notification

You can use ARC OR Postman or your own server to send notification. You need to collect your web_server_key from the console and you need to specify the unique registration key which you will get from the app when calling the onRefreshToken() method.

You need to send the request to https://fcm.googleapis.com/fcm/send with Content-Type : json and Authorization: web_server_key. On To value user your app_registration_token .

Solution 5

Try this,

URL - https://fcm.googleapis.com/fcm/send

Method - Post

Headers

  • Authorization -> key= Server Key which you can get it from console
  • Content-Type -> application/json

Body

{
 "to" : "FCM Token goes here",
 "notification" : {
     "body" : "New Lesson Added 1",
     "title": "Lokesh"
 }
}
Share:
72,338

Related videos on Youtube

cimenmus
Author by

cimenmus

Updated on August 16, 2021

Comments

  • cimenmus
    cimenmus over 2 years

    Is it possible to send push notification with REST API on Firebase? I can send notifications with Firebase console but i need to send notifications with REST API.

    • Frank van Puffelen
      Frank van Puffelen almost 8 years
      Sending Firebase Notifications is only supported from the Firebase Console. But you accomplish the same by using Firebase Cloud Messaging, which has an API you can call (and is what Firebase Notifications are built on).
  • SriMaharshi Manchem
    SriMaharshi Manchem almost 7 years
    i want to send notifications to all the clients.So what would be "to" value
  • Piash Sarker
    Piash Sarker almost 7 years
    Then you need to create a topic . You can create or subscribe topic from your java code. The users that are subscribed to this specific topic can get the notification. If you want to send the notification to all users then create one topic and subscribe it from your java code. Reference - firebase.google.com/docs/notifications/android/console-topic‌​s
  • huggie
    huggie about 5 years
    At least on iOS the "data" key should be "notification" for it to show up. The "data" key is for custom data and I don't see the notification when testing without the "notification" key.
  • Sumanth Varada
    Sumanth Varada almost 5 years
    How to manage this device FCM token ? In my case, i wants to store it in mongo db on every login. is it fine ?
  • Ashis Laha
    Ashis Laha almost 5 years
    @SumanthVarada You can use any database to save the fcm token even Firebase database. fcm token is derived from device token, sent by APNS while registering for Remote notification which is constant until you delete the application and install a fresh one. You can do a local check while log in that fcm_token changed or not? If changed then make a server call to update else it's unnecessary to update the same information again.
  • Ashis Laha
    Ashis Laha over 4 years
    @KakiMasterOfTime for rich notification in iOS, people already reported to Firebase but the functionality yet to provide by Firebase console.(github.com/firebase/quickstart-ios/issues/…) Agree with you. Maybe in future Firebase will be smart enough to do all of these?
  • MarkusAfricanus
    MarkusAfricanus almost 3 years
    @AshisLaha Thank you so much! I looked at the Firebase docs and just needed one example. You made my day