我正在尝试使用以下方法设置 HTML 桌面通知:
// 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 (window.Notification.permission === "granted") {
// If it's okay let's create a notification
var options = {
body: "This is the body of the notification",
dir : "ltr"
};
var notification = new window.Notification("Hi there",options);
}
// 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 (window.Notification.permission !== 'denied') {
console.log('Requesting Notification permission ' + window.Notification.permission);
window.Notification.requestPermission(function (permission) {
console.log('Got resolve of request permission ' + permission);
// Whatever the user answers, we make sure we store the information
if (!('permission' in window.Notification)) {
window.Notification.permission = permission;
}
// If the user is okay, let's create a notification
if (permission === "granted") {
var options = {
body: "This is the body of the notification",
dir : "ltr"
};
var notification = new window.Notification("Hi there",options);
}
});
}
但是我没有看到允许通知的弹出窗口;相反, requestPermission 会自动被拒绝。