Send Push Notification to the browser using JavaScript

by vivek07mishra

HTML

<h1>
Push Notification Demo
</h1>
<input type="button" id="btnPush" value="Send Notification" />

CSS

body {
  font-family: Calibri;
  text-align: center;
}

input {
  font-size: 18px;
}

JavaScript

$(document).ready(function() {
  $('#btnPush').click(function() {
    sendNotification('Update', 'This is a Nuance test notification', 'https:/www.nuance.com/healthcare.html');
  })


  function sendNotification(title, message, url) {
    //First, check notification is supported or not in your browser!
    if (!Notification) {
      console.log('Push notifications are not supported in your browser..');
      return;
    }

    //Ask for permission from user 
    if (Notification.permission !== "granted") {
      Notification.requestPermission();
    } else {
      var notification = new Notification(title, {
        icon: 'https://cdn1.iconfinder.com/data/icons/twitter-ui-colored/48/JD-24-128.png',
        body: message,
      });

      // Remove the notification when clicked and open the URL.
      notification.onclick = function() {
        window.open(url);
      };

      // Callback function when the notification is closed.
      notification.onclose = function() {
        console.log('Notification closed');
      };
    }
  }
})