Scaleup Guide
Building a Real-Time Notification System in Node.js with Firebase
In this guide
A simple guide to Building a Real-Time Notification System in Node.js with Firebase. Learn what to build first, what to check, and where mistakes usually happen.
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);
}
};