我正在尝试通过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>
您可以使用Notification.close()
。
在.onclick
上,只需运行notify.close()
notify.onclick = function() {
window.location = "https://www.google.com/";
notify.close();
};