Send Browser Notifications from Website
Many web applications use browser push notifications to notify their users. These notifications are useful to send reminders or notify users instantly even if the browser window is not active. In this post we will implement this push notification to be sent after every five minutes.
The Notifications API is an API which allows websites to control the system notifications displayed to end user. These notifications work outside the current viewport which means even if the current page document isn't active or in view it will still send notifications to user.
We will be using Notifications interface of this Notifications API. Notifications are supported by almost all latest browsers. For this post we will create following files:
- index.html: Holds the main web page elements which will initiate notifications.
- javascript.js: Which will check if browser supports notifications then trigger notifications at a specific interval.
- style.css: Will contain document styles.
index.html
<!DOCTYPE html>
<html>
<head>
<title>Send Browser Notification from Website - Demo</title>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type"/>
<script src="js/javascript.js"></script>
<link rel="stylesheet" href="css/style.css" />
</head>
<body>
<div class="container">
<div class="py-4">
<div>
<button type="button" class="btn btn-green show-notification">Show Notification</button>
</div>
<div class="popup hidden">
<span class="close" data-dismiss="popup">×</span>
Your browser does not support notifications
</div>
</div>
</div>
</body>
</html>
We will set four static notifications in an object and show them every five minutes. We will be using two functions init_notification() and send_notification() and an object variable notifications which will hold the notification settings like notification's title,icon and body.
The init_notification will be triggered when document is loaded or for demo purpose when show notification button is clicked. This function will check if the browser supports notifications it will request permission to show notifications, Otherwise it will show a popup to user informing the browser does not support notifications.
The send_notifications will show the notification after every five minutes and increment the counter which is the pointer of current notification being displayed from notifications object. We will select current notification to display from notifications variable based on counter.
javascript.js
let count = 0;
let notifications = [];
let notification_object = {
url: "https://www.codestacked.info",
options: {
icon: "https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh4Zx7iHsdDhwLSlxKsPMw4RGdOX46kfrh8jJsRAaOI4C0Au0q8b3fI_MBLJm30wuz0bwJuPkROLLNBO84l4aQ5dzZHwytmOgR5wVGZmIbCCBnQ24S2C-one18dJAvQA0emV8kETsCPAzOV/s1600/codestacked-logo.png",
}
}
for (let i = 1; i <= 5; i++) {
notifications.push({
...notification_object,
...{
title: "Notification " + i,
options: {
...notification_object.options,
...{body: "Notification " + i}
}
}
});
}
document.addEventListener("DOMContentLoaded", function () {
init_notification();
document.querySelector("[data-dismiss=popup]").addEventListener("click", function () {
this.parentElement.classList.add("hidden");
});
document.querySelector(".show-notification").addEventListener("click", function () {
init_notification();
});
});
function init_notification() {
if ("Notification" in window) {
Notification.requestPermission(function (permission) {
if (permission === "granted") {
if (count >= (Object.keys(notifications).length)) {
count = 0;
}
send_notification(notifications[count]);
count++;
}
});
} else {
document.querySelector(".popup-chip").classList.remove("hidden");
}
}
function send_notification(notification) {
if (Notification.permission === "granted") {
var notes = new Notification(notification.title, notification.options);
notes.onclick = function () {
window.location.assign(notification.url);
}
}
}
window.setInterval(function () {
send_notification(notifications[count]);
}, (60000 * 5));
style.css
*{
box-sizing: border-box;
}
html,body{
margin: 0;
padding: 0;
}
body{
background-color: #f6f6f6;
font-family: "Segoe UI", "Roboto", "Helvetica", sans-serif;
font-size: 15px;
font-weight: normal;
font-style: normal;
}
a{
text-decoration: none;
color: #3778cd;
}
.container{
max-width: 1140px;
width: 100%;
margin-right: auto;
margin-left: auto;
padding-right: 15px;
padding-left: 15px;
}
.py-4{
padding-top: 1rem;
padding-bottom: 1rem;
}
.btn{
display: inline-block;
padding: 5px 10px;
cursor: pointer;
font: inherit;
border-radius: .25rem;
}
.btn-green{
background: #009549;
border: 1px solid #009549;
color: #fff;
}
.popup{
max-width: 300px;
background: #e65442;
color: #fff;
padding: 10px;
position: fixed;
inset: auto 0 10px 0;
margin: 0 auto;
text-align: center;
animation: moveUp .5s;
}
span.close{
font-size: 20px;
line-height: 20px;
cursor: pointer;
}
.hidden{
display: none;
}
@keyframes moveUp{
from{
bottom: -50%;
}
to{
bottom: 10px;
}
}