html5 浏览器消息通知(Notification)

      发布在:前端技术      评论:0 条评论

html5 浏览器消息通知(Notification)摘抄自DISCUZ源码

function Html5notification() {
var h5n = new Object();

h5n.issupport = function() {
return 'Notification' in window;
};

h5n.shownotification = function(replaceid, url, imgurl, subject, message) {
if (Notification.permission === 'granted') {
sendit();
} else if (Notification.permission !== 'denied') {
Notification.requestPermission().then(function (perm) {
if (perm === 'granted') {
sendit();
}
});
}
function sendit() {
var n = new Notification(subject, {
tag: replaceid,
icon: imgurl,
body: message
        });
n.onclick = function (e) {
e.preventDefault();
window.open(url, '_blank');
};
}
};

return h5n;
}

使用方法

var h5n = new Html5notification();
if(h5n.issupport()) {
h5n.shownotification('tag', '点击跳转链接', 'logo图标', '新的短消息', '有新的短消息,快去看看吧');
}


相关文章
热门推荐