In this article, I’m going to share with you how I upgraded a legacy web service in NodeJS/Express for push notification via Firebase. How I Solved A Push Notification Bug on a legacy NodeJS
Ever had that moment where you really want to point the finger at someone else for a buggy feature? Well, I had such and in fact, I had it for weeks!!
Hey guys, been a while I wrote an article and that’s because I having been writing codes 😺. In this article, I’m going to share with you how I upgraded a legacy web service in NodeJS/Express for push notification via Firebase
Recently, I inherited a legacy web service written in NodeJS/Express and coincidentally the web push notification the service provided started failing. Hmm, just my luck right?
It so happens that the push notification service provider is Firebase Cloud Messaging and this was working smoothly until it did not and it broke(thanks to Uncle Murphy) in a beta test version.
So clients (Android devices and iOS devices) were not receiving the loved pushed notification. I need not add how upsetting this was for the stakeholders of the project right ?
So with my detective hat on (feeling like Sherlock Holmes are we now?), I studied how the push notification feature was being implemented on the web service. The first thing I noticed was that the service was using the legacy HTTP protocol as described here.
Noticing that, I decided to use good ‘ol console.log on the response sent from the firebase Cloud Messaging service when it was called using the legacy HTTP protocol and I found this…
So I checked here to see what that error code could mean specific to Firebase and it said:
(HTTP error code = 403) The authenticated sender ID is different from the sender ID for the registration token.
Doing more debugging I really couldn’t see how that was possible since everything seems to be okay(the developer was really following the configuration of the legacy HTTP method to the letter)
After weeks of searching StackOverflow and every article I could find on the said matter, I decided to update the legacy API used for calling the Firebase Cloud Messaging service to the most advocated…
Using the docs, I did the following:
$ npm install firebase-admin --save
Requiring the Firebase SDK in my NodeJS/Express Web Service
Now according to the setup docs, you will need to generate a private key file for your service account in order to initialize the Firebase Admin SDK. You can find this by going to the firebase console, selecting your app and clicking on the cog icon to see the settings(The docs also describes this process)
I placed the generated file in my root directory(trust me I .gitignore it 😃). Then I update my code to have this:
💦 hope that wasn’t too much to take in? So let’s use the Firebase Admin to send a test push notification to our client apps, shall we?
Before you raise any eyebrows, In the spirit of DRY, I am using a helper function here called: sendPushNotification which takes in the message configuration object as an argument. Below is the definition of the helper function:
So you could see above how the Firebase Admin SDK is being used. Not surprisingly it returns a promise which on successful, returns the message-id of the push notification message. I am simply logging it out here, but you, of course, can do whatever you want(It’s a free world ).
So there you go, that’s how I saved the day for my stakeholders and it was all smiles when push notification started working like a charm again thanks to the Firebase Admin SDK.
I’ve just finished building a CRUD API with Restify using Firebase for storage and auth. Here’s how I did that and some gotchas I found.
Looking to **hire NodeJS Developer**[https://www.dataeximit.com/node-js-development/](https://www.dataeximit.com/node-js-development/ "https://www.dataeximit.com/node-js-development/") to build a perfect back-end to your website or...
In this tutorial, you will learn how to install Node onto Ubuntu 19.04 Disco Dingo. We will cover installation from the default repositories and, for those wanting more recent releases, how to install from the NodeSource repositories.