Push Notification Tutorial

I really wish someone could do a video tutorial for push notifications. The written tutorial is not providing the information needed to implement the feature and it’s the last thing to figure out before submitting my app!

Mainly it’s missing details on the HTTP request. Is the HTTP request supposed to post to firebase since that’s the method to send the notifications through for android?

More detailed written instructions or a video tutorial would be so very helpful!

3 Likes

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:

  1. I check if Notification permissions need to be granted. And if so…
  2. Request notification permissions (though on my Android no dialog ever pops up, it just enables them. I think this is a bug)
  3. Get Device Token (we need this to Identify our device to Firebase)
  4. 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:

Capture3

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:

Capture6

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:

Capture4

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:

Capture5

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):
Capture8
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.

9 Likes

Also, Be aware that if your app is running in the foreground that you will not receive the notification you send, unless you build out data{…} fields in your notification message and have code to handle the Notification Received event, but thats another thing… So to see a notification come to your device your app must be closed and in the background. This means you have to send it from your development environment, or from Postman or some other program besides your app.

@JOHN_WORSHAM wow, it is a very good approach. I am using a firebase with a custom back end. But how can an app send a post request when it is not running? let’s say I want to send a push notification every day, how can the app send a post request to firebase?

Thanks! You would need a back end to send notifications that way since the app has to be running to send.
The way i do it works without a back end because the notifications are generated by the user running the app.

1 Like

Okay great! Thank you
Now last question about point number " 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: "

How do i create a topic name?

Pick any name you want

In my example you just sent a HTTP POST request:

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

so after getting the token code and sending the http request , the device will be registered in firebase right?
then when i create a new notification from firebase dashboard the registered device should receive the push notification ?

Yes if youre using Topics, and you Subscribe that device to the topic you create, then any notification you send To: /topics/(your topic name) will appear on their device

Okay understood , thanks a lot!
Now let’s say i want all the registered devices to receive automatically a notification when a new post was posted( let’s say when the administrator post something new , they get the notification ) any idea how this can be done?

Just subscribe all the devices to a common topic and send to that topic

can the notification trigger be sent from the app? i mean automatically when admin click on button(to post something new) the notification will be directly sent

Thats how I send mine from the app

Hey John , i was finally able to send Push Notification from a device.(Tested on Android )

Now i have two question in mind :
1 - does the same configuration will works as well when i build the app on ios? i mean i will still receive the notifications from firebase
2 - I noticed that the notification doesn’t work while the app is running on the receiver mobile , is there any solution for this .

  1. It should work the same.
  2. If the app is running then you have to Receive Notification Event and handle the object data that is sent in the app. I havent done this in any of my apps, but in the case of Android you need to fill out the ‘Data’ fields (just like the Notifcation fields) of the message object.

Hey John, i submitted my app to Apple TestFlight , but unfortunately the push notification with the same configuration is not working on IOS ( while the same configuration is working on Android )
And i think there’s a problem with topic subscription on IOS , because it return me empty error with no details.

Hi @JOHN_WORSHAM, thanks so much for the above tutorial.

I however have a question, I’ve set everything up as explained above, but for some reason topic registration doesn’t happen.

I have a test android device I used to download my app as an internal tester via the google play portal, so it’s a live version of the app.

It registers the device to receive push notifications, so I can send to “all users” in Firebase, as well as to Android only, but If I try send to a topic from Firebase, the phone doesn’t receive the message.

Any ideas’s?

Hello friend … I am following your tutorial and after solving some questions, I am managing to adapt the functionality to my application.

Could you comment how you download Server Key from your database?

Just store it in a record in your database, and GET the record into the app.