我正在尝试通过Chrome创建Web通知,我已经成功地接收到了通知。
问题在于当我点击通知时它不会消失,请帮助我在点击/打开通知时关闭它。
目前我有以下内容:
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>