【问题标题】:close chrome notification on click单击时关闭 chrome 通知
【发布时间】:2018-05-26 16:30:20
【问题描述】:

我正在尝试通过 chrome 创建网络通知 我正在收到通知并且工作正常 问题是当我点击通知时它并没有消失 单击/打开时请帮助我关闭通知

我现在有

let dnperm = document.getElementById("dnperm");
let dntrigger = document.getElementById("dntrigger");

dnperm.addEventListener("click", function(e) {
  e.preventDefault();

  if(!window.Notification) {
    alert("Sorry, Notifications are not supported!");
  } else {
    Notification.requestPermission(function(p) {
      if (p === "denied") {
        alert("You Denied");
      } else if (p === "granted") {
        alert("You Allowed");
      }
    });
  }
});

dntrigger.addEventListener("click", function(e) {
  let notify;
  e.preventDefault();
  if (Notification.permission ==="default") {
    alert("Allow Notifications First!");
  } else {
    notify = new Notification("New Message!", {
                     	"body": "Hello",
                     	"icon": "favicon.png",
                     	"tag": "123456",
                     	"image": "img/legendary.jpg",
    });

    notify.onclick = function() {
      window.location = "https://www.google.com/";
    };
  }
});
<a href="#" id="dnperm">Notifications</a>
<a href="#" id="dntrigger">Trigger</a>

【问题讨论】:

    标签: javascript google-chrome push-notification web-push web-notifications


    【解决方案1】:

    您可以使用Notification.close() 来做到这一点。

    .onclick 上,只需运行notify.close()

    notify.onclick = function() {
      window.location = "https://www.google.com/";
      notify.close();
    };
    

    【讨论】:

      猜你喜欢
      • 2015-11-09
      • 2018-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-26
      • 1970-01-01
      • 1970-01-01
      • 2014-08-23
      相关资源
      最近更新 更多