单击关闭 chrome 通知
close chrome notification on click
我正在尝试通过 chrome 创建网络通知
我收到了通知并且工作正常
问题是当我点击通知时它没有消失
请帮助我在 clicked/opened
时关闭通知
我目前有
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();
};
我正在尝试通过 chrome 创建网络通知 我收到了通知并且工作正常 问题是当我点击通知时它没有消失 请帮助我在 clicked/opened
时关闭通知我目前有
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();
};