Reputation: 717
I have a website where I register a service worker for push notifications. There are two cases to show an upcoming notification :
the user is not on the website but the browser is up: the service worker handles the event and show the notifications.
the user is on the website: the website handles the event of receiving the notification and shows it.
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
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
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