DEV Community

Sujit M
Sujit M

Posted on

1 1

FCM Integration with NodeJs using firebase-admin

In this part we will understand how to send notification from NodeJs server.

If you want to integrate with react then, check out here

Lets create a node server first.

I am guessing npm and node is already installed if not then Install using nvm

Initialize a NPM Project

npm init -y 
Enter fullscreen mode Exit fullscreen mode

Install dependencies

npm i express firebase-admin
Enter fullscreen mode Exit fullscreen mode

Now lets generate a firebase private key for admin

Firebase Admin SDK private key

  • Open firebase console
  • Select the project (Use same if you are following from previous chapter, FCM with React)
  • Open a project settings
  • Go to service accounts
  • Generate a new Key pair

Firebse Console Screenshot

This action will download a JSON file.

Rename the file google-cloud-service-account.json for simplicity

Create a file app.js (Make sure you update entry point in package.json)

touch app.js
Enter fullscreen mode Exit fullscreen mode
const http = require("http");
const express = require("express");
const queue = require("./services/queue");
const app = express();

const admin = require("firebase-admin");
const serviceAccount = require("./config/google-cloud-service-account.json"); // Make sure to provide a correct path

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

const router = express.Router();

router.get("/", (req, res) => {
    res.send("Hello World");
});

app.use(router);

const server = http.createServer(app);

server.on("request", (req, res) => {
    console.log("Request received");
});

// We are not mentioning the host name here, so it will listen on all the available network interfaces
server.listen(3000, () => {
    console.log("Server is running on " + JSON.stringify(server.address().port));
});

Enter fullscreen mode Exit fullscreen mode

This creates a server using express and http

Note - We have not used app.listen to create server instead http.createServer() to achieve more flexibility

Create a route to send notification

create user.routes.js

touch user.routes.js
Enter fullscreen mode Exit fullscreen mode

Add this code

const express = require("express");
const messaging = require("../services/firebase");
const router = express.Router();

router.get("/user-action", (req, res) => {
    // User request comes for specific action

    //Once the action is performed, we can send the notification to the user

    // Here we are sending a notification to the specif this user with mentioned token
    const registrationToken = "REGISTRATION_TOKEN";

    const data = {
        title: "Hello from the server",
        body: "This is a test notification",
    };

    const message = {
        notification: {
            title: data.title,
            body: data.body,
        },
        token: registrationToken,
    };
    messaging
        .send(message)
        .then((response) => {
            console.log("Successfully sent message:", response);
            res.send("Notification sent");
        })
        .catch((error) => {
            console.log("Error sending message:", error);
            res.send("Error sending notification");
        });
    // res.send("Send notifications");
});

module.exports = router;

Enter fullscreen mode Exit fullscreen mode

Update your app.js

const http = require("http");
const express = require("express");
const app = express();
const userRoutes = require("./user.routes");

const router = express.Router();

router.get("/", (req, res) => {
    res.send("Hello World");
});

app.use(router);
app.use("/users", userRoutes);

const server = http.createServer(app);

server.on("request", (req, res) => {
    console.log("Request received");
});

// We are not mentioning the host name here, so it will listen on all the available network interfaces
server.listen(3000, () => {
    console.log("Server is running on " + JSON.stringify(server.address().port));
});

Enter fullscreen mode Exit fullscreen mode

Now lets add Registration Token of the client device

Create a device token

Follow these instruction to create a device token.

Replace the token with REGISTRATION_TOKEN in user.routes.js

Restart your server

Visit the url
http://localhost:3000/users/user-action and you will receive a notification at the client device.

You can find the entire working code here

Happy Coding

Top comments (0)

Image of PulumiUP 2025

Let's talk about the current state of cloud and IaC, platform engineering, and security.

Dive into the stories and experiences of innovators and experts, from Startup Founders to Industry Leaders at PulumiUP 2025.

Register Now

👋 Kindness is contagious

Value this insightful article and join the thriving DEV Community. Developers of every skill level are encouraged to contribute and expand our collective knowledge.

A simple “thank you” can uplift someone’s spirits. Leave your appreciation in the comments!

On DEV, exchanging expertise lightens our path and reinforces our bonds. Enjoyed the read? A quick note of thanks to the author means a lot.

Okay