AskHandle

AskHandle Blog

How to Implement Push Notifications in React: A Comprehensive Guide

October 1, 2025Emily Henderson3 min read

How to Implement Push Notifications in React: A Comprehensive Guide

Push notifications are a key feature for modern web applications. They keep users informed and engaged even when the application is not open. This article outlines how to implement push notifications in a React application using Firebase Cloud Messaging (FCM).

Getting Started

What are push notifications? They are messages sent from a server to the user's device, alerting them of new updates or events. In web applications, push notifications help re-engage users and provide timely information.

Setting Up Firebase

To implement push notifications in your React application, use Firebase Cloud Messaging. Follow these steps:

  • Create a Firebase project in the Firebase console.
  • Register your web application to obtain your Firebase configuration. This includes the API key, messaging sender ID, and app ID.

Integrating Firebase With React

To integrate Firebase with your application, install the Firebase SDK by running:

bash
1npm install firebase

Then, initialize Firebase in your index.js file:

javascript
1import firebase from 'firebase/app';
2import 'firebase/messaging';
3
4const firebaseConfig = {
5  apiKey: 'YOUR_API_KEY',
6  authDomain: 'YOUR_AUTH_DOMAIN',
7  projectId: 'YOUR_PROJECT_ID',
8  storageBucket: 'YOUR_STORAGE_BUCKET',
9  messagingSenderId: 'YOUR_MESSAGING_SENDER_ID',
10  appId: 'YOUR_APP_ID'
11};
12
13firebase.initializeApp(firebaseConfig);
14const messaging = firebase.messaging();

Requesting Permission for Notifications

Users must grant permission to receive notifications. Add the following code to your React component:

javascript
1messaging.requestPermission()
2  .then(() => {
3    console.log('Notification permission granted.');
4    return messaging.getToken();
5  })
6  .then((token) => {
7    console.log('Token:', token);
8  })
9  .catch((error) => {
10    console.log('Error:', error);
11  });

Handling Incoming Notifications

Once permissions are granted, you can handle incoming notifications with this code:

javascript
1messaging.onMessage((payload) => {
2  console.log('Message received.', payload);
3  // Handle the incoming notification
4});

Sending Notifications from Firebase Console

To send push notifications, navigate to Cloud Messaging in the Firebase console. Compose and send notifications to users based on various targeting options.

Handling Notification Clicks

You can also manage notification clicks with an event listener:

javascript
1navigator.serviceWorker.addEventListener('notificationclick', (event) => {
2  const notification = event.notification;
3  notification.close();
4
5  // Handle the notification click
6});

This guide outlines the main steps to implement push notifications in a React application using Firebase Cloud Messaging. Following these steps enhances user engagement and interaction in your web applications. If you have questions, refer to Firebase documentation for additional support.