这是我在 Google Chrome 中显示通知的代码。
如何在代码中关闭通知?
document.addEventListener('DOMContentLoaded', function() {
if (!Notification) {
alert('Desktop notifications not available in your browser. Try Chromium.');
return;
}
if (Notification.permission !== "granted")
Notification.requestPermission();
});
function notifyMe() {
if (Notification.permission !== "granted")
Notification.requestPermission();
else {
var notification = new Notification('test', {
icon: 'http://cdn.sstatic.net/stackexchange/img/logos/so/so-icon.png',
body: "test",
});
notification.onclick = function() {
window.open("http://stackoverflow.com/a/13328397/1269037");
};
}
}
最佳答案
很简单,每个通知对象都有 close()
方法,您只需将它们推送到数组并在窗口之前对每个通知对象调用 close()
关闭
var notify=[];
for(var i=0; i<=4;i++){
var notification = new Notification('test', {
icon: 'http://cdn.sstatic.net/stackexchange/img/logos/so/so-icon.png',
body: "test"+i
}); //create some notifications
notify.push(notification);
}
function removeAllNotifys()
{
for(var i=0; i<notify.length;i++){
notify[i].close(); //remove them all
}
}
window.onbeforeunload = removeAllNotifys;
您还可以在单击某个按钮时关联 removeAllNotifys()
以清除所有通知,或使用 setTimeout 在 2 秒后删除它们。
关于javascript - 如何使用 JavaScript 删除加载 View 中的所有 Chrome 通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41223068/