首页 > 解决方案 > 如何在本地安排 React 中的重复网络通知?

问题描述

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

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

我是 React 开发的初学者。我读过这可能以某种方式通过服务人员完成,但我还没有找到任何方法来在本地设置重复通知。

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

所以我的问题如下:是否可以在本地设置定期网络通知?如果是这样,你会怎么做?

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>;
  }
}

标签: javascriptreactjsnotificationsjob-schedulingweb-notifications

解决方案


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

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

你可以在这里阅读更多内容:https ://web.dev/notification-triggers/

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

如果有人对此主题有更新或不同的信息,请提供更多答案:)


推荐阅读