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图标', '新的短消息', '有新的短消息,快去看看吧');
}


阿里云服务器1核/2G/1M(88元/1年) 【腾讯云】云产品限时秒杀,爆款2核4G云服务器首年74元 腾讯云服务器老用户专享(每日限量,下手要快) 香港高稳定云服务器推荐(优惠码:haixiuge)
相关文章
热门推荐