Push notification firebase combines reactjs and nodejs

Tram Ho

1. Opening:

Hello everyone today I will show you how to push notification firebase with reactjs and nodejs. No more wandering, let’s do it together

2. Create reactjs and nodejs project projects

Note: Your computer must have nodejs pre-installed before creating reactjs and nodejs projects, if anyone has not installed access to the link: https://nodejs.org/en

1. Create reactjs . project

To create a reactjs project, run the following command:

npx create-react-app reactjs-firebase

Then you run the following command to install the necessary packages for the project:

npm install firebase --save

2. Create a nodejs . project

To create a nodejs project, run the following command:

npm init

Then you run the following command to install the necessary packages for the project:

npm install fcm-notification fcm-node express --save

3. Configure firebase

1. Firebase login

We will visit this link https://firebase.google.com/ to register or login

image.png

You click on the create a project button as shown above:

image.png

Next we enter an arbitrary name and click continue

image.png

Continue clicking continue

image.png

We click on create a new account and enter an arbitrary name as shown below

image.png

We will choose like the image below and click Create project

image.png

Creating the project we will have to wait for a while

image.png

2. Create a firebase project for the web

After the notification project is created, click on the continue button, then it will go to the management page and now we will click the button as shown in the image below to register the project for the web.

image.png

You will enter your project name:

image.png

Then click on the Register app button, you will see a manual page open:

image.png

Copy the code above and save it somewhere for us to use

4. Configuration

1. Configure firebase for Reactjs

We go to the reactjs project just created above and create a file called firebase-messaging-sw.js located in the public folder

and edit firebaseConfig you just created in the previous step

Next in the src folder you create the file firebase.js and copy the code below

Then also edit firebaseConfig

Now we will get vapidKey

Go back to the firebase management page and click on the tab as shown in the image below

image.png

Then select the cloud messaging tab and scroll down to the bottom and click the Generate key pair button to generate the key, then copy the newly created key and replace the value for vapidKey

image.png

Next, copy the code below into the src/App.js folder

We start the project with the command npm start and go to http://localhost:3000/ . And allow the browser to push notifications like the image below:

image.png

Now you open the browser’s console window and see if there will be a token string for the client:

image.png

Copy the string like the picture I circled in red and go back to the firebase management page and select the tab as shown below

image.png

You click the button Create your first campaign to create a test message

image.png

Next, you set Notification title and Notification text :

image.png

Then click the send test message button on the right. It will display a popup where you paste the token you just copied the console window into, then press the + sign and finally press button test .

image.png

Great to be done after so many steps finally the announcement was pushed out

image.png

The reactjs setup step is done, continue to the nodejs setup step

2. Configure firebase for Nodejs

In the nodejs folder you create the file index.js and copy the code below

Then you go back to the firebase page and select the tab as shown below:

image.png

You click on generate new button Generate new private key to generate the key then save it in the nodejs folder

image.png

and replace your_path_key in the code at the top with the path where your key just downloaded:

image.png

Continue to select the tab as shown in the picture and click the button Manage API in Google Cloud Console to switch to the google cloud management page to enable firebase

image.png

After you enable it, reload the firebase management page and you will see server key section

image.png

Copy and paste it instead of private_key in fcm = new FCM("private_key");

The last step you go back to the browser http://localhost:3000/ , and open the browser’s console window and see a token string for the client:

image.png

copy the above string and replace token_client located in

Ok after so many steps, now we are done, let’s check the results. Now we will use postman to test. Please fill in the postman content like me

image.png

After using postman to send you will see the message has been pushed

image.png

5. Conclusion

This friend is also quite long, maybe when I write there will be many shortcomings or not fully describe the idea, hope everyone can give me suggestions to improve the quality of the article. I would like to thank everyone for following and supporting me

Share the news now

Source : Viblo