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

Bootstrap 5 Complete Course with Examples

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

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Using Firebase for Storage and Auth with NodeJS & Restify

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.

The essential JavaScript concepts that you should understand

The essential JavaScript concepts that you should understand - For successful developing and to pass a work interview

Hire Nodejs Developer

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

How to Install NodeJS on Ubuntu 19.04

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.

How to Make a Login & Sign Up in HTML, JavaScript and Firebase

How to make a login and signup page in HTML, JavaScript and Firebase. Hey guys and gurls, I'll be showing you how to make a sign in and signup page using fir...How to make a login and signup page in HTML, JavaScript and Firebase. Hey guys and gurls, I'll be showing you how to make a sign in and signup page using firebase authentication(database). This concept is very easy to learn and implement. We'll be using Firebase Authentication app to store email values.