if(undefined !== window.Notification) {
if (Notification.permission === "default") {
Notification.requestPermission();
}
}
Notification.requestPermission(function() {
if(Notification.permission === 'granted') {
// 用户点击了允许
let n = new Notification('华为好', {
body: '华为给我涨智慧',
icon: 'https://res.hc-cdn.com/cnpm-header-and-footer/2.0.6/base/header-china/components/images/logo.svg'
})
setTimeout(() => {
n.close();
}, 8000)
n.onclick = function(e) {
window.open("https://www.xxxx.com")
console.log(1, e);
}
n.onerror = function(e) {
console.log(2, e);
}
n.onshow = function(e) {
console.log(3, e);
}
n.onclose = function(e) {
console.log(4, e);
}
}else if(Notification.permission === 'denied') {
console.log("用户点击了拒绝");
}else {
console.log("用户没有做决定");
}
})
核心代码如下 :
// 用户点击了允许
let n = new Notification('华为好', {
body: '华为给我涨智慧',
icon: 'https://res.hc-cdn.com/cnpm-header-and-footer/2.0.6/base/header-china/components/images/logo.svg'
})
setTimeout(() => {
n.close();
}, 8000)
n.onclick = function(e) {
window.open("https://www.baidu.com")
console.log(1, e);
}
n.onshow = function(e) {
console.log(3, e);
}
n.onclose = function(e) {
console.log(4, e);
}