简体   繁体   中英

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 !

在此处输入图片说明

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM