Notifications are a vital part of any app. They show information directly to users, and they're a good way to let users know what's happening in the app.
In a React app, we achieve notification functionality using JavaScript's Notification API, which has a variety of functions to use with different notification-related settings. You will learn many of them in this guide. You'll also learn how to get started with Notification, what the primary functions are of the JavaScript Notification API, and how it works in real-life examples.
The JavaScript Notification API is an interface that allows the browser to throw a notification to engage the website with the user and inform the user about what's happening on the current webpage.
The Notification API consists of several methods, including:
Apart from the above methods, Notification includes several properties:
By using these methods and instance properties, you can use the Notification API effectively to show various information to users.
Before getting started with the Notification API in a React app, make sure it is supported.
Open any components file and paste the following lines of source code.
1componentDidMount() {
2 if (!("Notification" in window)) {
3 console.log("This browser does not support desktop notification");
4 } else {
5 console.log("Notifications are supported");
6 }
7}
The method componentDidMount ()
will check whether the Notification
object of the API is accessible in window scope or not, and based on the status, the console statement gets triggered.
It's easy to use notifications to communicate messages or statuses to users through a pop-up box with elements like text, images, buttons, and so on.
Before showing a notification to a user, you must get permission to reveal notifications on the user's browser screen. To do that, use the lines of source code below to ask for notification access.
1componentDidMount() {
2 if (!("Notification" in window)) {
3 console.log("This browser does not support desktop notification");
4 } else {
5 Notification.requestPermission();
6 }
7}
When the page renders, the location pop-up will appear along with two buttons, Allow and Block.
Once a user allows notifications, they will get all notifications from that web app in the future.
Now open the components and paste the lines of code below.
1import React, { Component } from "react";
2
3class SimpleNotification extends Component {
4 constructor() {
5 super();
6 this.showNotification = this.showNotification.bind(this);
7 }
8
9 componentDidMount() {
10 if (!("Notification" in window)) {
11 console.log("This browser does not support desktop notification");
12 } else {
13 Notification.requestPermission();
14 }
15 }
16
17 showNotification() {
18 new Notification('Hey')
19 }
20
21 render() {
22 return (
23 <div>
24 <button onClick={this.showNotification}>
25 Click to show notification
26 </button>
27 </div>
28 );
29 }
30}
31
32export default SimpleNotification;
In the above example, the button is added with the onclick event this.showNotification()
. When the user clicks on the button, the simple notification class is called.
1showNotification() {
2 new Notification('Hey')
3}
This means that it uses the notification()
class along with one parameter as the body of the notification that shows once it is triggered.
Once you run the example, the output should look like this.
So far in this guide, you've learned the different properties and methods used with the Notification API. You have also gone through a simple notification example that illustrated how a notification shows on a desktop.
Surprisingly, you can also customize notifications with a custom body and a custom image.
The below lines of source code create the notification pop-up with the media element.
1showNotification() {
2 var options = {
3 body: "This is the body of the Notification",
4 icon: "https://images.pexels.com/photos/853168/pexels-photo-853168.jpeg? auto=compress&cs=tinysrgb&dpr=1&w=500",
5 dir: "ltr"
6 };
7 var notification = new Notification("Notification Demo", options);
8}
In the above example, observe that there is an object called options
that contains various object keys such as body
, icon
, and dir
, which decides the pop-up notification's appearance.
Now when you run the above example, the output would be as shown below.
As you can see, the custom body is there as text, and the left side of the notification has the icon you specified as an option and the direction property.
You can also close the notification pop-up programmatically using the in-built close()
function.
Make sure the notification is stored in its reference to some variables. It's better to make the variable as a global variable.
1var notification;
Modify the notification source code.
1showNotification() {
2 var options = {
3 body: "This is the body of the Notification",
4 icon: "https://images.pexels.com/photos/853168/pexels-photo-853168.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500",
5 dir: "ltr"
6 };
7 notification = new Notification("Notification Demo", options);
8}
After modifying the above source code, now you will be able to close the notification like this.
1closeNotification() {
2 notification.close();
3}
The function closeNotification()
would be called on button click event, where notification
is a global variable in which you have defined the reference of the notification.
So, that's how to execute a complete notification mechanism, from creating and customizing the notification to closing it programmatically.
Notifications are easy to execute in any React app using the JavaScript's Notification web API, and you can customize it by using the various properties. In this guide, you got a complete introduction to notifications, including creating a simple notification and modifying its body. I hope this guide was helpful for you.