How can remove all Chrome notifications in load view with JavaScript?

后端 未结 1 701
野的像风
野的像风 2021-01-21 01:32

This is my code to show notification in Google Chrome.

How can I close notification in code?

document.addEventList         


        
相关标签:
1条回答
  • 2021-01-21 02:12

    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; 
    

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

    0 讨论(0)
提交回复
热议问题