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.

Send Browser Notifications from Website

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 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.  We will create following files:

  1. index.html: Holds the main web page elements which will initiate notifications.
  2. javascript.js: Which will check if browser supports notifications then trigger notifications at a specific interval.
  3. style.css: Will contain document styles.


<!DOCTYPE html>
        <title>Send Browser Notification from Website - Demo</title>
        <meta content="text/html; charset=UTF-8" http-equiv="Content-Type"/>
        <script type="text/javascript" src="js/javascript.js"></script>
        <link rel="stylesheet" href="css/style.css" />
        <div class="main-container">
            <div class="section">
                <button type="button" class="btn btn-green show-notification">Show Notification</button>
            <div class="popup-chip hide">
                <span class="close" data-dismiss="popup-chip">&times;</span>
                Your browser does not support notifications

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 that the browser doesn't support notifications. 

The send_notifications will show the notification after every five minutes and increment the counter which is the pointer of current of notification being displayed from notifications object. We will current notification to display from notifications variable based on counter.


var count = 0;
var icon = "";
window.addEventListener("load", function () {
// Initiate notification on page load
document.querySelector("span.close").onclick = function () {
document.querySelector(".show-notification").onclick = function () {
// Initiate notification on button click

// Set notification settings
var notifications = {
0: {
title: "Notification 1",
body: "Notification message body 1",
url: ""
1: {
title: "Notification 2",
body: "Notification message body 2",
url: ""
2: {
title: "Notification 3",
body: "Notification message body 3",
url: ""
3: {
title: "Notification 4",
body: "Notification message body 4",
url: ""

// Function to initiate notification
function init_notification() {
// Check if browser allows notificatons
if ("Notification" in window) {
// If notifications are allowed in browser request permission
Notification.requestPermission(function (permission) {
if (permission === "granted") {
// If permission is granted show notification
} else {

// Function to show notification
function send_notification(notification) {
if (Notification.permission === "granted") {
if (count >= (Object.keys(notifications).length)) {
count = 0;
var notes = new Notification(notification.title, {icon: icon, body: notification.body});
notes.onclick = function () {

// Show notification every five minutes
window.setInterval(function () {
}, (60000 * 5));


    box-sizing: border-box;
    padding: 15px;
    max-width: 1024px;
    margin: 0px auto;
    display: inline-block;
    padding: 5px 10px;
    border: 1px solid #ddd;
    background: #360;
    color: #fff !important;
    max-width: 300px;
    background: #222;
    color: #fff;
    padding: 10px;
    box-shadow: 0px 0px 10px rgba(100,100,100,0.5);
    position: fixed;
    bottom: 10px;
    left: calc((100% - 300px)/2);
    text-align: center;
    animation: moveUp .5s;
    position: absolute;
    top: 2px;
    right: 5px;
    font-size: 20px;
    line-height: 20px;
    cursor: pointer;
    display: none;
@keyframes moveUp{
        bottom: -50%;
        bottom: 10px;