Jagajit Prusty
Jagajit Prusty

Reputation: 2110

Does service worker runs on background even if browser is closed?

I see push notification for Facebook web in chrome only when I open chrome. I know that this notification are sent through service worker. I am wondering whether this background sync goes on even though browser is closed or only on opening chrome only these service-worker sync process get started and start sending push notification.

Upvotes: 38

Views: 32335

Answers (7)

SLEAR MENDOZA
SLEAR MENDOZA

Reputation: 11

Let me explain the key differences in push notification behavior between Android and Windows:

Android:

  • Push notifications work even when the browser is completely closed

  • If the PWA is installed, notifications work like native app notifications

  • The service worker stays active in the background

  • Users will receive notifications as long as:

     1. They granted notification permission
     2. The PWA/browser is not force stopped
     3. Battery optimization/power saving mode isn't restricting background services
     4. They have internet connectivity
    

Windows:

Push notifications only work when:

The browser is running (can be minimized) For Chrome/Edge/Firefox specifically, they must be running in the background (can be in system tray)

  • If the browser is completely closed (no background process), push notifications won't work
  • Even if the PWA is installed on Windows, it still depends on the browser being active
  • The exception is if you use Windows native notifications through the Windows Push Notification Service (WNS), but this requires a different implementation

This difference occurs because:

Android has a more permissive background process model that allows service workers to run continuously Windows treats web push notifications as part of the browser process Android integrates PWAs more deeply into the system compared to Windows

So Android will receive push notifications even with the browser closed (more like a native app), while Windows requires the browser to be running in some form. To support both platforms effectively, you should:

  1. Implement standard web push notifications for both platforms
  2. Consider implementing WNS for better Windows support if needed
  3. Inform Windows users they need to keep their browser running
  4. Consider developing a native Windows app if consistent push notification delivery is crucial

Upvotes: 0

sunyata
sunyata

Reputation: 2261

If you go to chrome://serviceworker-internals/ you will find a list of registered service workers. (For me the vast majority of these have Running Status: STOPPED and only a few Running Status: RUNNING)

If a service worker is stopped (idle) it should wake up to handle events like push. However there are some platform differences (for example on mobile platforms there might be delays for battery reasons)

I hope this helps!

Upvotes: 0

I found a solution to this issue by adding it to the manifest.json next parameter

"background": {
    "service_worker": "service-worker.js"
}

Upvotes: 0

Maksim Shamihulau
Maksim Shamihulau

Reputation: 1738

In the context of receiving push messages on desktop, you will receive messages when the browser is running, i.e. has the marking underneath the icon.

This means the browser can have no windows open, and you'll still receive the push message in your service worker, because the browser in running in the background.

The only time a push won't be received is when the browser is completely closed, i.e. not running at all (no marking). The same applies for Windows, although it's a little trickier to determine whether or not Chrome is running in the background.

source https://web.dev/push-notifications-faq/#why-doesn't-push-work-when-the-browser-is-closed

Upvotes: 0

Martin Ždila
Martin Ždila

Reputation: 3219

Little offtopic but it is possible to write Chrome extension where background script can run if Chrome is allowed to run in the background (configurable in settings). It can also use GCM.

Upvotes: 0

Jagajit Prusty
Jagajit Prusty

Reputation: 2110

I don't think service worker will be able to run if browser is closed. Because service-worker is able to send push notification only after I open the browser. If it is running in the background then it could have send notification even after closing browser also.

Upvotes: 4

Matt Gaunt
Matt Gaunt

Reputation: 9831

First thing to say is this depends somewhat on the platform. My understanding of chrome is:

On desktop platforms like windows and Mac OS X the browser needs to have some background process running for a service worker to be able to run. On Mac OS X this is quite easy to detect as the browser can have no windows open but the browser still has the glowing dot beneath it.

On mobile platforms it's easier to listen for events and handle them in an efficient manner, so in these cases the platform can wake up the browser which will then handle any corresponding events.

The above applies to any service worker api's.

On Desktop: If the browser is completely closed then service workers can not run and will not dispatch any events (i.e. no push or background sync events)

On Mobile: The events will still be dispatched (i.e. background sync will trigger when the users device comes online and push will be received and cause a push event).

Upvotes: 26

Related Questions