Search code examples
javascriptwebnotifications

Web notifications is not working on chrome, but it's working fine on firefox and microsoft edge


I am trying to make web notification, which is worked fine on firefox and microsoft edge. even it worked fine on safari. but it does not want to work on chrome. and it shows no error. this is my code:

    <script>
    window.setInterval(function(){notifyMe();}, 5000);
    function notifyMe() {
      if (!("Notification" in window)) {
        alert("This browser does not support desktop notification");
      } else if (Notification.permission === "granted") {
          var data = []
          data['title'] = "notification's title";
          data['body'] = "notification's body";
          var notification = new Notification(data['title'], {
            'body': data['body']
          });
          notification.onclick = function(event) {
            window.open('https://www.example.com/', '_blank');
          }
      } else if (Notification.permission !== "denied") {
        Notification.requestPermission().then(function (permission) {
          if (permission === "granted") {
            var notification = new Notification("really");
          }
        });
      }
    }
</script>

Solution

  • Is your website served using https:// - because Chrome has deprecated Notifications API on insecure (e.g. http://) origins

    Otherwise, your code works just fine in Chrome