我找到了一个关于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>
file:///....
),它将无法按预期工作。即使只是IIS或Apache的本地实例,您也需要连接到正确的http服务器上。 - Ben