Is there any easy way to feature detect Web Push API in browsers (Desktop and mobile)
Asked
Active
Viewed 2,722 times
2 Answers
17
Some browser versions support only the service worker but not the Push API.
It's recommended that you try to detect the Push API itself.
If you need to check the browser support in a synchronous way (i.e. without waiting for Promise), you can use this (copied from the Pushpad SDK):
function isPushApiSupported() {
return 'PushManager' in window;
}

collimarco
- 34,231
- 36
- 108
- 142
-
Interesting, I wasn't aware some browsers didn't support the Push API but supported Service Workers! Which ones are they? – Marco Castelluccio Aug 12 '16 at 11:57
-
Firefox for mobile supports serviceworker but not Push API – Prashant Agrawal Aug 12 '16 at 14:09
-
This check wouldn't work from within a Service Worker, as the window object simply isn't available there. – Rednael May 02 '19 at 13:46
-
@Marco also Safari 12 – Martin Ždila Aug 06 '19 at 09:29
1
Currently, any browser which supports Service Workers also supports Web Push.
So simply check if navigator
has a serviceWorker
property.
If you are afraid in the future some browsers might start supporting Service Workers before Web Push, something like this would work:
navigator.serviceWorker.getRegistration()
.then(function(registration) {
if (registration.pushManager) {
// Web Push supported.
} else {
// Web Push not supported.
}
});

Marco Castelluccio
- 10,152
- 2
- 33
- 48
-
`registration` is `null` if a service worker isn't controlling the current page. – Jason Jun 26 '17 at 21:08
-
This would be the best solution in my opinion, because this check can also be done within a Service Worker. The accepted answer uses the window object, which isn't available in a Service Worker. – Rednael May 02 '19 at 13:44
-
2This is not true. For a long time and as of now (04/22) Safari supports ServiceWorkers but not Push. – Lukáš Řádek Apr 05 '22 at 13:11