如何在本地React中安排定期Web通知?

时间:2020-06-29 16:06:26

标签: javascript reactjs notifications job-scheduling web-notifications

我正在尝试构建一个React Web应用程序,该应用程序将在每天的指定时间通知用户而无需连接到服务器。

例如: 用户设置他/她希望每天下午5点收到通知。然后,应用程序将以某种方式在本地设置此循环Web通知。即使设备未连接到服务器,即使Web应用程序未打开,也会通知用户。

我是React开发的初学者。我已经知道可以通过某种方式使用服务人员来完成此操作,但是我还没有找到在本地设置定期通知的任何方法。

我尝试了诸如node-schedulereact-browser-notifications之类的软件包,但还没有找到与服务工作者一起使用它们的方法。

所以我的问题是:是否可以在本地设置重复出现的网络通知?如果是这样,您将如何做?

import React, { Component } from "react";

class NotificationScheduler extends Component {
  constructor() {
    super();
    this.state = {
      // information about the notification time...
    };

    this.setNotification = this.setNotification.bind(this);
  }

  setNotification = () => {
    // What should be here?
  };

  render() {
    return <Button onClick={this.setNotification}>{"Schedule notitication"}</Button>;
  }
}

1 个答案:

答案 0 :(得分:0)

显然,没有办法在浏览器中本地进行此操作,因为浏览器不支持服务工作者中的时间触发。

话虽如此,这些通知触发器仍在开发中,但我们不太可能很快看到它们。

您可以在此处了解更多信息:https://web.dev/notification-triggers/

因此,如果您尝试创建类似常规通知的内容,则每次都必须从服务器发送推送通知。

如果有人有关于此主题的更新或其他信息,我们将为您提供更多答案:)