javascript - 如何使 service-worker.js 在浏览器打开时执行?

标签 javascript push-notification service-worker

您好,我已经安装了一个服务工作人员,但它仅在我打开页面时才起作用,这是代码

self.addEventListener('install', function(event) {
  console.log('service worker online')
});

安装时它会导致服务人员在线,但我的问题是即使页面未打开,我怎样才能使其工作

我想用它来发送浏览器通知,例如

enter image description here 在浏览器上打开就像 Facebook 一样

最佳答案

有关向 Web 应用程序添加推送通知支持的信息,请访问 https://developers.google.com/web/fundamentals/codelabs/push-notifications/

如果您尚未绑定(bind)到给定的推送消息传递提供商,一种方法是使用 Firebase Cloud Messaging ,它附带一个库来简化服务工作线程的推送处理程序的实现。

一般来说,如果您对 Service Worker 中的各种代码实际执行的时间感到好奇,this answer可能会有帮助。您确实需要在服务工作人员中实现推送通知才能获得您所描述的行为。

关于javascript - 如何使 service-worker.js 在浏览器打开时执行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51355516/

相关文章:

javascript - 使用 Service Worker 离线处理文件上传

javascript - Shiny 的条件面板 : condition based on class of variable in a dataset

javascript - JQuery Animate 函数不改变左绝对位置

javascript - 当一个部分显示在滚动条上时,滚动条是错误的

javascript - 如何防止同一个 Service Worker 在多个页面上注册?

javascript - Web/桌面通知 API - 刷新页面时强制关闭所有通知

javascript - jquery 完整日历 : initialize an array from source every time new events are fetched

php - 从 iOS 到 php 服务器的 POST 数据始终为 NULL - iOS 10、Swift 3、php

iOS 10 请求通知权限触发两次

push-notification - 工作灯 : Push notification without User ID