简体   繁体   English

延迟HTML5通知?

[英]Delay an HTML5 notification?

I've been looking at the state of HTML notifications and service workers, and was wondering - is it possible to show a notification on a delay? 我一直在查看HTML通知和服务人员的状态,并且想知道-是否可以显示延迟通知? Basically, I would like to be able to say "remind me in 30 minutes" (or whatever), then push a notification to the user 30 minutes later. 基本上,我想说“在30分钟内提醒我”(或其他内容),然后在30分钟后向用户发送通知。 That could be scheduled immediately, but I'm not seeing any functionality that allows it. 可以立即安排,但我没有看到任何允许它的功能。

Am I missing something or is it impossible in the current state of (particularly) Chrome APIs? 我会丢失某些东西吗?或者在当前状态下(尤其是Chrome API),这是不可能的吗?

This is possible but not straightforward with service workers, at least in their present form. 对于服务工作者,这是可能的,但并非一帆风顺,至少在目前的形式上如此。 It's not straightforward because a service worker can't keep itself awake for half an hour or wake itself up with a setTimeout or setInterval . 这并不简单,因为服务工作者无法将自己保持半小时的唤醒状态,也无法使用setTimeoutsetInterval唤醒自身。 The browser will just shut the worker down and will keep no record of any timeouts or intervals. 浏览器只会关闭工作进程,并且不会保留任何超时或间隔的记录。 You could wake it up with a message from an open tab, but you said that you don't want to have to have to keep an open tab, and if you assume an open tab then why even bother with the service worker anyway? 您可以通过一个打开的选项卡中的一条消息将其唤醒,但是您说不想保留一个打开的选项卡,如果您假设使用了打开的选项卡,那为什么还要打扰服务人员呢? As Jeff Posnick suggested in a comment, you could perhaps eventually use the Sync or PeriodicSync APIs, but, as he also points out, they aren't implemented yet and aren't really intended for this anyway. 正如Jeff Posnick在评论中建议的那样,您也许最终可以使用Sync或PeriodicSync API,但是,正如他还指出的那样,它们尚未实现,也并非真的打算用于此目的。

You can accomplish what you want in current versions of Chrome using the Push API , but you'll have to include the server in the loop and set yourself up with a push notification service (ie GCM). 可以使用Push API完成当前版本的Chrome中所需的操作,但必须将服务器包含在循环中,并使用推送通知服务(即GCM)进行设置。 Here's how it would work, roughly: 大致如下:

  • When you decide to delay a notification, let the server know about it 当您决定延迟通知时,请让服务器知道
  • After a delay, the server sends out a push message for your user 延迟后,服务器会向您的用户发送推送消息
  • The service worker is woken up in response to the push and creates a new notification 服务人员响应该推送而醒来并创建新的通知

This last part will be a hassle, because currently you can't actually send any kind of payload with a push, so your service worker will need some way of figuring out what the notification is supposed to be. 最后一部分很麻烦,因为当前您实际上无法通过推送发送任何类型的有效负载,因此您的服务人员将需要某种方式来弄清楚通知的内容。 Maybe the server has a list of snoozed notifications and the service worker can get it from there, or maybe you saved them in IndexedDB. 也许服务器有一个被延后的通知列表,服务工作者可以从那里获取通知,或者您将它们保存在IndexedDB中。

Adapted from https://developer.cdn.mozilla.net/media/uploads/demos/e/l/elfoxero/c17223c414d8ddafb7808972b5617d9e/html5-notifications_1400214081_demo_package/ : 改编自https://developer.cdn.mozilla.net/media/uploads/demos/e/l/elfoxero/c17223c414d8ddafb7808972b5617d9e/html5-notifications_1400214081_demo_package/

<script>
    var Notification = window.Notification || window.mozNotification || window.webkitNotification;

    function show() {
        window.setTimeout(function () {
            var instance = new Notification("Hello World!");
        }, 5000);

        return false;
    }
</script>

<a href="#" onclick="return show()">Notify me!</a>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM