Google chrome extension 如何自动关闭通知窗口

Google chrome extension 如何自动关闭通知窗口,google-chrome-extension,notifications,Google Chrome Extension,Notifications,您好,我正在显示一个基于服务器文件输出的通知。服务器文件的输出每隔5分钟检查一次。现在我需要的是,当输出在任何时刻发生更改时,通知应在该时刻自动关闭。我试图传达的是,如果服务器文件的输出为0,通知每5分钟显示一次。如果输出为1,则通知将不再显示。我的代码的问题是,除非我手动关闭通知,否则即使输出为1,通知也不会自动关闭。任何人请帮助我自动关闭我的通知。 这是我的background.js var myNotificationID = null; var oldChromeVersion = !c

您好,我正在显示一个基于服务器文件输出的通知。服务器文件的输出每隔5分钟检查一次。现在我需要的是,当输出在任何时刻发生更改时,通知应在该时刻自动关闭。我试图传达的是,如果服务器文件的输出为0,通知每5分钟显示一次。如果输出为1,则通知将不再显示。我的代码的问题是,除非我手动关闭通知,否则即使输出为1,通知也不会自动关闭。任何人请帮助我自动关闭我的通知。 这是我的background.js

var myNotificationID = null;
var oldChromeVersion = !chrome.runtime;
function getGmailUrl() {
 return "http://calpinemate.com/";
  }


 function isGmailUrl(url) { 
return url.indexOf(getGmailUrl()) == 0;

 }

 chrome.browserAction.onClicked.addListener(function(tab) {
 if(!localStorage.username){
 chrome.windows.create({url : "userinfo.html",type: "popup", height: 200, width:300 , top :400 , left : 800}); 


    }
    else{
      chrome.tabs.query({

    url: "http://calpinemate.com/*",

   currentWindow: true
    },
    function(tabs) {

    if (tabs.length > 0) {

       var tab = tabs[0];

       console.log("Found (at least one) Gmail tab: " + tab.url);

       console.log("Focusing and refreshing count...");

       chrome.tabs.update(tab.id, { active: true });

       updateIcon();

        }
      else {

       console.log("Could not find Gmail tab. Creating one...");

       chrome.tabs.create({ url: getGmailUrl() });

       updateIcon();

        }
         });
            }
      });



     function onInit() {

       console.log('onInit');

       updateIcon();

         if (!oldChromeVersion) {

         chrome.alarms.create('watchdog', {periodInMinutes:5});

           }

             }




      function onAlarm(alarm) {

         console.log('Got alarm', alarm);

         if (alarm && alarm.name == 'watchdog') {

            onWatchdog();

             } 
          else {

         updateIcon();

           }

          }




      function onWatchdog() {

        chrome.alarms.get('refresh', function(alarm) {

           if (alarm) {

           console.log('Refresh alarm exists. Yay.');

              } 
          else {
       console.log('Refresh alarm doesn\'t exist!? ' +
              'Refreshing now and rescheduling.');

        updateIcon();

           }
          });

          }


      if (oldChromeVersion) {

        updateIcon();

           onInit();

             } 

            else {

       chrome.runtime.onInstalled.addListener(onInit);

       chrome.alarms.onAlarm.addListener(onAlarm);

         }




   function updateIcon(){

    var urlPrefix = 'http://www.calpinemate.com/employees/attendanceStatus/';
    var urlSuffix = '/2';

       var req = new XMLHttpRequest();

      req.addEventListener("readystatechange", function() {

     if (req.readyState == 4) {

       if (req.status == 200) {  
       var item=req.responseText;
       if(item==1){
        chrome.browserAction.setIcon({path:"calpine_logged_in.png"});
        chrome.browserAction.setBadgeBackgroundColor({color:[190, 190, 190, 230]});
        chrome.browserAction.setBadgeText({text:""});  
        chrome.notifications.clear(id1);//this is not working

         }

      else{
       chrome.browserAction.setIcon({path:"calpine_not_logged_in.png"});   
       chrome.browserAction.setBadgeBackgroundColor({color:[190, 190, 190, 230]});           
       chrome.browserAction.setBadgeText({text:""}); 
        chrome.notifications.create(
       'id1',{
       type: 'basic',
       iconUrl: '/calpine_not_logged_in.png',
       title: 'Warning : Attendance',
        message: 'Please mark your Attendance !',
       buttons: [{ title: 'Mark',
                        iconUrl: '/tick.jpg'
                  },{ title: 'Ignore',
                        iconUrl: '/cross.jpg'}],
       priority: 0},
       function(id) { myNotificationID = id;}
      );


  chrome.notifications.onButtonClicked.addListener(function(notifId, btnIdx) {
  if (notifId === myNotificationID) {
    if (btnIdx === 0) {
        window.open("http://www.calpinemate.com/");
    } else if (btnIdx === 1) {
       notification.close();
    }
    }
   });

 chrome.notifications.onClosed.addListener(function() {
  notification.close();
 });


 }

      } 
   else {

        // Handle the error

        alert("ERROR: status code " + req.status);

       }

       }

    });
    var url = urlPrefix + encodeURIComponent(localStorage.username) + urlSuffix;
    req.open("GET", url);

   req.send(null);

   }
两个可能的问题:
  • chrome.notifications.clear()
    中传递一个名为
    id1
    的未定义变量,实际上是指字符串
    'id1'

  • 根据方法上的文档,第二个参数(回调函数)不是可选的,但是您无法提供一个

  • 一种可能的解决办法:
    顺便说一句,您不需要自己提供通知ID。
    您可以替换:
    chrome.notifications.create('id1'),…

    使用:
    chrome.notifications.create(“”,


    (然后,当然,使用:
    chrome.notifications.clear(myNotificationID,

    嗨,我希望我必须使用chrome.notifications.clear或chrome.notifications.getAll()来删除通知。但是谁能给出其中一个的语法?请帮助我
    // Replace that line:
    chrome.notifications.clear(id1);//this is not working
    
    // With this line:
    chrome.notifications.clear('id1', function(){});//this is working perfectly
    
    // Or with this line:
    chrome.notifications.clear(myNotificationID, function(){});//this is working perfectly