Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chrome treats 'default' result from Notification.requestPermission() as 'denied'

The Notification.requestPermission() has 3 possible results: granted, denied and default.

In Chrome you get default when the user close the permission dialog using the X instead of explicitly saying block. But, if after getting default as result, you call Notification.permission you get denied, making impossible to retry asking permission again in the future.

It this by design? is there a way to make chrome treating differently this two results? Firefox treats this in the right way (you can ask permissions until the user explicitly denied it)

like image 988
Coluccini Avatar asked Mar 07 '23 14:03

Coluccini


1 Answers

I'll leave this just in case someone is looking for an answer:

When the user had closed the permission dialog for the third time Chrome will automatically set the permission to denied (it shows a automatically blocked message below on the permission popup from the navbar). So the first three times the user close the dialog you gets default as result, but on the third time the permission are set to denied.

The way I'm using to work with this logic is:

window.Notification.requestPermission().then((result) => {
  if (result === 'denied') {
     // the user has denied permission
     return;
  }

  if (result === 'default') {
    // the user has closed the dialog
    if (window.Notification.permission === 'denied') {
       // the browser has decided to automatically denied permission 
    }
    return;
  }

  // the user has granted permission
});
like image 81
Coluccini Avatar answered Apr 26 '23 11:04

Coluccini