Be sure to read this documentation thoroughly for setting up your Built and Uploaded App on the Google Play Store with Firebase. You must have the binary on the Play Store for this to work.
So here’s how I do it:
- I check if Notification permissions need to be granted. And if so…
- Request notification permissions (though on my Android no dialog ever pops up, it just enables them. I think this is a bug)
- Get Device Token (we need this to Identify our device to Firebase)
- I send a HTTP POST request to subscribe my Device Token to my app Topic, in my case I chose “lighthouseconnect 3”, to this endpoint:
“https://iid.googleapis.com/iid/v1/” + appVars.APP_UserProfile.DeviceToken + “/rel/topics/lighthouseconnect3”
The flow function blocks underneath that basically do the same thing except it sends a DELETE request to Unsubscribe the Device Token from the app Topic if the user chose to not receive notifications.
So now Google says it can take 1 day before your Topic is created for the first time. So it might take that long to get to really test sending notifications. But for now let’s look at what it takes to create and send a notification.
I have a Bulletin Board page where users post messages. And when they post a message to the board it comes to the below 2 blocks:
First I set up my Message object variable (APP_FirebaseMessage) to the notification data I want to send. This is the basic structure of this message object variable:
In the “body” field I just put whatever message the user posted on the Bulletin board. The “title” field is just whatever title you want. I put something like “< username > has posted a new Bulletin!”. Sound = default, and content_available = true.
The “to” field is important because here I send it to the Firebase Topic that we created in the first step (the “HTTP POST Subscribe to Topic” flow function shown above). If you just wanted to send to 1 device (perhaps for an Instant Message app) you would put a registered Device Token here, but that would be useless for this implementation here which needs to notify multiple users. Sending to Topics is the preferred way to go for what I want to do.
Next I set up the actual HTTP POST request to send the message to Firebase:
The URL endpoint to send to is: https://fcm.googleapis.com/fcm/send
HTTP method: POST
Request body: the Message object data (“APP_FirebaseMessage” object variable I built above)
The Header must be populated like this:
The “value” field must be “key=< your Firebase Server Key >” (eg: key=bEidGJU2uGX_RR-RhS…). You get your Server Key on your App Project Settings on Firebase like):
I actually download this Server Key from my database and store it in a variable (APP_FirebaseMessageAuthKey). I don’t want this hard-coded in the app.
So that’s pretty much it. Once you send the HTTP POST to Firebase, with your app binary installed on your phone, you should hopefully see the notification appear.
But remember after you build your App (with the google-services.json file that you downloaded from Firebase for your app) you must upload it to the Play Store before any of this will work. But what ive noticed is that while you keep the app binary installed on your device (with notifications enabled), you can test sending notifications from your development environment. Actually, you can send notifications to your topic from anywhere you want, from Postman or anything that can send over HTTP, targeting just the Topic you specified.
Im still learning how to do this fully, and i still havent been able to test iOS yet, but Hopefully this will help somebody, and I will gladly listen to any suggestions or corrections in how Im doing this. The advantage of doing it this way is that it doesnt require any custom back-end at all.
Hope this was helpful.