Web Push Notification in Reactjs 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(<App />, document.getElementById('root'))
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:
We have a platform option in the above image click on the JavaScript platform for web  Now we will get the following page.
Register the app by adding an app nickname.
After registering the app, you will get the following page.
Copy the FirebaseConfig object and add inside the Firebase.js file
Then Firebase.js file should be

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
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 .
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
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
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
After adding the token click on the test button to test the notification to test the push notification
Copy the token and then add into a FCM registration token
You will get the push notification on the client app
The above image showing the notification on google chrome browser on localhost:3000

Conclusion

Now, we have steps to implement the push notification functionality in react js web application using firebase cloud messaging.

subscribe to our newsletter

   
   
Kumarswami Boga
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 restAPI in django-rest-framework.
Related Posts

Leave a Comment

BLE issues in Android 7 and above