- 用于向用户显示通知
- 有更多的自定义能力
- 可以跨页面通知,即使用户切换页面了,也可以通知成功
- 需要用户通过权限才能正常使用
- 可以使用
Notification.requestPermission()
方法获取权限 - 返回一个 Promise,如果值是 granted,则表示用户授权了,其他值均拒绝授权
Notification.requestPermission()
.then(permission => {
console.log(permission)
})
- 创建一个 Notification 实例,并传入想通知的内容,即可创建通知
new Notification('hello world')
new Notification('hello world', {
body: 'i am body',
image: '../static/1.jpg',
vibrate: true
})
const n = new Notification('hello world')
n.close()
事件 | 触发时间 |
---|
show | 在通知显示时 |
click | 在通知被点击时 |
close | 在通知消失或调用 close 时 |
error | 发生错误阻止通知显示时 |
const n = new Notification('hello world')
n.onshow = () => { console.log('show') }