Chrome扩展桌面通知在休眠时有效 [英] Chrome Extension Desktop Notification Works On Sleep
问题描述
我做了一个桌面通知,每隔1分钟就会显示一个通知。 10秒后,自行关闭。
我去吃午饭,然后电脑进入睡眠状态。当我回来时,我唤醒了我的电脑,然后很多通知开始了。
我该如何处理这个问题?我想如果电脑睡觉它不应该显示通知。我如何控制它?
Background.js
function show(){
var notification = webkitNotifications.createHTMLNotification(
'notification.html'
);
notification.show();
}
//有条件地初始化选项。
if(!localStorage.isInitialized){
localStorage.isActivated = true; //显示激活。
localStorage.frequency = 1; //显示频率,以分钟为单位。
localStorage.isInitialized = true; //选项初始化。
}
if(window.webkitNotifications){
//激活时,以显示频率显示通知。
if(JSON.parse(localStorage.isActivated)){show(); }
var interval = 0; //显示时间间隔,以分钟为单位。
setInterval(function(){
interval ++;
chrome.idle.queryState(15,state);
if(localStorage.frequency< =间隔&&状态==活动)
{show();
interval = 0;
}
},60000);
$ b $ / code>
code> Chrome.idle API来检测浏览器是否处于活动状态并触发您的通知。我猜你可以使用一个可配置的时间间隔来查询状态并阻止你的通知。
参考
编辑1
代码中的状态是一个回调函数,不是字符串!此代码
setInterval(function(){
chrome.idle.queryState(15,state); $ b $ (localStorage.frequency< = interval&& state ==active){
show();
interval = 0;
}
},60000);
至
setInterval(function(){
chrome.idle.queryState(15,function(state){
if(localStorage.frequency< = interval&& state ==active ){
show();
interval = 0;
}
});
},60000);
I made a Desktop Notification it shows a notification every 1 minute. After 10 second, closes itself.
I gone for a lunch , then computer goes to sleep. When i was back i wake my computer then lots of notification starts.
How can i handle that problem? I want if computer sleeps it shouldnt show notification. How can i control it?
Background.js
function show() {
var notification = webkitNotifications.createHTMLNotification(
'notification.html'
);
notification.show();
}
// Conditionally initialize the options.
if (!localStorage.isInitialized) {
localStorage.isActivated = true; // The display activation.
localStorage.frequency = 1; // The display frequency, in minutes.
localStorage.isInitialized = true; // The option initialization.
}
if (window.webkitNotifications) {
// While activated, show notifications at the display frequency.
if (JSON.parse(localStorage.isActivated)) { show(); }
var interval = 0; // The display interval, in minutes.
setInterval(function() {
interval++;
chrome.idle.queryState(15, state);
if(localStorage.frequency <= interval && state=="active")
{ show();
interval = 0;
}
}, 60000);
}
Use Chrome.idle
API to detect whether browser is active or not and trigger your notifications. I guess you can use a configurable interval to query state and hold off your notifications.
Reference
EDIT 1
The state in your code is a callback function, not a string!, So change this code
setInterval(function () {
chrome.idle.queryState(15, state);
if (localStorage.frequency <= interval && state == "active") {
show();
interval = 0;
}
}, 60000);
to
setInterval(function () {
chrome.idle.queryState(15, function (state) {
if (localStorage.frequency <= interval && state == "active") {
show();
interval = 0;
}
});
}, 60000);
这篇关于Chrome扩展桌面通知在休眠时有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!