Back to results for “Background Jobs & Queues”
Node.js › Email & Notifications

How would you send a push notification from a Node.js backend to a mobile app using Firebase Cloud Messaging (FCM)?

Intermediate

Answer

The backend uses the Firebase Admin SDK, authenticated with a service account, to send a message targeting a specific device (via a registration token the client app previously obtained and sent to the backend) or a topic (letting multiple subscribed devices receive the same message) -- FCM then handles actual delivery to the device across both iOS and Android.
const admin = require('firebase-admin');
admin.initializeApp({ credential: admin.credential.cert(serviceAccount) });

await admin.messaging().send({
  token: userDeviceToken,
  notification: { title: 'Order Shipped', body: 'Your order is on its way!' },
});
Real-world example An e-commerce app sends a push notification via FCM to a customer's phone the moment their order ships, using the device token the mobile app registered and sent to the backend during login.

Common follow-ups: How do you handle an invalid or expired device token returned by FCM when sending a notification?;What's the difference between targeting a specific device token versus a topic subscription for broadcast notifications?