【问题标题】:How can I show desktop alert using pull notification?如何使用拉取通知显示桌面警报?
【发布时间】:2019-02-06 18:26:39
【问题描述】:

当浏览器关闭或打开时,如何显示来自 chrome、firefox 或 IE 的桌面通知?有没有办法使用拉取通知方法来做到这一点?

【问题讨论】:

    标签: google-chrome internet-explorer firefox notifications microsoft-edge


    【解决方案1】:

    您可以尝试检查通知 API。

    通知 API 允许网页控制向最终用户显示系统通知。这些在顶级浏览上下文视口之外,因此即使用户切换选项卡或移动到不同的应用程序也可以显示。该 API 旨在与跨不同平台的现有通知系统兼容。

    示例代码:

    <!doctype html>
    <head>
    <script>
    window.addEventListener('load', function () {
      // At first, let's check if we have permission for notification
      // If not, let's ask for it
      if (window.Notification && Notification.permission !== "granted") {
        Notification.requestPermission(function (status) {
          if (Notification.permission !== status) {
            Notification.permission = status;
          }
        });
      }
    
      var button = document.getElementsByTagName('button')[0];
    
      button.addEventListener('click', function () {
        // If the user agreed to get notified
        // Let's try to send ten notifications
        if (window.Notification && Notification.permission === "granted") {
          var i = 0;
          // Using an interval cause some browsers (including Firefox) are blocking notifications if there are too much in a certain time.
          var interval = window.setInterval(function () {
            // Thanks to the tag, we should only see the "Hi! 9" notification 
            var n = new Notification("Hi! " + i, {tag: 'soManyNotification'});
            if (i++ == 9) {
              window.clearInterval(interval);
            }
          }, 200);
        }
    
        // If the user hasn't told if he wants to be notified or not
        // Note: because of Chrome, we are not sure the permission property
        // is set, therefore it's unsafe to check for the "default" value.
        else if (window.Notification && Notification.permission !== "denied") {
          Notification.requestPermission(function (status) {
            // If the user said okay
            if (status === "granted") {
              var i = 0;
              // Using an interval cause some browsers (including Firefox) are blocking notifications if there are too much in a certain time.
              var interval = window.setInterval(function () {
                // Thanks to the tag, we should only see the "Hi! 9" notification 
                var n = new Notification("Hi! " + i, {tag: 'soManyNotification'});
                if (i++ == 9) {
                  window.clearInterval(interval);
                }
              }, 200);
            }
    
            // Otherwise, we can fallback to a regular modal alert
            else {
              alert("Hi!");
            }
          });
        }
    
        // If the user refuses to get notified
        else {
          // We can fallback to a regular modal alert
          alert("Hi!");
        }
      });
    });
    </script>
    </head>
    <body>
    <button>Notify me!</button>
    </body>
    </html>

    参考资料:

    (1)Notifications API

    (2)Using the Notifications API

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-08-25
      • 2020-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-05
      相关资源
      最近更新 更多