HTML5通知无法在Mobile Chrome中使用 [英] HTML5 Notification not working in Mobile Chrome

查看:340
本文介绍了HTML5通知无法在Mobile Chrome中使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 HTML5通知API 来通知用户使用Chrome或Firefox。在桌面浏览器上,它可以工作。但是,在Android版Chrome 42中,请求权限,但通知本身未显示。



请求代码适用于所有设备:

  if('窗口通知'){
Notification.requestPermission();
}

发送代码适用于桌面浏览器,但不适用于手机:

  if('Notification'in window){
new Notification('Notify you');


解决方案

请尝试以下操作: p>

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

在Chrome和Firefox Nightly中都可以在Android上使用(但不是Firefox beta版) / p>

sw.js 文件可以只是一个零字节文件。)



有一点需要注意的是,您必须从安全的地方运行它( https URL,而不是 http URL)。



https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerRegistration/showNotification 有更多信息。



https://groups.google。 com / a / chromium.org / forum /#!topic / blink-dev / BygptYClroM 有关于Android中的Chrome不支持 Notification 构造函数的信息尽管它仍然在桌面版Chrome中。


I'm using the HTML5 notification API to notify the user in Chrome or Firefox. On desktop browsers, it works. However in Chrome 42 for Android, the permission is requested but the notification itself is not displayed.

The request code, works on all devices:

if ('Notification' in window) {
  Notification.requestPermission();
}

The sending code, works on desktop browser but not on mobile:

if ('Notification' in window) {
  new Notification('Notify you');
}

解决方案

Try the following:

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

That should work on Android both in Chrome and in Firefox Nightly (but not Firefox beta yet).

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

One caveat is that you must run it 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.

这篇关于HTML5通知无法在Mobile Chrome中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆