You are correct; Notification.requestPermission() does not fire when the permission request is automatically blocked by the browser, such as Chrome.

In modern browsers like Chrome, automatic permission is often given for notifications based on user behavior on the website. If a website abuses the notification feature or doesn't meet the browser's criteria for a good user experience, the browser might automatically block notifications by default.

When the permission is automatically blocked, the Notification.requestPermission() call will not trigger any events or callbacks. Instead, the permission value will be set to 'default' or 'denied' depending on the browser's default behavior.

Here's how you can handle the situation in JavaScript:

javascript
function requestNotificationPermission() { if (Notification.permission === 'granted') { // Notifications are already allowed // You can proceed with showing notifications } else if (Notification.permission === 'denied') { // Notifications are blocked, and user denied permission // You might show a message or UI to inform the user } else { // Browser is yet to decide, or the user hasn't interacted with the notification feature // Call Notification.requestPermission() to request permission explicitly Notification.requestPermission().then((permission) => { if (permission === 'granted') { // Permission granted, you can proceed with showing notifications } else { // Permission denied, you might show a message or UI to inform the user } }); } } // Call the function to request permission requestNotificationPermission();

The code above checks the current Notification.permission status. If the permission is 'granted', notifications are allowed, and you can proceed with showing notifications. If the permission is 'denied', notifications are blocked, and you might want to inform the user or provide an alternative.

If the Notification.permission is neither 'granted' nor 'denied', it means the browser hasn't made a decision yet (or the user hasn't interacted with the notification feature). In this case, calling Notification.requestPermission() will explicitly request permission from the user. The result of the request will be available in the .then() callback, and you can handle it accordingly.

Keep in mind that browser behavior might change, and this approach is based on the behavior as of my last knowledge update in September 2021. Always check the latest browser documentation and updates for changes related to notification permissions.

Have questions or queries?
Get in Touch