Scaleup Guide
Building a Real-Time Notification System in Node.js with Firebase
Direct Answer
This guide gives a simple overview of building a real-time notification system in node.js with firebase. If you need help turning the idea into a website, app, or business system, the related Scaleup Infotech services are listed below.
Push notifications are essential for user retention. In this guide, we will set up a Node.js Express server that triggers Firebase Cloud Messaging (FCM) to send notifications.
Step 1: Setup Firebase Admin SDK
const admin = require("firebase-admin");
const serviceAccount = require("./serviceAccountKey.json");
admin.initializeApp({
credential: admin.credential.cert(serviceAccount)
});
module.exports = admin;Step 2: Sending the Notification
const sendNotification = async (token, title, body) => {
try {
const message = {
notification: { title, body },
token: token
};
const response = await admin.messaging().send(message);
console.log("Successfully sent message:", response);
} catch (error) {
console.log("Error sending message:", error);
}
};