Show Desktop Notification Using JavaScript

  1.    <html>    
  2.    <head>    
  3.        <title>javascript - Desktop notification</title>    
  4.        <script>    
  5.            function ShowNotification() {    
  6.                if (!("Notification" in window)) {    
  7.                    console.log("This browser does not support desktop notification");    
  8.                }    
  9.                else if (Notification.permission === "granted") {    
  10.                    var notification = new Notification("Demo title", {    
  11.                        icon: 'Images/end-chat.png',    
  12.                        body: 'This is a simple desktop notification'    
  13.                    });    
  14.                }    
  15.                else if (Notification.permission !== 'denied') {    
  16.                    Notification.requestPermission(function (permission) {    
  17.                        if (!('permission' in Notification)) {    
  18.                            Notification.permission = permission;    
  19.                        }    
  20.                        if (permission === "granted") {    
  21.                            var notification = new Notification("Demo title", {    
  22.                                icon: 'Images/end-chat.png',    
  23.                                body: 'This is a simple desktop notification'    
  24.                            });    
  25.                        }    
  26.                    });    
  27.                }    
  28.            }      
  29.                
  30.        </script>    
  31.    </head>    
  32.    <body>    
  33.        <button onclick="ShowNotification()">Show Notification</button>    
  34.    </body>    
  35.    </html>   
  36.   
  37.   
  38. It works on all major browser if you run this code using valid site url, you can prefer firefox for quick output.