如何使用JavaScript删除加载视图中的所有Chrome通知? [英] How can remove all Chrome notifications in load view with JavaScript?

查看:35
本文介绍了如何使用JavaScript删除加载视图中的所有Chrome通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码,用于在Google Chrome浏览器中显示通知.

This is my code to show notification in Google Chrome.

如何关闭代码中的通知?

How can I close notification in code?

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()方法,您只需将它们推入数组并调用在关闭窗口之前,在每个窗口上都关闭()

It's simple, every notification object has close() method you need to just push them on to an array and call close() on each one of them before window 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秒后说的内容.

You can also associate removeAllNotifys() on some button click to clear all notification or use setTimeout to remove them say after 2 seconds .

这篇关于如何使用JavaScript删除加载视图中的所有Chrome通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆