Firebase Push Notifications

Does anybody know how to send notifications from the app using API ( a HTTP POST request) ? If you have please let me know how you do it. I can do notifications from the Firebase console but not programmatically from the app.

I have a use case where users post to a message board and i want a notification sent to all users when this happens.

If anyone can advise please let me know thanks

You can send a post request to : https://fcm.googleapis.com/fcm/send

Headers :
Content-Type: application/json
Authorization: key=YOUR-FIREBASE-KEY

Body:
{
“to”: “THE DEVICE TOKEN YOU WANT TO NOTIFY”,
“notification”: {
“sound”: “default”,
“body”: “test message”,
“content_available”: true,
“title”: “ENTER YOUR TITLE”
},
“data”: {
“sound”: “default”,
“body”: “test message”,
“content_available”: true,
“title”: “ENTER YOUR TITLE”
}
}

Im doing this from my backend, but I dont see any reason you wouldnt be able to post it directly from appgyver

Also to test it you’ll need to build your app - it doesn’t seem to work in the appgyver preview app

Where Im running into issues is that I need the “to” to go to all users. Ive read this was possible by setting to:/topics/all … but this doesnt seem to work. I read about Subscribing to topics but that functionality doesnt seem possible through a HTTP request.

In your example I assume “THE DEVICE TOKEN YOU WANT TO NOTIFY” is the token we get from the flow function ‘Get Device Token (beta)’ . My question about this is - do you submit that Token to Firebase to register it first, and if so how do you do that? Or does the Token only need to be just sent in the “to” field of the push request, as you showed above?

I don’t think you need to register it with Firebase first. I’m storing the device token in my backend and simply POSTing it in the to field which seems to be working well on Android.

When I need to send push notifications to multiple recipients I am currently looping through a list, though this might not be feasible in your case where you need to send it to lots of recipients at once.

Ok I understand. Thanks for your help.

@Anuj_Nayyar I solved the problem! :smiley:

I have figured out how to send to multiple users, all users, of just the app they are using. When I get their Device Token, they send a HTTP Post request to Create/Subscribe to a topic I created. In this case, since my app is named Lighthouse Connect 3, i use “lighthouseconnect3” as the topic name. So the HTTP url looks like this:

https://iid.googleapis.com/iid/v1/” + appVars.APP_UserProfile.DeviceToken + “/rel/topics/lighthouseconnect3”

This endpoint will subscribe the user’s Device Token to the topic. So when users post a new bulletin message, they just send a push notification message from the app to the topic like this:

{
“to”: “/topics/lighthouseconnect3”,
“notification”: {
“sound”: “default”,
“body”: “message post”,
“content_available”: true,
“title”: “user-name posted a message!”
},
“data”: {
“sound”: “default”,
“body”: “message post”,
“content_available”: true,
“title”: “user-name posted a message!”
}
}

And Firebase sends to every device token subscribed to that topic. It works wonderfully, without having to loop through Token IDs like you do.

2 Likes

Good day, I keep getting the error “MismatchSenderId”. Do you know what the problem may be? I’m using the correct token