Push Notifications Using Node.js and Firebase

Push Notifications Using Node.js and Firebase

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

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?

This is image title

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 ?

This is image title

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
  1. Then I went ahead(of course) to import the module using CommonJS syntax:

This is image title Requiring the Firebase SDK in my NodeJS/Express Web Service

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

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

This is image title

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

This is image title

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?

This is image title

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:

Hold the Phone

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.

nodejs javascript Firebase

What is Geek Coin

What is GeekCash, Geek Token

Best Visual Studio Code Themes of 2021

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

What is firebase,firebase bangla tutorial.

LIKE | COMMENT | SHARE | SUBSCRIBE The Firebase Realtime Database is a cloud-hosted NoSQL database that lets you store and sync data between your users in re...

Decoding Nodejs

The main goal of this blog is to explain the “Architecture of Nodejs” and to know how the Nodejs works behind the scenes. Generally, most of the server-side languages, like PHP, ASP.NET, Ruby, and including Nodejs follows multi-threaded architecture. That means for each client-side request initiates a new thread or even a new process.

Hire NodeJs Developer

Looking to build dynamic, extensively featured, and full-fledged web applications? **[Hire NodeJs Developer](https://hourlydeveloper.io/hire-dedicated-node-js-developer/ "Hire NodeJs Developer")** to create a real-time, faster, and scalable...

Hire Dedicated JavaScript Developers -Hire JavaScript Developers

Hire dedicated JavaScript Developers who are proficient in AngularJS, ReactJS, NodeJS, & VueJS frameworks. Get flexible hiring models as per your business requirements.

Firebase Introduction with C#

LIKE | COMMENT | SHARE | SUBSCRIBE Firebase is a mobile and web application development platform developed by Firebase, Inc. in 2011, then acquired by Google...