我怎么能使用上拉通知显示桌面警报?

问题描述 投票:-2回答:1

如何显示从浏览器,Firefox或IE桌面通知时,浏览器关闭或打开?有没有办法做到这一点使用上拉通知方法?

google-chrome internet-explorer firefox notifications microsoft-edge
1个回答
0
投票

你可以尝试检查的通知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

© www.soinside.com 2019 - 2024. All rights reserved.