如何使用JavaScript删除加载视图中的所有Chrome通知?
这是我在Google Chrome中显示通知的代码 如何在代码中关闭通知如何使用JavaScript删除加载视图中的所有Chrome通知?,javascript,google-chrome,notifications,Javascript,Google Chrome,Notifications,这是我在Google Chrome中显示通知的代码 如何在代码中关闭通知 document.addEventListener('DOMContentLoaded',function(){ 如果(!通知){ 警报('您的浏览器中没有桌面通知。请尝试使用Chromium'); 返回; } if(Notification.permission!=“已授予”) Notification.requestPermission(); }); 函数notifyMe(){ if(Notification.perm
document.addEventListener('DOMContentLoaded',function(){
如果(!通知){
警报('您的浏览器中没有桌面通知。请尝试使用Chromium');
返回;
}
if(Notification.permission!=“已授予”)
Notification.requestPermission();
});
函数notifyMe(){
if(Notification.permission!=“已授予”)
Notification.requestPermission();
否则{
var通知=新通知('测试'{
图标:'http://cdn.sstatic.net/stackexchange/img/logos/so/so-icon.png',
正文:“测试”,
});
notification.onclick=function(){
窗口打开(“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;
var notify=[];
对于(var i=0;我不确定您在这里的意思。用户是否将关闭通知-如果保存句柄var w=window,则可以关闭窗口。打开(“http://stackoverflow.com/a/13328397/1269037“,“_blank”);
使用w.close();
除非加载其他原点会阻止您再次访问该窗口