How I Solved A Push Notification Bug on a legacy NodeJS/Express Server using Firebase Admin

Kelvin Omereshone
codeburst
Published in
4 min readNov 7, 2019

--

Photo by Jamie Street on Unsplash

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

The Problem

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 😅?

https://content.wisestep.com/bad-boss-memes/

Putting on my Debugging Hat 🤠

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…

It was returning a 403: Forbidden…

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)

Paying the Technical Debt 😰

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…

Firebase Admin

Using the docs, I did the following:

  1. Installed the Firebase Admin SDK on the Web Service via:
$ npm install firebase-admin --save

2. Then I went ahead(of course) to import the module using CommonJS syntax:

Requiring the Firebase SDK in my NodeJS/Express Web Service

3. 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)

4. I placed the generated file in my root directory(trust me I .gitignore it 😃). Then I update my code to have this:

5. I initialized the Firebase Admin SDK to get it rolling:

Initializing the SDK

Are We Pushing Yet?

💦 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?

Sending a push notification

Hold the Phone? ☎️

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:

Firebase Admin at Work

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 😄).

Rounding up…

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.

That feeling

--

--

Currently building Sailscasts — a platform to learn server-side JavaScript with the Sails.js framework. Come learn and master Sails.js @ https://sailscasts.com