How to Build a Push Notification System with Node.js and Firebase

How to Build a Push Notification System with Node.js and Firebase

Push notifications are one of the best ways to engage users in real-time. In this tutorial, you'll learn how to create a basic push notification system using Node.js and Firebase Cloud Messaging (FCM).

What You'll Need

  • Node.js (v14 or newer)
  • Firebase account
  • Firebase Cloud Messaging credentials (server key)
  • Frontend app (mobile or web) to receive notifications
  • Firebase Admin SDK

Step 1: Set Up Firebase

  1. Go to Firebase Console at https://console.firebase.google.com/
  2. Create a new project or use an existing one
  3. Navigate to Project Settings > Cloud Messaging
  4. Copy the Server key — you'll use this in your Node.js app

Step 2: Initialize Your Node.js Project

Open your terminal and run these commands:

mkdir push-server cd push-server npm init -y
npm install firebase-admin express body-parser` 

Step 3: Add Firebase Admin SDK

Download the serviceAccountKey.json file from your Firebase project by going to Firebase Console > Project Settings > Service Accounts and generate a new private key. Place this file inside your Node.js project folder.

Step 4: Create the Express Server

Create an index.js file and add this code:


admin.initializeApp({ credential: admin.credential.cert(serviceAccount),
});

app.use(bodyParser.json());

app.post('/send', async (req, res) => { const { token, title, body } = req.body; const message = { notification: { title, body },
    token,
  }; try { const response = await admin.messaging().send(message);
    res.status(200).send({ success: true, response });
  } catch (error) {
    res.status(500).send({ success: false, error });
  }
});

app.listen(port, () => { console.log(`Server running on http://localhost:${port}`);
}); 

Step 5: Send a Test Notification

Use a tool like Postman or cURL to send a POST request:

POST http://localhost:3000/send
Content-Type: application/json

{ "token": "YOUR_DEVICE_FCM_TOKEN", "title": "Hello!", "body": "This is a test notification from Node.js!" }

Where Do You Get the FCM Token?

On your frontend (mobile app or web client), use Firebase SDK to request and get a unique device token.

For Web (JavaScript), example code:

import { getMessaging, getToken } from  "firebase/messaging"; const messaging = getMessaging(); getToken(messaging, { vapidKey: 'YOUR_PUBLIC_VAPID_KEY' })
  .then((currentToken) => { if (currentToken) { console.log("Device Token:", currentToken);
    }
  });

Security Tip

Never expose your Firebase service account key in frontend code. Keep all push notification logic on your backend server to protect your credentials.

Final Thoughts

You have built a basic push notification server using Node.js and Firebase! Next steps you might consider:

  • Supporting multiple device tokens
  • Storing tokens in a database like MongoDB
  • Creating user segments for targeted notifications
  • Scheduling or triggering notifications based on events or cron jobs

Pro tip: Combine push notifications with analytics to measure delivery rates and user engagement.

admin
By admin

2025-05-16