ThunderHorn
ThunderHorn

Reputation: 2035

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

enter image description here on browser open just like facebook does

Upvotes: 0

Views: 90

Answers (1)

Jeff Posnick
Jeff Posnick

Reputation: 56044

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.

Upvotes: 1

Related Questions