8

I have a web app in which I am using HTML Notifications. It works fine if the user allows it for the first time and start using it, however if user blocks the notification the first time by clicking the block button and later on try to request permission again by some user gesture then the browser doesn't trigger (Allow/Block) popup.

Here is the second time I am triggering the permission.

if(Notification.permission == 'denied' || Notification.permission == 'default'){

        Notification.requestPermission(function (permission) {
    // If the user accepts, let's create a notification
            if (permission === "granted") {
                console.log("Regranted");
            }
        });
    }

It works fine for the default case but not for the denied case.

Ram
  • 3,092
  • 10
  • 40
  • 56
firebolt_ash
  • 1,204
  • 4
  • 13
  • 21
  • 4
    I'm not 100% but isn't this by design, else you could just spam the user requesting access constantly? – djsmiley2kStaysInside Jun 30 '15 at 08:58
  • Completely agree with @djsmiley2k. You are probably tring to make a adware kind of thing. – Vibhor Dube Jun 30 '15 at 09:00
  • 1
    @Vibhor Dube :- My app has a button to enable notifications. (If the user for some reason blocked it earlier and now want to unblock using my app ~ I wanted to give him an option) to avoid any inconsistency. Thanks though ! – firebolt_ash Jun 30 '15 at 09:06
  • Use this link to see the answer posted by me: [enter link description here](https://stackoverflow.com/questions/47447637/notification-permission-gives-denied-always/50299546#50299546) – Utmost Creator May 11 '18 at 20:11
  • See the answer by clicking this link: [enter link description here](https://stackoverflow.com/a/50299546/6901693) – Utmost Creator May 11 '18 at 20:12
  • To see the answer go by this link: [enter link description here](https://stackoverflow.com/a/50299546/6901693) – Utmost Creator May 11 '18 at 20:15
  • I would advice you instead of immediately asking for Notification permissions, display a dialog or component to your user explaining what the notifications are for, then use a button to trigger Notification permission request. This way, your user would be in context when prompted to give permissions – Gabriel Balsa Cantú Jan 20 '19 at 22:06

2 Answers2

12

The behavior you’re seeing is by design, as an earlier comment points out. If you read step 2, substep 2 at https://notifications.spec.whatwg.org/#dom-notification-requestpermission you’ll see the spec requires that the only time a user is asked whether showing notifications is acceptable is when the permission value is default. If the permission value is granted or blocked, that algorithm requires that the user is never asked again whether showing notifications is acceptable.

Users who do change their minds about notifications for a site they’ve blocked have the option to go into their browser settings to reset the permissions for that site themselves.

sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
  • 1
    This is the correct answer. The only alternative to display the notification prompt multiple times (even if the user says "No" to the notifications) is to use two prompts: the first prompt is made with HTML+CSS and the second prompt is the real one. Check out this article about the [double opt-in](https://blog.pushpad.xyz/2020/04/the-double-opt-in-for-web-push-notifications/) – collimarco Apr 05 '22 at 19:48
6

I would recommend having a button to turn notifications on, then checking the permission there, falling back if it's been previously denied.

ex:

  if (Notification.permission === "denied") {
    alert("Notifications blocked. Please enable them in your browser.");
  }