最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Calling Javascript Notification API on Google Chrome for Android not working directly from a web page - Stack Overflow

programmeradmin2浏览0评论

I'm working with the JavaScript Notification API to show a small message to my users. It works on every desktop browser I've tested with, including Chrome...but not Chrome for Android (KitKat, at least). Everything I've read says that Android Chrome supports the Notification API as of April 2015, and the app even has settings for Notification permissions...yet nothing happens when I call new Notification(...) from within it. In fact, even the official Mozilla Notification API demo page doesn't show them, despite being able to grab permissions information, etc.

Is there something special I need to do to make Notifications compatible with Android Chrome?

I'm working with the JavaScript Notification API to show a small message to my users. It works on every desktop browser I've tested with, including Chrome...but not Chrome for Android (KitKat, at least). Everything I've read says that Android Chrome supports the Notification API as of April 2015, and the app even has settings for Notification permissions...yet nothing happens when I call new Notification(...) from within it. In fact, even the official Mozilla Notification API demo page doesn't show them, despite being able to grab permissions information, etc.

Is there something special I need to do to make Notifications compatible with Android Chrome?

Share Improve this question edited Dec 7, 2015 at 12:45 Kinlan 16.6k5 gold badges58 silver badges88 bronze badges asked Nov 22, 2015 at 20:45 IceMetalPunkIceMetalPunk 5,5564 gold badges20 silver badges28 bronze badges 3
  • 1 Have you tried push notification examples here? developers.google.com/web/updates/2015/03/… – Mohammed AlBanna Commented Nov 22, 2015 at 21:28
  • Wow. So it sounds like not only does Google require use of its own proprietary notifications system, but also that it requires HTTPS just to use it. That's...annoying. Thanks for the help. – IceMetalPunk Commented Nov 29, 2015 at 10:42
  • There's a difference between Chrome Notification API that provided for Chrome Extensions, and Chrome Push Notification API for Mobile usage. Here is an example for notification API for chrome apps. Before days ago they talked about Push Notification API in ChromeDevSummit and still there're some of limitations around. – Mohammed AlBanna Commented Nov 29, 2015 at 16:13
Add a comment  | 

2 Answers 2

Reset to default 8

On Chrome for Android you can only open a notification via a Service Worker. A service worker is a script that runs alongside the browser and can be loaded even when the page or browser are currently closed.

The main reasoning behind the service worker requirement is that a notification can outlive the length of the browser's lifetime (i.e, the user can close the browser) and the notification will still be active, therefore when a user clicks on the notification Android needs to wake "something" up, this "something" is the Service Worker.

This Notification Guide is a good introduction to the current state of Notifications on the Web and in Chrome - it focuses a little on Push messaging but also has all the details of how to trigger a notification from the Service Worker.

This might seem like an extra hoop to jump through but it actually is a net benefit: your notification will still work when clicked even when the browser is closed.

Check this for compatibility: http://caniuse.com/#feat=notifications

"Partial Support" Although as Kinlan mentioned, it's possible with Web Service Workers.

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

The sw.js file can just be a zero-byte file.

Credit Due: HTML5 Notification not working in Mobile Chrome

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论