Уведомления Webkit на нескольких вкладках

Я использую Уведомления WebKit для моего приложения. Скажите, если я использую этот код:

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: для полного кода, пожалуйста, смотрите это:невозможно показать уведомления на рабочем столе с помощью Google Chrome.

мой вопрос в том, что, если у меня есть более одной вкладки открыт?

скажите, если это будет уволен, когда новый комментарий появится на моем приложении. Что, если у меня открыто больше одной вкладки? Будет ли это генерировать много уведомлений? Скажи, у меня есть!--1--> вкладки открываются,и я получаю два уведомления. Сколько уведомлений будет создано,20 - 30?

Если это так, как предотвратить создание одного уведомления несколько раз для каждой открытой вкладки?

2 ответов


подробное объяснение тегов уведомлений, так что только последний появляется доступен на сайте MDN docs

выдержка кода [на всякий случай, если документы идут вниз]

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

вам просто нужно указать опцию " tag " для уведомления. Уведомления с одинаковым значением в теге отображаются только один раз, даже если открыто много вкладок.

например:

var notification = new Notification('Hey!', {
    body : 'So nice to hear from you',
    tag : 'greeting-notify',
    icon : 'https://mysite.com/my_funny_icon.png'
});