0

I am trying to receive notifications from FCM automatically whenever data in the Firebase Database is changed. What I am hoping to do is to send a POST request to https://fcm.googleapis.com/fcm/send in the OnDataChange() method of Firebase Database. I also want the new value of the changed variable to be present in the notification. I have tested the POST request in Advanced Rest Client and it is working nicely. What I am looking for is to automate the process so that I do not need to create a server. I am new to Android so I am having a little trouble trying to implement this. It would be very helpful if someone could show me how to go about creating the POST request directly in Android in the format below with the flexibility of being able to change the value of the notification text and body.

Headers

Authorization: key=AAAA4wkL01w:APA91bHu... //Server Key
Content-type: application/json

Body

{
"to": "eGS6QMzVlbA:APA91bFtY...", //Token
"notification": {
"title": "title",
"body": "body text",
"icon": "ic_notification"
}
}
Farhana Naaz Ansari
  • 7,524
  • 26
  • 65
  • 105
  • The problem with the post request from an android device is the authorization part. You would have to distribute the secret key that should be used on the server only. Besides, I think FCM expects the request restricte to a specific domain. – Juan Nov 13 '17 at 10:38
  • So what would you suggest I do? – Rohit Agrawal Nov 13 '17 at 10:57
  • The android device should hit a server, and have the server send the request to fcm. – Juan Nov 13 '17 at 10:59
  • I tried to create a local wamp server and running the following php script: https://pastebin.com/chSEe9vm, but it is not working. The notification does not appear. – Rohit Agrawal Nov 13 '17 at 11:06
  • You need to check where it is failing. If you ar using curl, check the curl info after executing the request, and if you get a response check to see the errors. – Juan Nov 13 '17 at 11:12

0 Answers0