Nicholas Rasmussen
Nicholas Rasmussen

Reputation: 21

Notification API permission being reset

I am trying to make a chatroom and have everything up and running, but am getting stuck when it comes to desktop notifications. Whenever I request permission for notifications and get the access set to 'granted', the permission will reset to 'default' not allowing notifications to be displayed. Is there a way to get the permission to stay set as 'granted' or would I have to get them to keep allowing notifications every time a new message is sent? Is it a protection involved with running it as a local file (on ChromeOS)? Here is the code for the part about notifications:

function showNotification() {
  var title = "New Message!";
  var messageValueNotif = document.querySelectorAll(".message");
  if (messageValueNotif.length !== 0) {
    messageValueNotif = messageValueNotif[messageValueNotif.length - 1].innerHTML;
  } else {
    return false;
  }
  var currentFavicon = document.querySelector('link').href;
  var notif = new Notification(title, {
    body: messageValueNotif,
    icon: currentFavicon
  });
  notif.onclick = () => {
    notif.close();
    window.parent.focus();
  }
}

function requestAndShowPermission() {
  if (Notification.permission === 'granted') {
    showNotification();
  } else if (Notification.permission !== 'denied') {
    Notification.requestPermission().then(function(permission) {
      showNotification()
    });
  }
}

window.onload = requestAndShowPermission();

Upvotes: 1

Views: 372

Answers (1)

Nicholas Rasmussen
Nicholas Rasmussen

Reputation: 21

It is apparently a file protection with ChromeOS.

Upvotes: 1

Related Questions