HTML 5 通知在 chrome 中本地不起作用?

jan*_*jan 5 html javascript google-chrome

我找到了以下 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>
Run Code Online (Sandbox Code Playgroud)

dan*_*lmb 4

根据W3C 规范,只要文件托管在某处,您所拥有的内容看起来就没有问题。这是一个很好的通知存储库,带有现场演示

截屏