HTML 5通知在Chrome本地环境下无法工作?

5

我找到了一个关于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>

1
我不知道你所说的“本地”是什么意思。当我在本地服务器上运行该页面时,它可以正常工作,但使用文件路径运行时失败了。 - Xweque
和大多数这样的事情一样,如果“本地”意味着UNC URL(即在浏览器地址栏中为file:///....),它将无法按预期工作。即使只是IIS或Apache的本地实例,您也需要连接到正确的http服务器上。 - Ben
Web API通知在Safari中工作吗?因为我尝试了emogotchi的演示https://developer.mozilla.org/en-US/docs/Web/API/Notifications_API/Using_the_Notifications_API,它可以工作,但是当我尝试创建自己的应用程序(通过直接复制代码进行测试)时,它无法工作。 - Kevin Cohen
1个回答

4
根据W3C规范,只要文件被托管在某个地方,您所拥有的看起来就没问题。这里有一个不错的通知库,附带一个实时演示

screenshot


这是否意味着你可以托管JavaScript文件,还是必须同时托管HTML文件和JS文件?我想在本地进行测试。 - Laki Politis
1
对我不起作用。Opera,Mac OS,勿扰模式=关闭。 - Alexey Sh.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接