Is there a way to display desktop notifications even when Chrome or Firefox is closed?

Sivaprasanna Sethuraman picture Sivaprasanna Sethuraman · Feb 10, 2016 · Viewed 15.1k times · Source

We're developing a website which sends push notifications to end users using GCM. We've gone through Service Worker and all. We have developed a prototype using this codelab tutorial. It is working so far, but the only issue is the notifications are displayed only when Chrome is opened. When Chrome is closed, the notifications don't reach the users.

I want to know is there any way we can overcome this and display the notifications even when the browser is closed, similar to Safari Push Notification. Thanks in advance!

Answer

abielita picture abielita · Feb 11, 2016

If you have a "background" permission in manifest.json, your background page will be able to show notifications even when Chrome window is closed.

"permissions": [
    "background"
],

As stated in the documentation:

When any installed hosted app, packaged app, or extension has "background" permission, Chrome runs (invisibly) as soon as the user logs into their computer—before the user launches Chrome. The "background" permission also makes Chrome continue running (even after its last window is closed) until the user explicitly quits Chrome.

You need to use the "background" permission with a background page, event page or a background window for hosted apps.

For web, use Push API for Chrome and other browsers. The advantage of using push messages is that even if your page is closed, your service worker will be woken up and be able to show a notification. Web Sockets and EventSource have their connection closed when the page or browser is closed so it's not recommended. Here is the documentation and example.