PWA 关闭时发送通知

发布于 2025-01-15 21:02:40 字数 176 浏览 1 评论 0原文

我使用 Angular 制作了一个 PWA 待办事项列表应用程序(链接到应用程序)。 我现在计划添加通知,以便在应用程序关闭时可以到达用户。 由于它是一个离线工作的 PWA,我不能依赖推送通知。 提前致谢

I have made a PWA Todo List App (Link To App) using Angular.
I am now planning on adding Notifications which can reach the user when the app is closed.
Since it is a PWA which works offline, I cannot depend on Push Notifications.
Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情释 2025-01-22 21:02:40

您可以使用Web周期性后台同步API您可以也可以离线运行此功能,即使应用程序已关闭,它也不需要后端服务器进行推送,因为它由服务工作人员定期(我猜至少 24 小时)

在您的项目文件中触发,注册同步事件:

registration.periodicSync.register(constants.periodicBgSyncEventName, {
    minInterval: syncMinInterval,
    networkState: "any",
});

在您的 Service Worker 文件中监听同步并显示 Web 通知:

self.addEventListener("periodicsync", (event) => {
  if (event.tag === constants.periodicBgSyncEventName) {
    self.registration.showNotification("Wake Time !!!", {
      body: `Hi, Good Morning`,
    });
  }
});

注意 - 为了使用此定期同步 API,您需要先安装 PWA。

You can make use of Web Periodic Background Sync API You can run this feature offline too and even app is closed, it doesn't require backend server for push as it triggers by the service worker periodically (min 24 hr I guess)

in your project files register sync event:

registration.periodicSync.register(constants.periodicBgSyncEventName, {
    minInterval: syncMinInterval,
    networkState: "any",
});

in your service worker file listen to the sync and show web notification:

self.addEventListener("periodicsync", (event) => {
  if (event.tag === constants.periodicBgSyncEventName) {
    self.registration.showNotification("Wake Time !!!", {
      body: `Hi, Good Morning`,
    });
  }
});

Note - In order to use this Periodic Sync API you need to install the PWA first.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文