How to manage push notification from NodeJS Backend

Gaetano Di Russo
4 min readFeb 18, 2023

--

Push notifications are great for engage users to your app, almost all apps use them, so it’s an imprtant feature you must know how to implement them.

How push notifications work internally?

What we have to understand first of all is that push notifications work thanks to Firebase and APNs (Apple Push Notification service), which are two services that are able to send push notifications to our devices (Android and iOS).

So, what we have to do from the backend? Of course we are not going to send the push notification directly to the device, because we have to use one of the two services mentioned above. What we must do from the backend is to ask these services to send the push notification to the device/s we want.

It’s important to notice that we need the device push token of the device we want to send the push notification, to do that, it’s important to have an endpoint (or in the login endpoint) to receive the push device token from the logged user, then we save the token in the database to use it when need it.

In this tutorial we are going to show you how to configure the notifications from Firebase for Android devices, in another chapter we will show you how to integrate the APNs for iOS devices.

Let’s start by configuring the Firebase Console

Go to the firebase console and create a project (you must be logged in to google)

Select the Create a project button, then enter your project name.

You can enable analytics if you want, but for now we are going to disable them.

Finally click on Create project.

Create Android app in Firebase

Now we have the firebase project created, we need to create the app instances for the Frontend apps we want to send notifications, we could create one for Android and one for iOS, or even two for Android and one for iOS, it depends on which apps do you want to enable push notifications from this Firebase project.

Let’s create just one Android app. Click the Android icon, then copy your Android package name of the app you want to receive push notifications. You can also add an optional App nickname to identify your app in firebase.

Then click on Register app, and after that you are going to download a json file you have to place in your frontend app in order to start configuring push notifications in Android. In this part is supposed that you already know how to configure push notifications in Android, because this tutorial is just for the backend side. If you don’t know how to do it, please check my post about push notifications in React Native.

Get Firebase credentials required for the NodeJS backend

Once your Android app is created in Firebase and all the configuration in your mobile app is done, go to settings (gear icon) and click on Project settings. Go to Service accounts tab and click on Generate new private key, with this you are going to download a json file with the keys you need to initialize the Firebase push notification sdk in your backend.

Configure NodeJS Backend

Now let’s go to our NodeJS project and configure what we need.

First of all, we need to install the firebase-admin library:

npm i firebase-admin

Then we need to create a file and initialize the firebaseAdmin instance. To do that we have to copy the json file we downloaded in the previous step to our project directory, and then require that file in our code.

After that, we have to pass that file as a parameter in the initializeApp() function.

Finally, we are going to create a basic function called sendPushNotification.

Now we can use this function in our app, and we are going to be able to send push notifications, we just need the devicePushToken from the device we want to send the notification.

Remember we need to have the Android app configured to receive push notifications.

--

--

Gaetano Di Russo

I'm a Fullstack developer, focused on JavaScript/TypeScript, using NodeJS for the backend and ReactJS/ReactNative for the Frontend.