locropulenton
locropulenton

Reputation: 4853

Why is the Notification API not working on chrome for android?

I am using the W3C Notifications API, documented at MDN.

However on Android Chrome it seems to prompt the user to allow/disallow notification request, but if the user clicks 'allow' nothing seems to happen. Does Android Chrome support this?

Notification.requestPermission(function (permission) {
    // If the user accepts, let's create a notification
    if (permission === "granted") {
        var options = {
            body: 'something in the body'
        }
        var n = new Notification('My title', options);
    }
}

Then I Created a minimun repository to try this out: https://github.com/agrcrobles/minimum-web-notification

It's deployed here: https://minimum-web-notification-n8jmmz0fz.now.sh/

Upvotes: 1

Views: 2519

Answers (1)

locropulenton
locropulenton

Reputation: 4853

The following works on a service worker on android chrome:

Notification.requestPermission(function(result) {
  if (result === 'granted') {
    navigator.serviceWorker.ready.then(function(registration) {
      registration.showNotification('Notification with ServiceWorker');
    });
  }
});

it must run from a secure origin (an https URL, not an http URL).

https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerRegistration/showNotification has more info.

https://groups.google.com/a/chromium.org/forum/#!topic/blink-dev/BygptYClroM has info about why the Notification constructor is not supported in Chrome on Android though it still is in desktop Chrome.

Upvotes: 1

Related Questions