Chrome扩展桌面通知在休眠时有效

编程入门 行业动态 更新时间:2024-10-27 15:27:47
本文介绍了Chrome扩展桌面通知在休眠时有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我做了一个桌面通知,每隔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来检测浏览器是否处于活动状态并触发您的通知。我猜你可以使用一个可配置的时间间隔来查询状态并阻止你的通知。

参考
  • Chrome.idle
编辑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
  • Chrome.idle
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扩展桌面通知在休眠时有效

本文发布于:2023-11-13 21:36:15,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:桌面   通知   Chrome

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!