Webkit通知requestPermission函数不起作用 [英] Webkit notifications requestPermission function doesn't work

查看:504
本文介绍了Webkit通知requestPermission函数不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在我的网站上实现对WebKit原生桌面通知的支持。当我尝试要求用户使用通知的权限时,绝对没有任何反应。例如,下面的HTML不会提示Chrome向我要求许可:

 < html lang =en> 
< head>< / head>

< body>
< h1>测试< / h1>
< script>
window.webkitNotifications.requestPermission();
< / script>
< / body>
< / html>

我知道我的Chrome版本没有问题,因为其他网站(例如 http://www.html5rocks.com/tutorials/notifications/quick/ )工作得很好:我可以看到提示和后续通知。

解决方案

检查 chromium api docs 。您可以将其仅称为用户手势/操作的反馈 - 鼠标点击等。

lockquote
requestPermission请求
用户代理要求用户获得
的权限以显示来自脚本的通知。
在处理用户手势时,此方法只能称为
;在
其他情况下它不会有
的影响。这种方法是异步的。
当用户对权限请求回复
时,回调中提供的函数将被
调用。如果
当前权限级别为
PERMISSION_DENIED,则用户代理可能
不会响应
requestPermission而采取任何行动。


UPDATE 2014-10-01 :在Chrome 37中,用户手势要求已被移除。现在应该可以请求许可以随时显示通知。如果您希望定位旧版Chrome(例如,在公司环境中),则可能需要继续依靠用户手势事件。


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.

解决方案

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.

这篇关于Webkit通知requestPermission函数不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆