In Web Applications, you may need to display notifications to the user which do not require the user to be active on your page. The Web Notifications API allows developers to implement this feature.
The Web Notifications API allows web apps to send system-level notifications to the user. These notifications are shown outside the browsing context viewport` and are visible even if the user has moved to another page or app.
It is supported by almost all modern browsers apart from Safari on IOS. Old browsers like Internet Explorer also lack the support for it.
You can quickly check for browser support using the following snippet:
js
if ('Notification' in window) {// Do something with notifications here}
Many web applications utilize this API to send notifications, including Slack, Facebook, and other chat applications. This API is designed to be cross-platform and compatible with existing notification systems.
To display a notification, you need to do two things:
The API provides the Notification.requestPermission()
method which does what it says; asks the user for permission. Once the permission is granted, the choice is saved for at least a whole session.
This method should be triggered explicitly as a result of some user action such as a button click. Here is how you can use the method in your React application.
js
const showNotification = () => {Notification.requestPermission().then(result => {if (result === 'granted') {// Show the notification here}});};// Inside your component<ButtononClick={() => {showNotification();}}>Show Notification</Button>;
The code above will spawn a dialog box asking the user to allow notifications.
Once the user grants permission, you can show the notifications using the Notification()
constructor. Simply create a new Notification
instance, and specify its parameters like title, body, icon, etc. Let's add it to our function.
js
const getPermission = () => {Notification.requestPermission().then(result => {if (result === 'granted') {new Notification('Title', {body: 'This is an example notification.'});}});};
Click the Show Notification button below to try it yourself. Make sure your browser is allowed to show notifications.
It will look like this:
This guide was a brief tour of the Notification API, and we hope that now you can use it in your awesome projects.