/**
* 发送通知
* @param {String} title 标题
* @param {String} message 消息内容
* @param {String} icon 通知图标
*/
async function sendNotification(title, message, icon = 'https://i.loli.net/2019/03/08/5c81dbb1c0b71.png') {
if (window.Notification) {
if (Notification.permission != 'granted') {
let result = await Notification.requestPermission()
if (result === 'denied' || result === 'default') {
throw Error('申请通知权限时被用户拒绝。')
}
}
return new Notification(title, {
body: message,
icon: icon
})
} else {
throw Error('当前浏览器不支持发送通知。')
}
}
;(async () => {
try {
let instans = await sendNotification('提示', '操作成功!')
// 1秒钟后关掉通知
setTimeout(() => {
instans.close()
}, 1000)
} catch (err) {
alert(err.message || '发送通知失败')
}
})()