前言
if(Notification.permission === 'granted'){ console.log('用户允许通知');}else if(Notification.permission === 'denied'){ console.log('用户拒绝通知');}else{ console.log('用户还没选择,去向用户申请权限吧');}
请求权限
Notification.requestPermission().then(function(permission) { if(permission === 'granted'){ console.log('用户允许通知'); }else if(permission === 'denied'){ console.log('用户拒绝通知'); }});
推送通知
var n = new Notification('状态更新提醒',{ body: '你的朋友圈有3条新状态,快去查看吧', tag: 'linxin', icon: 'http://blog.gdfengshuo.com/images/avatar.jpg', requireInteraction: true})
var n = new Notification('状态更新提醒',{ body: '你的朋友圈有3条新状态,快去查看吧'})setTimeout(function() { n.close();}, 3000);
事件
var n = new Notification('状态更新提醒',{ body: '你的朋友圈有3条新状态,快去查看吧', data: { url: 'http://blog.gdfengshuo.com' }})n.onclick = function(){ window.open(n.data.url, '_blank'); // 打开网址 n.close(); // 并且关闭通知}
应用场景
function addOnBeforeUnload(e) { FERD_NavNotice.notification.close();}if(window.attachEvent){ window.attachEvent('onbeforeunload', addOnBeforeUnload);} else { window.addEventListener('beforeunload', addOnBeforeUnload, false);}
以上就是html5桌面通知之Notification API详解的详细内容,更多请关注Gxl网其它相关文章!
查看更多关于html5桌面通知之NotificationAPI详解的详细内容...
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://www.haodehen.cn/did40616