【发布时间】:2014-08-20 22:35:54
【问题描述】:
我找到了以下 HTML 通知示例,它在 Chrome 和 Firefox 中运行良好。下载并在本地尝试后,它不再在 Chrome 中运行。这是预期的行为(Chrome 由于某种原因在本地阻止通知)还是有任何其他原因导致这不起作用?
<!DOCTYPE html>
<html>
<body>
<button onclick="notifyMe()">Notify me!</button>
<script>
function notifyMe() {
// Let's check if the browser supports notifications
if (!("Notification" in window)) {
alert("This browser does not support desktop notification");
}
// Let's check if the user is okay to get some notification
else if (Notification.permission === "granted") {
// If it's okay let's create a notification
var notification = new Notification("Hi there!");
}
// Otherwise, we need to ask the user for permission
// Note, Chrome does not implement the permission static property
// So we have to check for NOT 'denied' instead of 'default'
else if (Notification.permission !== 'denied') {
Notification.requestPermission(function (permission) {
// Whatever the user answers, we make sure we store the information
if(!('permission' in Notification)) {
Notification.permission = permission;
}
// If the user is okay, let's create a notification
if (permission === "granted") {
var notification = new Notification("Hi there!");
}
});
}
}
</script>
</body>
</html>
【问题讨论】:
-
我不知道你所说的“本地”是什么意思。我发现页面在本地服务器上运行正常,但使用文件路径运行失败。
-
与大多数类似的事情一样,如果“本地”表示 UNC URL(即浏览器地址栏中的
file:///....),它将无法按预期工作。即使它只是 IIS 或 Apache 的本地实例,您也需要在正确的 http 服务器上。 -
Web API 通知在 Safari 中是否有效?因为我尝试了 emogotchi 的演示 developer.mozilla.org/en-US/docs/Web/API/Notifications_API/… 并且它可以工作,但是当我尝试创建自己的应用程序时(通过逐字复制代码进行测试)它不起作用。
标签: javascript html google-chrome