How to Show Notification in Javascript using Notification API
In this tutorial, you will learn how to show notification in javascript using Notification API. This is a great feature if you just want to greet your website users with a nice greeting message in the notification or promote some sort of product by providing a discount coupon in the notification message.
Any user who visits your website for the first time will be asked to grant permission so that he can receive notifications. A user can accept or decline the permission. It will only happen once so next time when that user will visit your website you can show him a notification right away.
We are going to cover only the basic usage of Notification API. The real power comes when you can send such notifications, also known as web push notifications, through a server or third-party service. Such a thing is out of the scope of this tutorial. You can google to learn more about web push notifications.
I recommend you to check out MDN docs for Notification API to learn more about it.
In the following example, we will show the notification to the user upon click of a button. Please have a look over the code example and the steps given below.
HTML & CSS
- We have 2 elements in the HTML file (
div
andbutton
). Thediv
element is just a wrapper for thebutton
element. - The
innerText
for thebutton
element is“Show Notification”
. - We are using
style
attribute with bothdiv
andbutton
elements to apply some basic styles. - We have also included our javascript file
script.js
with ascript
tag at the bottom.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <div style="margin-top: 20px; text-align: center;"> <button style="padding: 10px 5px">Show Notification</button> </div> <script src="script.js"></script> </body> </html>
Javascript
- We have selected the
button
element usingdocument.querySelector()
method and stored it in thebutton
variable. - We have attached a
click
event listener to thebutton
element. - We are checking whether a browser supports Notification API or not by using
if
statement andNotification
property of thewindow
object. - We are requesting permission using
requestPermission()
method and then executingshowNotication()
method. - The
showNotification()
method haspermission
parameter which returns permission status. - We are using the
Notification
constructor to create an instance. It will take 2 parameters, the title of notification and an options object. Thebody
will be the message in the notification and theicon
will be the image that will be displayed on the right-hand side. I already have icon.png in the local directory, so just replace it with your image. - In the
onclick
event handler function, we are opening a website when somebody clicks on the notification. You can usewindow.open
orwindow.location.href
to open a website in javascript.
let button = document.querySelector('button'); button.addEventListener('click', () => { if(!window.Notification) return; Notification .requestPermission() .then(showNotification) }); function showNotification(permission){ if(permission !== 'granted') return; let notification = new Notification('My Title', { body:"Hi, how are you today?", icon:'icon.png' }) notification.onclick = () => { // window.open('https://google.com') window.location.href= "https://www.google.com" } }