简体   繁体   中英

How to make a service-worker.js to excecute on browser open?

Hello i have installed a service worker but it works only when i open the page here's the code

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

on install it prits that service worker online but my question is how can i make it work even if the page is not open

I would like to use it to send browser notifications such as

在此处输入图片说明 on browser open just like facebook does

There's information about adding push notification support to a web app at https://developers.google.com/web/fundamentals/codelabs/push-notifications/

One approach, if you're not already tied to a given push messaging provider, is to use Firebase Cloud Messaging , which ships with a library to simplify implementing your service worker's push handler.

In general, if you're curious about when various bits of code inside your service worker actually execute, this answer might be helpful. You do need to implement push notifications in a service worker to get the behavior you describe.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM