Webkit notifications requestPermission function doesn't work

21,546

Solution 1

Check the specification at chromium api docs. You can call it only as a feedback to user gesture/action - mouse click etc.

requestPermission Requests that the user agent ask the user for permission to show notifications from scripts. This method should only be called while handling a user gesture; in other circumstances it will have no effect. This method is asynchronous. The function provided in callback will be invoked when the user has responded to the permission request. If the current permission level is PERMISSION_DENIED, the user agent may take no action in response to requestPermission.

UPDATE 2014-10-01: In Chrome 37, the user gesture requirement was removed. It should now be possible to request permission to display notifications at any moment. If you wish to target older versions of Chrome as well (eg. in a corporate environment), you'll probably need to continue relying on user gesture events.

Solution 2

For checking Notification on local file (file://) permission will popup but notification wont work on chrome.

As a work around you can change

Content setting > Notification > Allow All websites to show notification

Shortest code to show notification

var notification = new Notification("YOUR MESSAGE");
Share:
21,546

Related videos on Youtube

Endzior
Author by

Endzior

Engineering Manager at http://songkick.com

Updated on July 09, 2022

Comments

  • Endzior
    Endzior almost 2 years

    I am trying to implement support for WebKit's native desktop notifications on my site. When I try to ask for user's permission to use the notifications, absolutely nothing happens. For example, the following HTML does not prompt Chrome to ask me for permission:

    <html lang="en">
    <head></head>
    
    <body>
        <h1>Testing</h1>
        <script>
            window.webkitNotifications.requestPermission();
        </script>
    </body>
    </html>
    

    I know that there is no problem with my version of Chrome because other sites (e.g. http://www.html5rocks.com/tutorials/notifications/quick/) work perfectly fine: I can see both the prompt and the subsequent notifications.

    • Dan Dascalescu
      Dan Dascalescu over 11 years
      Even if you get the permission bar to pop-up, notifications still won't display if you work off a local file:/// URL. After you move the file to an http:// location, window.webkitNotifications.createNotification.show() will display the notification.
    • Kevin Cohen
      Kevin Cohen over 8 years
      How do we test locally then? @DanDascalescu
  • idbehold
    idbehold about 11 years
    The example on this page is only requesting permission on a 'click' event, yet it still does not work: html5rocks.com/en/tutorials/notifications/quick
  • Tom Tu
    Tom Tu about 11 years
    if you check your errors console you'll find that twitter changed their API and the demo is now broken
  • idbehold
    idbehold about 11 years
    The button that says "Set notification permissions for this page" doesn't work either.
  • Tom Tu
    Tom Tu about 11 years
    works for me alright - a ribbon pops up in chrome 24 and asks you to allow for notifications - also if you allowed it once it isn't going to pop up anymore you'd need to clean it up in settings (chrome://settings/contentExceptions#notifications)
  • idbehold
    idbehold about 11 years
    Right under the button it specifically says: "Use this button if you also want to reset the permissions". The button doesn't do anything for me regardless of what "exceptions" I have listed for Notifications in Chrome.
  • Tom Tu
    Tom Tu about 11 years
    example is broken anyway - got link to api docs in the answer and write your example