Integrating Firebase Push Notifications (FCM) in Angular and Node.js Applications

Saunak Surani
Widle Studio LLP
Published in
4 min readAug 28, 2023

Firebase Cloud Messaging (FCM) offers a potent solution for sending push notifications across various platforms. This comprehensive guide will take you through the process of integrating Firebase Push Notifications in both Angular and Node.js applications. By the end of this tutorial, you'll be well-equipped with code examples to successfully implement push notifications in both frontend and backend environments.

Table of Contents:

  1. Understanding Firebase Cloud Messaging (FCM)
  2. Setting Up a Firebase Project
  3. Integrating Firebase Push Notifications in Angular (Frontend)
    Installing Required Packages
    Configuring FCM in Angular
    Requesting User Permission for Notifications
    Handling Incoming Notifications
  4. Integrating Firebase Push Notifications in Node.js (Backend)
    Setting Up a Node.js Project
    Installing Firebase Admin SDK
    Sending Notifications from Node.js
  5. Advanced Topics: Targeting and Customizing Notifications
    Sending Targeted Notifications
    Customizing Notification Content
  6. Conclusion

Section 1: Understanding Firebase Cloud Messaging (FCM)

Firebase Cloud Messaging (FCM) is a cloud-based messaging platform that enables sending messages to devices on iOS, Android, and the web. It empowers you to engage users by delivering timely and relevant content directly to their devices.

Section 2: Setting Up a Firebase Project

The initial step is to create a Firebase project. This involves setting up the project in the Firebase console, adding your application, and obtaining the necessary configuration settings.

Section 3: Integrating Firebase Push Notifications in Angular (Frontend)

Angular is a popular frontend framework, and integrating push notifications enhances user interaction. Here's how to proceed:

Installing Required Packages

Generate a new Angular project using Angular CLI. Install the @angular/fire package to facilitate Firebase integration.

ng new PushNotificationsAngular
cd PushNotificationsAngular
npm install @angular/fire

Configuring FCM in Angular

Integrate Firebase configuration into your Angular app, allowing seamless communication with the FCM server.

// src/environments/environment.ts
export const environment = {
production: false,
firebaseConfig: {
apiKey: 'YOUR_API_KEY',
authDomain: 'YOUR_AUTH_DOMAIN',
projectId: 'YOUR_PROJECT_ID',
storageBucket: 'YOUR_STORAGE_BUCKET',
messagingSenderId: 'YOUR_MESSAGING_SENDER_ID',
appId: 'YOUR_APP_ID',
},
};
// src/app/app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AngularFireModule } from '@angular/fire';
import { environment } from '../environments/environment';

@NgModule({
declarations: [AppComponent],
imports: [
BrowserModule,
AngularFireModule.initializeApp(environment.firebaseConfig),
],
bootstrap: [AppComponent],
})
export class AppModule {}

Requesting User Permission for Notifications

Implement code to request user permission for notifications through the browser's Notification API.

// src/app/app.component.ts
import { Component, OnInit } from '@angular/core';
import { AngularFireMessaging } from '@angular/fire/messaging';

@Component({
selector: 'app-root',
template: '<button (click)="requestPermission()">Request Permission</button>',
})
export class AppComponent implements OnInit {
constructor(private afMessaging: AngularFireMessaging) {}

ngOnInit() {
this.afMessaging.messaging.subscribe((messaging) => {
messaging.onMessage((message) => {
console.log('Message received:', message);
// Handle the received message here
});
});
}

requestPermission() {
this.afMessaging.requestPermission.subscribe(
() => {
console.log('Permission granted!');
// Handle successful permission grant
},
(error) => {
console.log('Permission denied:', error);
// Handle permission denied or error
}
);
}
}

Handling Incoming Notifications

Utilize Angular service workers to manage incoming notifications and display them to users.

Section 4: Integrating Firebase Push Notifications in Node.js (Backend)

Node.js is commonly used for backend development. Integrating push notifications in your Node.js application keeps users informed, even when they're not actively using the app.

Setting Up a Node.js Project

Set up a new Node.js project or utilize an existing one.

mkdir PushNotificationsNodejs
cd PushNotificationsNodejs
npm init -y

Installing Firebase Admin SDK

Install the firebase-admin package to establish interaction between your Node.js app and Firebase services.

npm install firebase-admin

Sending Notifications from Node.js

Develop code to send push notifications from your Node.js backend to devices using the Firebase Admin SDK.

const admin = require('firebase-admin');

// Initialize Firebase Admin SDK
admin.initializeApp({
credential: admin.credential.applicationDefault(),
databaseURL: 'YOUR_DATABASE_URL',
});

// Construct a notification message
const message = {
notification: {
title: 'New Notification',
body: 'This is a sample push notification.',
},
topic: 'allDevices', // Replace with your topic or token
};

// Send the notification
admin.messaging().send(message)
.then((response) => {
console.log('Notification sent successfully:', response);
})
.catch((error) => {
console.error('Error sending notification:', error);
});

Section 5: Advanced Topics: Targeting and Customizing Notifications

Sending Targeted Notifications

Discover how to send notifications to specific user segments, enhancing personalization and engagement.

Customizing Notification Content

Tailor notification content, including titles, bodies, and images, to create a more personalized user experience.

Section 6: Conclusion

This comprehensive guide walked you through the process of integrating Firebase Push Notifications in both Angular and Node.js applications. You learned how to set up Firebase projects, configure FCM in Angular, request user permission, handle notifications, and implement push notifications in Node.js. By applying this knowledge and code samples, you can enrich your applications with real-time communication and elevate user engagement.

In today's digital landscape, effective communication is paramount, and push notifications serve as a robust tool to keep users engaged and informed. With Firebase Cloud Messaging at your disposal, you possess the tools to craft exceptional user experiences and establish meaningful connections with your audience. It's time to embark on this journey of seamless communication and bring your applications to life!

Ready to supercharge your application's engagement? Start integrating Firebase Push Notifications in both your Angular frontend and Node.js backend today! Dive into the provided step-by-step examples and witness the transformation as your applications come alive with real-time communication. Start enhancing your user experience today!

--

--

Saunak Surani
Widle Studio LLP

Passionate about technology, design, startups, and personal development. Bringing ideas to life at https://widle.studio