每天在特定时间显示Chrome桌面通知

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

我想编写一个扩展程序,该扩展程序每天在指定时间显示桌面通知.快速浏览Chrome API,看来唯一的方法是:

I'd like to write an extension that displays a desktop notification every day at a specified time. Having a quick look through the Chrome APIs, it seems like the only way to do this would be to:

  • 为我的扩展程序创建背景页面,
  • 以足够低的分辨率使用setInterval()以免给CPU造成负担(即使5分钟也可以),
  • 间隔触发时,请检查当前时间是否晚于所需时间,
  • 确保今天尚未向用户显示通知.

(最后一步的详细信息与我的问题无关,只是为了表明我意识到我需要防止通知的拍打"而已.)

(The details of the last step are irrelevant to my question, just put in to show I realize I need to prevent "flapping" of the notice).

这似乎是间接的,而且可能很昂贵;有没有办法解决?是否需要背景页面?

This seems rather indirect and potentially expensive though; is there any way around this? Is the background page needed?

我想我可以只调用setTimeout()并只触发一次事件(通过计算现在和所需时间之间的时间),然后在显示通知后再次调用它.出于某种原因,听起来更脆弱",尽管我不确定为什么...

I suppose I could just call setTimeout() and only fire the event once (by calculating how long between now & desired time), then call it again after the notification is shown. For some reason that sounds more "brittle", though I'm not sure why...

推荐答案

我认为您希望后台页面能够平稳地执行此操作.您不能使用内容脚本,因为您需要保留状态"/计时器.

I think you will want the background page to do this smoothly. You can't use a content script because you need to keep the "state"/timer.

因此,当第一次加载后台页面(浏览器启动)时,您可以计算出当前时间和下一个通知时间的偏移量,并将Interval设置为该确切间隔.这样,您将不需要每五分钟轮询一次和/或如果显示了消息就可以进行锻炼.您只需在所需的确切时间显示它即可.这必须比轮询更有效,更有效和更清洁.收到通知后,您只需重新设置间隔即可.

So when background page first loads (browser start) you work out the current time and the offset to the next notification time and setInterval to that exact interval. That way you won't need to poll every five minutes and/or work out if you've shown the message. You simply show it at the exact time required. This has to be far more efficient, effective and cleaner than polling. At notification you just reset the interval again.

此处有一些示例函数:

setTimeout但在给定时间内

通过阅读以上文章并通过在网上快速搜索,您似乎可以毫无问题地调用setInterval,例如每天一次.卡尔文建议25天!

From reading the above post and from a quick search on the net it appears that you should have no problem calling setInterval for an interval such as once a day. Calvin suggests 25 days!

这就是我要采取的方式.

That is how I would approach it.

由于发布了一个突然出现的事情,如果PC休眠了n个小时会发生什么?我需要自己为一个类似的项目进行测试,因此一旦有机会对其进行测试,我将进行更新.

Since posting one thing that has sprung to mind is what happens if a PC gets hibernated for n hours? I need to test this myself for a similar project so I will update once I've had a chance to test this out.

更多推荐

每天在特定时间显示Chrome桌面通知

本文发布于:2023-11-25 18:44:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1630906.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:桌面   通知   时间   在特定   Chrome

发布评论

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

>www.elefans.com

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