html [js-chrome桌面通知]浏览器通知#js

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了html [js-chrome桌面通知]浏览器通知#js相关的知识,希望对你有一定的参考价值。

<!--
notifications for Chrome, Firefox, Opera and Safari
other solution use lib: https://github.com/alexgibson/notify.js/
-->
<button onclick="notifyMe()">Notify me!</button>
<script>
// request permission on page load
document.addEventListener('DOMContentLoaded', function () {
  if (!Notification || !("Notification" in window)) {
    alert('Desktop notifications not available in your browser. Try Chromium.'); 
    return;
  }

  if (Notification.permission !== "granted")
    Notification.requestPermission();
});

function notifyMe() {
  //grant notification in first time
  if (Notification.permission !== "granted") {	//or !== 'denied'
    Notification.requestPermission();
  	
    //with callback
    Notification.requestPermission(function (permission) {
      // Whatever the user answers, we make sure we store the information
      if(!('permission' in Notification)) {
        Notification.permission = permission;
      }
      
      // If the user is okay, let's create a notification
      if (permission === "granted") {
        var notification = new Notification("Hi there!");
      }
    });
  }
  else {
    //## build notification popup
    var notification = new Notification('Notification title', {
      icon: 'http://cdn.sstatic.net/stackexchange/img/logos/so/so-icon.png',
      body: "Hey there! You've been notified!",
    });
    //simple
    var notification = new Notification("Hi there!");
    
	//## click on notification
    notification.onclick = function () {
      window.open("http://stackoverflow.com/a/13328397/1269037");      
    };

  }

}
</script>

以上是关于html [js-chrome桌面通知]浏览器通知#js的主要内容,如果未能解决你的问题,请参考以下文章

如何正确设置浏览器创建的桌面通知的无限超时

浏览器发起桌面通知Notification.requestPermission

用于桌面的异地推送通知

如何正确设置浏览器创建的桌面通知的关闭超时

Chrome 以外的浏览器的桌面通知 API?

如何让 QWebView 显示桌面通知?