Need help?

Web Push Notification In React Js With Firebase

In this blog, we will show how to add a push notification functionality in react js using Firebase. Nowadays, most web applications have notification functionality to get continuous updates and user interaction.

A push/alert message will help the user to get the new updates on the app.

Firebase provides cloud messaging functionality, which helps get alert messages on the client-side.

Here are some basic steps required to implement the push notifications in ReactJS with Firebase.

  1. React project set-up and install a Firebase npm package
  2. Initialize Firebase in the project
  3. Create a project in a Firebase console
  4. Create a Firebase-message-sw.js file
  5. User permissions to get a notification
  6. Testing in Firebase cloud message

React project set-up and install Firebase npm package

If you don’t know about react first, go through the react tutorials, or you can check the best react courses.

Or

Create an app by using the create-react-app following command

React Routing

npm create-react-app push-notification-demo

cd push-notification-demo

Then run the command npm start to run the app.

Now moving to the Firebase initialization in react app.

We require the Firebase library to initialize the Firebase in the created app.

Run the following command

npm install Firebase –save

Initialize Firebase in project

To initialize the Firebase lets create a file Firebase.js inside a src folder

App.js file

import React from 'react';

import ReactDOM from 'react-dom';

import './index.css';

import App from './App';

import DeepLink from './deepLink'

ReactDOM.render(, document.getElementById('root'))

Web Push Notification In Reactjs With Firebase

After creating a Firebase.js in the app, we have to create a project inside a Firebase console

Create project in Firebase console

To create a project in Firebase

Create a project with name – first-cloud-messaging

Now you have a Firebase project with name first-cloud-messaging

Now go to the project overview, You will get the following page:

Web Push Notification In Reactjs With Firebase

We have a platform option in the above image click on the JavaScript platform for web Now we will get the following page.

Web Push Notification In Reactjs With Firebase

Register the app by adding an app nickname.

After registering the app, you will get the following page.

Web Push Notification In Reactjs With Firebase

Copy the FirebaseConfig object and add inside the Firebase.js file

Then Firebase.js file should be

Web Push Notification In Reactjs With Firebase

Create a Firebase-message-sw.js file

Create a Firebase-message-sw.js file inside a public folder inside an app and add the following code

Web Push Notification In Reactjs With Firebase

Add messagingSenderId which is provided by Firebase. Imports all the scripts that are needed to show the notifications when you app is running in the background.

User permission to get notifications

It is a best practice to take permission from the user before the push notification starts.

By using the react lifecycle hook componentDidMount, which is helpful to execute the function after rendering the render function in the class-based component.

The following image shows the app.js file with componentDidMount.

Web Push Notification In Reactjs With Firebase

In the above image we have imported the Firebase.js file to initialize the Firebase in the app. After requesting permission, we will get a token that is printed in the console.

To get the token run the react server by command npm start

Web Push Notification In Reactjs With Firebase

In the above image, we are getting a token. With the help of the token, we can have permission to get the notification from the Firebase.

Testing the Firebase cloud-messaging

For testing the push notification we have to go to the Firebase console.

In the Firebase console go to -> project overview page

Web Push Notification In Reactjs With Firebase

After adding form => click on => Send test message button

Click on the Cloud Messaging -> click to => Send your first message button

You will get pop which needed the FCM registration token which we got in the client app browser console

Web Push Notification In Reactjs With Firebase

After adding the token click on the test button to test the notification to test the react firebase push notification

Copy the token and then add into a FCM registration token

Web Push Notification In Reactjs With Firebase

You will get the push notification on the client app

Web Push Notification In Reactjs With Firebase

The above image showing the notification on google chrome browser on localhost:3000

Want to Hire ReactJS Developer?

Contact Us

Conclusion

Now, we have steps to implement the push notification functionality in react js web application using firebase cloud messaging. Hope my article helps you to understand the process of React firebase push notification. If you have any doubts, write your comment to discuss more.

Read about the blog on Stripe Payment in React Native App Using Firebase Cloud Function.

Subscribe To Our Newsletter

Kumarswami Boga

Tech Expert

Kumarswami Boga is a full-stack web application developer with 1.5 years of experience. He is eager to learn new technologies and passionate about his work. His area of expertise in HTML, CSS, javascript, and frontend frameworks reactjs, angularjs for backend creating rest API in Django-rest-framework.

Leave a Reply

Your email address will not be published. Required fields are marked *