思路:轮询方式,JS定时任务去请求后端接口,拿到数据进行提醒
function showNotification(title, content, icon, requireInteraction, link) {
window.Notification.permission = "granted";
if (window.Notification) {
if (window.Notification.permission == "granted") {
var notification = new Notification(title, {
body: content,
icon: icon,// "图标路径,若不指定默认为favicon"
requireInteraction: requireInteraction,//
link: link//
});
notification.onclick = function () {
//可直接打开通知notification相关联的tab窗口
window.focus();
window.open=link;
}
setTimeout(function(){notification.close();},10000);
setTimeout(function () {
notification.close();
}, 10000);
} else {
window.Notification.requestPermission();
}
} else alert('你的浏览器不支持此消息提示功能,请使用chrome内核的浏览器!');
}
function backstageMsgPush() {
$.post("${base}/admin/msg/getMsg",function (res) {
if (res.code == "200") {
setTimeout(backstageMsgPush, 300000);
$.each(res.data, function (index, item) {
showNotification(item.title, item.content, item.icon, item.requireInteraction, item.link);
});
}
});
}
$(function () {
backstageMsgPush();
})