Mai*_*tel 5 html5 notifications google-chrome
我按照http://www.beakkon.com/tutorial/html5/desktop-notification教程进行了html 5桌面通知.该页面上的演示适用于我.如果我复制整个代码它是这样的,但是......当我从javascript调用该方法时,它不会显示通知或permision请求.相反,它提出SECURITY_ERR: DOM Exception 18.
似乎错误是由创建通知本身的行引发的.
有人胶合为什么按钮工作并直接调用函数不?
我目前的代码:
function RequestPermission(callback)
{
window.webkitNotifications.requestPermission(callback);
}
function notif() {
if (window.webkitNotifications.checkPermission() > 0) {
RequestPermission(notif);
}
notification = window.webkitNotifications.createHTMLNotification('http://localhost:3000/images/rails.png');
notification.show();
}
Run Code Online (Sandbox Code Playgroud)
不计算:
notif();
Run Code Online (Sandbox Code Playgroud)
计算:
<button onclick="notif()">NOTIFY</button>
Run Code Online (Sandbox Code Playgroud)
谷歌浏览器:9.0.597.84(Oficiálnísestavení72991)
WebKit:534.13
SECURITY_ERR: DOM Exception 18 如果用户未允许您的请求收到通知,则此选项有效.
发生这种情况的原因仅仅是因为它requestPermission是异步的.用户点击后Allow,为了获得许可,它将允许您使用HTML5通知功能.
在您的情况下,您不等待用户点击Allow按钮,它会自动尝试创建HTML5通知,而无需等待他们的确认.如果你重新安排你的条件,它应该工作.
function RequestPermission(callback) {
window.webkitNotifications.requestPermission(callback);
}
function notif() {
if (window.webkitNotifications.checkPermission() > 0) {
RequestPermission(notif);
} else {
notification = window.webkitNotifications.createHTMLNotification('http://localhost:3000/images/rails.png');
notification.show();
}
}
Run Code Online (Sandbox Code Playgroud)
如上所述,将通知创建放在条件语句中,当回调被触发时,它将保证具有权限.
| 归档时间: |
|
| 查看次数: |
13593 次 |
| 最近记录: |