Send Bulk Notification In Node js Using Firebase Cloud Messaging

Devendra Patil
Canadiv’s Technology and Design
5 min readSep 17, 2023

What are notifications?

A notification is a message that Android displays outside your app’s UI to provide the user with reminders, communication from other people, or other timely information from your app.

These notifications can contain various types of information, such as news updates, reminders, messages from social media platforms, or alerts from mobile applications. They are commonly used by apps to engage users, provide updates, and re-engage users who may not have recently interacted with the app.

Push notifications are a crucial part of mobile app engagement and are widely used across different industries including social media, news, e-commerce, and more. They can be configured to include text, images, sounds, and even interactive elements, like buttons, allowing users to take specific actions directly from the notification.

We are using push notifications for several purposes.

  1. Instant Communication: They allow for instant communication with users. This can be crucial for time-sensitive information or updates.
  1. User Engagement: Notifications help in keeping users engaged with an application. They serve as a reminder of the app’s existence and encourage users to interact with it.
  2. Information Updates: They deliver important information, news, or updates directly to a user’s device without them having to actively check for it.
  1. Personalization and Customization: Notifications can be personalized based on user preferences, behavior, and location. This allows for a more tailored and relevant user experience.
  2. Re-Engagement: Notifications can be used to re-engage users who may not have used the app in a while. For example, by sending special offers or updates.
  3. Promotions and Marketing: They can be used for marketing purposes, such as promoting new features, products, or services.
  4. Alerts and Reminders: Notifications are used to provide alerts, reminders, and important information, such as calendar events, deadlines, or emergency alerts.
  5. Feedback and Surveys: They can be used to collect feedback from users or to conduct surveys about their experience with the application.

Bulk push notifications are an essential tool for businesses and organizations to communicate effectively with their audience. Here are several reasons why bulk push notifications are important:

  1. Reach a Large Audience: Bulk push notifications allow you to reach a large number of users simultaneously. This is crucial for businesses with a substantial user base, as it ensures that important information or updates are delivered efficiently.
  1. Time-Sensitive Information: For time-sensitive information like flash sales, limited-time offers, or breaking news, bulk push notifications are highly effective. They can quickly alert users and encourage them to take immediate action.
  2. Increased Engagement: Push notifications have a higher chance of being seen compared to emails, as they appear directly on a user’s device screen. This can lead to increased engagement rates, especially for important announcements or promotions.
  3. Cost-Effective: Sending bulk push notifications is often more cost-effective than other forms of communication, such as SMS or traditional advertising. It requires fewer resources and can yield higher engagement rates.
  4. Personalization and Segmentation: Even when sent in bulk, push notifications can be personalized based on user behavior, preferences, and location. This helps in providing relevant and targeted content, which improves user experience and engagement.

Overall, bulk push notifications are a powerful communication tool that, when used effectively, can enhance user engagement, drive conversions, and ultimately contribute to the success of a business or organization.

Difference between Push and Text Messages

Text messages or SMSes might seem like an old approach to reaching customers. However, it can be an effective medium to reach out to customers, especially those who do not have a smartphone to install the app. You can also automate SMS campaigns using automation tools.

The drawback is that you cannot add rich media content such as GIFs, animations, and emojis to make the text visually appealing. Push notifications let you do that. There are legal limitations, such as certain countries having a cap on the number of text messages you can send daily. Push notifications have no such limits.

Another significant difference is it’s easier to opt out of push messages than messages. While this might seem like good news to marketers, your customers may find your text messages intrusive and spam and may even choose not to engage with you anymore. This can have a significant impact on customer loyalty.

Now we should go step by step to reach our destination, For sending the bulk notification to the user using the code. which platform we are using is listed below.

  1. Node js (Express).
  2. Firebase.

Now Initialize our node js Application.

I assumed that node js is already installed on your machine.

Before starting any new Node.js project we should run npm init to create a new package.json file for our project.

npm init

and also install our required extensions like

npm i express fcm-node

we use Firebase cloud messaging for sending notifications. Then our package.json file looks like this

{
"name": "send_Bulk_notification",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"dependencies": {
"body-parser": "^1.19.2",
"express": "^4.17.3",
"web-push": "^3.4.5",
"fcm": "^1.0.3",
},
"scripts": {
"start": "node index.js"
}
}

now we are creating a file name index.js so here is a sample code for sending Bulk notifications. now our index.js file looks like this.


export const groupNotifications = functions.https
.onCall(async (data) => {
const fcm = admin.messaging();
const topic = data.topic;
const payload = {
notification: {
title: data.title,
body: data.body,
sound: "default",
},
};
// Send a message to devices subscribed to the provided topic.
fcm.sendToTopic(topic, payload)
.then((response) => {
// Response is a message ID string.
console.log("Successfully sent message:", response);
return response;
})
.catch((error) => {
console.log("Error sending message:", error);
return error;
});
});

In this sample code, we need the topic on the basis of the topic we send the notification to that user For eg. we need to send a notification of that user whose city is as per your requirement then they receive the same notification

We have also saved secret tokens for each user on the user Document and if the user logs in on 2/3 of the devices also we send a notification on all devices. Once we get the token we send a notification bar when the user is not working on the Application or an App notification when the user is working on the Application.

Note: This function was created on the on-call method If we want we have also sent a notification onUpdate method as per our requirement

Hey! Now it’s time to deploy our code on our Firebase. deploy it by clicking on deploy on the package.json file and our function is deployed successfully….
now…check it test user and you receive a notification……👌

--

--