wawanopoulos
wawanopoulos

Reputation: 9794

Does Notification HTML5 work in local in Chrome?

I am trying to create a notification in Chrome. I have write this simple code but there is notification shown in CHrome whereas checkPermission() return well 0.

I do the same thing than this website (example), which works fine in my Chrome browser.

if (window.webkitNotifications.checkPermission() == 0) {
  window.webkitNotifications.createNotification("icon.png", "title", "text").show();
} else {
  window.webkitNotifications.requestPermission();
}

Where is the problem ?

[EDIT : Problem fixed]

In fact, i allows to show notification from all website in Chrome settings, and now, it works fine !

enter image description here

Upvotes: 2

Views: 781

Answers (1)

Rick Lancee
Rick Lancee

Reputation: 1659

Request permission only work on a user gesture (see below question, and quote from the docs).

In short you need to register a click event or something similar then request permission.

document.querySelector('#show_button').addEventListener('click', function() {
  if (window.webkitNotifications.checkPermission() == 0) { // 0 is PERMISSION_ALLOWED

    var notification = window.webkitNotifications.createNotification(
        'icon.png', 'Notification Title', 'Notification content...');
      notification.show();
  } else {
    window.webkitNotifications.requestPermission();
  }
}, false);

Here's a jsfiddle

Webkit notifications requestPermission function doesn't work
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.

Upvotes: 2

Related Questions