【问题标题】:Webkit Notifications on Multiple Tabs多个选项卡上的 Webkit 通知
【发布时间】:2012-10-07 08:27:22
【问题描述】:

我正在为我的应用程序使用 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:完整代码请见:Unable to show Desktop Notifications using Google Chrome

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

说当我的应用上出现新评论时,这是否会被解雇。如果我打开了多个标签怎么办?这会产生很多通知吗?比如说,我打开了10 - 15 选项卡,我收到了两个通知。将生成多少个通知,20 - 30

如果是这样,如何防止为每个打开的标签多次生成单个通知?

【问题讨论】:

  • ddnt 甚至不知道这个功能,大声笑,去研究它!
  • 试试吧,伙计。太棒了! :) 查看我的链接问题,了解我实施它的方式! :)
  • 我也在寻找这个问题的答案。你有什么解决办法吗?
  • 您能找到解决问题的方法吗?我现在在完全相同的地方:( 尝试了答案中的解决方案,但它在萤火虫中出现错误 - ReferenceError: Notification is not defined [Break On This Error] var notification = new Notification('Hey!', {
  • 很遗憾看到这个庞大的 SO 社区中没有人知道解决方案。

标签: html google-chrome webkit notifications html5-notifications


【解决方案1】:

您只需要指定通知的“标签”选项。标签中具有相同值的通知即使打开了许多标签也只会显示一次。

例如:

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

【讨论】:

  • 哦。好的。让我检查一下,让你知道。感谢您的回答。
  • 这不是 webkit 通知,它是 safari 的通知版本,它有一个标记方法,但我注意到他们用一个带有方法的对象定义了它们,而另一个是一个值数组。我也没有找到答案。
  • 这很有用。但它显示闪烁的通知。有什么办法可以防止闪烁?
  • 这在 Firefox 上对我不起作用 添加标签是否真的为您在 safari 中解决了这个问题?
【解决方案2】:

标记通知的详细说明,因此仅显示最后一个可用 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!");
    }
  });
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-05
    • 2023-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多