【问题标题】:HTML 5 Notifications are not working locally in chrome?HTML 5 通知在 Chrome 中本地不起作用?
【发布时间】:2014-08-20 22:35:54
【问题描述】:

我找到了以下 HTML 通知示例,它在 Chrome 和 Firefox 中运行良好。下载并在本地尝试后,它不再在 Chrome 中运行。这是预期的行为(Chrome 由于某种原因在本地阻止通知)还是有任何其他原因导致这不起作用?

<!DOCTYPE html>
<html>

    <body>

        <button onclick="notifyMe()">Notify me!</button>

        <script>
        function notifyMe() {
          // Let's check if the browser supports notifications
          if (!("Notification" in window)) {
            alert("This browser does not support desktop notification");
          }

          // Let's check if the user is okay to get some notification
          else if (Notification.permission === "granted") {
            // If it's okay let's create a notification
            var notification = new Notification("Hi there!");
          }

          // Otherwise, we need to ask the user for permission
          // Note, Chrome does not implement the permission static property
          // So we have to check for NOT 'denied' instead of 'default'
          else if (Notification.permission !== 'denied') {
            Notification.requestPermission(function (permission) {

              // Whatever the user answers, we make sure we store the information
              if(!('permission' in Notification)) {
                Notification.permission = permission;
              }

              // If the user is okay, let's create a notification
              if (permission === "granted") {
                var notification = new Notification("Hi there!");
              }
            });
          }

        }
        </script>

    </body>
</html>

【问题讨论】:

  • 我不知道你所说的“本地”是什么意思。我发现页面在本地服务器上运行正常,但使用文件路径运行失败。
  • 与大多数类似的事情一样,如果“本地”表示 UNC URL(即浏览器地址栏中的file:///....),它将无法按预期工作。即使它只是 IIS 或 Apache 的本地实例,您也需要在正确的 http 服务器上。
  • Web API 通知在 Safari 中是否有效?因为我尝试了 emogotchi 的演示 developer.mozilla.org/en-US/docs/Web/API/Notifications_API/… 并且它可以工作,但是当我尝试创建自己的应用程序时(通过逐字复制代码进行测试)它不起作用。

标签: javascript html google-chrome


【解决方案1】:

根据W3C Specification,只要文件托管在某处,您所拥有的看起来还不错。这是一个不错的notification repo,带有live demo

【讨论】:

  • 这是否意味着您可以托管javascript文件或者您必须托管html文件和js文件?我想在本地测试。
  • 对我不起作用。歌剧,mac os,请勿打扰 = 关闭
猜你喜欢
  • 2013-05-29
  • 1970-01-01
  • 2017-06-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-09
相关资源
最近更新 更多