9

Is there any easy way to feature detect Web Push API in browsers (Desktop and mobile)

Marco Castelluccio
  • 10,152
  • 2
  • 33
  • 48
Prashant Agrawal
  • 660
  • 9
  • 24

2 Answers2

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
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
  • 2
    This 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