javascript - 如何在本地安排 React 中的重复网络通知?
问题描述
我正在尝试构建一个 React Web 应用程序,它会在设定的时间每天通知用户,而无需连接到服务器。
例如:用户设置他/她希望每天下午 5 点收到通知。然后,应用程序会以某种方式在本地设置此循环网络通知。即使设备未连接到服务器并且 Web 应用程序未打开,用户也会收到通知。
我是 React 开发的初学者。我读过这可能以某种方式通过服务人员完成,但我还没有找到任何方法来在本地设置重复通知。
我尝试了诸如node-schedule或react-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>;
}
}
解决方案
显然没有办法在浏览器中本地执行此操作,因为浏览器不支持服务工作者中的时间触发器。
话虽如此,这些通知触发器正在开发中,但我们不太可能很快看到它们。
你可以在这里阅读更多内容:https ://web.dev/notification-triggers/
因此,如果您尝试创建类似常规通知的内容,则每次都必须从服务器发送推送通知。
如果有人对此主题有更新或不同的信息,请提供更多答案:)
推荐阅读
- python - DJANGO-GRAPHQL-JWT:我们如何知道刷新令牌发布后的年龄?
- forms - Netlify 无法识别 nuxt 应用程序中的表单
- javascript - 如何在相同的子组件中处理 React Native animated.timing
- database - 仅当 hdfs 中有文件时如何执行命令
- javascript - 小数到分数计算器Javascript问题
- swift - 如何将视图(不是 UIView)转换为图像?
- python - Matplotlib 绘图悬停 xlwings 导出到 excel
- php - 在 PHP 中不显示登录错误的验证消息
- java - 执行长时间运行的命令后,Jsch“exec”通道不会自动关闭
- java - 如何修复此输出