Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Web通知API-每个时段仅显示一次_Javascript_Html_Notifications_Web Notifications - Fatal编程技术网

Javascript Web通知API-每个时段仅显示一次

Javascript Web通知API-每个时段仅显示一次,javascript,html,notifications,web-notifications,Javascript,Html,Notifications,Web Notifications,我正在编写一个需要发送通知的web应用程序。它每30秒检查12件不同的事情,如果其中一件符合标准,它应该发送通知;然后,在接下来的30分钟(一小时半)间隔之前,它不应该发送另一个通知。如何检查通知是否已发送,是否不再发送?我以为这是通过标签完成的,但是仍然会弹出两个具有相同标签的通知,第二个通知将取代第一个通知 用于生成通知的函数是 function randomNotification(whoisit, tagtoPass) { var message = whoisit + " i

我正在编写一个需要发送通知的web应用程序。它每30秒检查12件不同的事情,如果其中一件符合标准,它应该发送通知;然后,在接下来的30分钟(一小时半)间隔之前,它不应该发送另一个通知。如何检查通知是否已发送,是否不再发送?我以为这是通过标签完成的,但是仍然会弹出两个具有相同标签的通知,第二个通知将取代第一个通知

用于生成通知的函数是

function randomNotification(whoisit, tagtoPass) {

    var message = whoisit + " is getting a notification";
    var options = {
        body: message,
        icon: 'logo.png',
        tag: tagtoPass,
    }

    var n = new Notification('Website Says:',options);
    setTimeout(n.close.bind(n), 5000);
  }
该函数由另一个脚本调用,该脚本每30秒通过Ajax加载一次。该脚本检查数据库,查看12件事情中是否有一件处于需要通知的状态,然后调用函数并在30分钟的间隔内传递名称和唯一标记


我还注意到,这段代码基于Mozilla on Notifications API上的开发者文档,没有将通知添加到OS X通知中心。

您是否考虑过在localStorage中使用标志来存储上次发送通知的时间

假设您存储了发送第一个通知的时间,然后再次检查它,如果它足够旧,则发送另一个通知并更新该标志

即使刷新网页,本地存储数据也将保留

例如:

localStorage.setItem("last_notification", timestamp)
localStorage.getItem("last_notification")

参考资料:

我考虑过这一点,但我认为这不是使用通知的正确方式,我应该使用某种本机通知API解决方案。您可以使用中所述的Redis锁