To add browser notifications

master
taoshihan 2 years ago
parent 08b00b42d8
commit 2ac2f999cf

@ -18,20 +18,36 @@ function getWsBaseUrl() {
} }
return url; return url;
} }
//除去html标签
function replaceHtml(str){
return str.replace(/<[^>]*>/g, '');
}
//浏览器桌面通知
function notify(title, options, callback) { function notify(title, options, callback) {
// 先检查浏览器是否支持 // 先检查浏览器是否支持
if (!window.Notification) { if (!window.Notification) {
console.log("浏览器不支持notify");
return; return;
} }
var notification; options.body=replaceHtml(options.body);
console.log("浏览器notify权限:", Notification.permission);
// 检查用户曾经是否同意接受通知 // 检查用户曾经是否同意接受通知
if (Notification.permission === 'granted') { if (Notification.permission === 'granted') {
notification = new Notification(title, options); // 显示通知 var notification = new Notification(title, options); // 显示通知
if (notification && callback) {
} else { notification.onclick = function(event) {
var promise = Notification.requestPermission(); callback(notification, event);
} }
setTimeout(function () {
notification.close();
},3000);
}
} else {
Notification.requestPermission().then( (permission) =>function(){
console.log("请求浏览器notify权限:", permission);
if (permission === 'granted') {
notification = new Notification(title, options); // 显示通知
if (notification && callback) { if (notification && callback) {
notification.onclick = function (event) { notification.onclick = function (event) {
callback(notification, event); callback(notification, event);
@ -40,6 +56,14 @@ function notify(title, options, callback) {
notification.close(); notification.close();
}, 3000); }, 3000);
} }
} else if (permission === 'default') {
console.log('用户关闭授权 可以再次请求授权');
} else {
console.log('用户拒绝授权 不能显示通知');
}
});
}
} }
var titleTimer=0; var titleTimer=0;
var titleNum=0; var titleNum=0;

Loading…
Cancel
Save