微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

html5 – 多个选项卡上的Webkit通知

我正在为我的应用程序使用WebKit Notifications.假如我使用此代码
var n = window.webkitNotifications.createNotification(
   'icon.png','New Comment','Praveen commented on your post!'
);
n.onclick = function(x) { window.focus(); this.cancel(); };
n.show();

PS 1:前五行实际上是一行.为了便于阅读,我已经发布了这种方式.

PS 2:有关完整代码,请参阅:Unable to show Desktop Notifications using Google Chrome.

我的问题是,如果我打开了多个标签怎么办?

假设当我的应用程序上出现新评论时,这会被解雇.如果我打开多个标签怎么办?这会产生很多通知吗?说,我打开了10到15个标签,我收到了两个通知.将生成多少通知,20 – 30?

如果是这种情况,如何防止每个打开的选项卡多次生成单个通知

解决方法

标记通知的详细说明只有最后一个出现才可用
on the MDN docs site

代码的摘录[以防万一文档失败]

HTML

<button>Notify me!</button>

JS

window.addEventListener('load',function () {
  // At first,let's check if we have permission for notification
  // If not,let's ask for it
  if (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 (Notification && Notification.permission === "granted") {
      for (var i = 0; i < 10; i++) {
        // Thanks to the tag,we should only see the "Hi! 9" notification
        var n = new Notification("Hi! " + i,{tag: 'soManyNotification'});
      }
    }

    // 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 (Notification && Notification.permission !== "denied") {
      Notification.requestPermission(function (status) {
        if (Notification.permission !== status) {
          Notification.permission = status;
        }

        // If the user said okay
        if (status === "granted") {
          for (var i = 0; i < 10; i++) {
            // Thanks to the tag,we should only see the "Hi! 9" notification
            var n = new Notification("Hi! " + i,{tag: 'soManyNotification'});
          }
        }

        // 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!");
    }
  });
});

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。