简体   繁体   English

通知 HTML5 在 Chrome 本地工作吗?

[英]Does Notification HTML5 work in local in Chrome?

I am trying to create a notification in Chrome.我正在尝试在 Chrome 中创建通知。 I have write this simple code but there is notification shown in CHrome whereas checkPermission() return well 0 .我已经编写了这个简单的代码,但是 CHrome 中显示了通知,而checkPermission()返回了0

I do the same thing than this website (example), which works fine in my Chrome browser.我和这个网站(示例)做同样的事情,它在我的 Chrome 浏览器中运行良好。

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 !事实上,我允许在 Chrome 设置中显示来自所有网站的通知,现在,它工作正常!

在此处输入图片说明

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这是一个jsfiddle

Webkit notifications requestPermission function doesn't work Webkit 通知 requestPermission 功能不起作用
requestPermission Requests that the user agent ask the user for permission to show notifications from scripts. requestPermission 请求用户代理请求用户允许显示来自脚本的通知。 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.如果当前权限级别为 PERMISSION_DENIED,则用户代理可能不会响应 requestPermission 采取任何行动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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