代码:
function sendNotificationForChatTimeoutViaWeb(title,body,tag) {
// 如果用户同意就创建一个通知
if (window.Notification && Notification.permission === "granted") {
sendNotification(title,body,tag);
}
// 如果用户没有选择是否显示通知
// 注:因为在 Chrome 中我们无法确定 permission 属性是否有值,因此
// 检查该属性的值是否是 "default" 是不安全的。
else if (window.Notification && Notification.permission !== "denied") {
Notification.requestPermission(function (status) {
if (Notification.permission !== status) {
Notification.permission = status;
}
// 如果用户同意了
if (status === "granted") {
sendNotification(title,body,tag);
}
});
}
}
/*提醒*/
function sendNotification(title,body,tag ,icon="") {
let n = new Notification(title, {
body: body,
// tag: tag,
icon: icon
})
n.onshow = function () {
console.log("bg onshow");
}
n.onclose = function () {
console.log("bg onclose");
}
n.onerror = function () {
console.log("bg onerror");
}
// n.onshow = function () {
// setTimeout(n.close.bind(n), 5000);
// }
}
只有通过Web服务访问该页面时才会生效,如果直接双击打开本地文件,是没有任何效果的