简体   繁体   English

使HTML5推送通知能够在所有Web浏览器和移动浏览器上正常工作吗?

[英]Getting HTML5 push notifications to work across all web browsers and mobile browsers?

I am having trouble getting HTML5 push notifications to work across all web browsers and mobile browsers. 我无法让HTML5推送通知在所有Web浏览器和移动浏览器上正常工作。 At the moment, I have the following code: 目前,我有以下代码:

<html>
<head>
<script>
document.addEventListener('DOMContentLoaded', function () {
  if (Notification.permission !== "granted")
    Notification.requestPermission();
});

function notifyMe() {
  if (!Notification) {
    alert('Desktop notifications not available in your browser.'); 
    return;
  }

  if (Notification.permission !== "granted")
    Notification.requestPermission();
  else {
    var notification = new Notification('Notification title', {
      icon: 'https://www.google.co.uk/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png',
      body: "Hey there! You've been notified!",
    });

    notification.onclick = function () {
        window.open("https://www.google.com");      
    };

  }

}
</script>
</head>
<body>

<a href="javascript://" onclick="setTimeout(function(){ notifyMe(); }, 1500);" style="font-size:50px;background:#000;color:#fff;">Test nofitications (Will load in 1.5 seconds)</a>

</body>
</html>

When I try and use the code on my android phone using Chrome, it asks for permission but the actual notification doesn't work. 当我尝试在使用Chrome的Android手机上使用代码时,它会征求许可,但实际的通知不起作用。

Any ideas? 有任何想法吗?

Edit: 编辑:

Have tried the following with no success: 尝试了以下操作但未成功:

http://www.girliemac.com/html5-notifications-webOS-style/
http://elfoxero.github.io/html5notifications/
http://codepen.io/imprakash/pen/ZYLayY
https://www.daftlogic.com/sandbox-using-html5-notifications.htm
https://davidwalsh.name/demo/notifications-api.php
http://ttsvetko.github.io/HTML5-Desktop-Notifications/
http://jsfiddle.net/Semmel/kY3Cq/
https://gauntface.github.io/simple-push-demo/
https://github.com/alexgibson/notify.js/blob/master/example/index.html

Notifications were changed to only be usable in service worker on Android via a push event. 通知已更改为仅可通过推送事件在Android的Service Worker中使用。

From a push event you can't make a notification using new Notification() , instead you need to do the following in a service worker: 在推送事件中,您无法使用new Notification()发出new Notification() ,而是需要在Service Worker中执行以下操作:

self.addEventListener('push', (event) => {
  event.waitUntil(self.registration.showNotification('Title', {
    body: 'Hello, World'
  }));
});

For more on push notifications: https://web-push-book.gauntface.com/ 有关推送通知的更多信息: https : //web-push-book.gauntface.com/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM