VictorGalisson
VictorGalisson

Reputation: 717

Is it possible to use service worker to show notifications when user is on website?

I have a website where I register a service worker for push notifications. There are two cases to show an upcoming notification :

Can service worker always handle the event, even when the user is on the website? If it's possible how to do so?

This with the purpose of not adding an event handler to each of my website pages.

Thanks in advance for any clarification,

Upvotes: 2

Views: 2097

Answers (2)

VictorGalisson
VictorGalisson

Reputation: 717

I found an anwser there : https://developers.google.com/web/ilt/pwa/introduction-to-push-notifications#handling_the_push_event_in_the_service_worker

The push event is the event that the service worker needs to handle in order to show a push notification afterwards.

By doing so, even when the webapp is on focus or not, the service worker will "see" the push message.

Upvotes: 1

Jonas Wilms
Jonas Wilms

Reputation: 138567

Can service worker always handle the event, even when the user is on the website?

Yes, they run regardless of the currently opened pages of the browser, or the browser being open at all. They only depend on the browsers background thread to run, which usually always runs when the client runs.

Upvotes: 1

Related Questions