如何自动关闭通知窗口 [英] How to close a notification window automatically
问题描述
你好我正在显示一个基于服务器文件输出的通知。服务器文件的输出正在检查一段时间间隔5分钟。现在我需要的是,当输出在任何时候都发生变化时,通知应该在那个时候自动关闭。我想表达的是,如果服务器文件的输出为0,则通知每5分钟显示一次。如果输出为1,则通知将不会再显示。我的代码问题是,除非手动关闭通知,否则即使输出为1,也不会自动关闭。任何人都请帮助我自动关闭通知。
这里是我的background.js
var myNotificationID = null;
,当你实际上是指字符串
var oldChromeVersion =!chrome.runtime;
函数getGmailUrl(){
返回http://calpinemate.com/;
}
函数isGmailUrl(url){
return url.indexOf(getGmailUrl())== 0;
$ b 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
var tab = tabs [0]; $ b $ (至少一个)Gmail标签:+ tab.url);
$ b console.log(聚焦和刷新次数...);
chrome.tabs.update(tab.id,{active:true});
updateIcon();
}
else {
console.log(找不到Gmail标签。创建一个...);
chrome.tabs.create({url:getGmailUrl()}) ;
updateIcon();
}
});
}
});
函数onInit(){
console.log('onInit');
updateIcon();
if(!oldChromeVersion){
chrome.alarms.create('watchdog',{periodInMinutes:5});
$ b function onAlarm(alarm){
console.log('有闹钟',闹钟);
if(alarm&& alarm.name =='watchdog'){
onWatchdog();
}
else {
updateIcon();
函数onWatchdog(){
chrome.alarms.get('refresh',function(alarm){
if(alarm){
console.log('Refresh alarm exists。Yay。') ;
else {
console.log('Refresh alarm does not exist!''+
'现在刷新并重新安排。');
updateIcon();
}
});
$ b if(oldChromeVersion){
updateIcon();
onInit();
}
else {
chrome.runtime.onInstalled.addListener(onInit);
chrome.alarms.onAlarm.addListener(onAlarm);
$ b函数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。状态== 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); //这是行不通的
}
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',
消息:'请标出您的出席!',
按钮:[{title:'Mark',
iconUrl:'/t.jpg.jpg'
},{title:'忽略' ,
iconUrl:'/cross.jpg'}],
priority:0},
function(id){myNotificationID = id;}
);
chrome.notifications.onButtonClicked.addListener(函数(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 {
//处理错误
alert (错误:状态码+ req.status);
}
}
});
var url = urlPrefix + encodeURIComponent(localStorage.username)+ urlSuffix;
req.open(GET,url);
req.send(null);
$ / code $ / pre
解决方案
问题:
$ b
在<$中传入一个名为
id1
的未定义变量c $ c> chrome.notifications.clear()'id1'
。
根据 chrome.notifications.clear()
方法,第二个参数(回调函数)不是可选的,但是您不能提供一个。
一个可能的解决方案:
//替换那行:
chrome.notifications.clear(id1); //这不起作用
//用这行代码:
chrome.notifications.clear ('id1',function(){}); //这是完美的工作
//或者用这行:
chrome.notifications.clear(myNotificationID,function(){} ); //这是工作完美tly
//替换那行:
chrome.notifications.clear(id1); //这不起作用
//用这行代码:
chrome.notifications.clear ('id1',function(){}); //这是完美的工作
//或者用这行:
chrome.notifications.clear(myNotificationID,function(){} ); //这是工作完美tly
顺便说一句,您不需要提供你可以自己替换: chrome.notifications.create('id1',...
with : chrome.notifications.create('',...
(然后使用: chrome.notifications.clear(myNotificationID,...
)
Hi I am displaying a notification based on the output of a server file.The output of the server file is being checked for a period of 5 min interval.Now what I need is that when the output changes at any instant, notification should be automatically be closed at that moment.What I am trying to convey that if the output of the server file is 0,the notification is being displayed in every 5 min. If the output is 1, then notification will not be shown anymore.The problem of my code is that unless I close the notification manually it won't be closed automatically even if the output is 1. Anyone please help me to close my notification automatically. here is my 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);
}
Two possible problems:
You pass an undefined variable named
id1
inchrome.notifications.clear()
, when you actually mean the string'id1'
.According to the docs on
chrome.notifications.clear()
method, the second argument (callback function) is not optional, yet you fail to supply one.
One possible solution:
// 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
BTW, you don't need to provide a notification ID yourself.
You can replace: chrome.notifications.create('id1',...
with: chrome.notifications.create('',...
(and then, of course, use: chrome.notifications.clear(myNotificationID,...
)
这篇关于如何自动关闭通知窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!