在公共的JS里面复制粘贴以下代码
/*上面这一段,放到需要弹出通知的地方*/
//var options = {
//dir: "auto", // 文字方向
//body: "通知:设置是否允许通知", // 通知主体
//requireInteraction: true, // 不自动关闭通知
通知图标
//icon: basePath_all+"/jsp/favicon.ico"//通知对应的图标图片地址
//};
/*延迟调用方法*/
//setTimeout(function () { notifyMe('这是通知的标题', options); }, 5000);
/*直接调用方法*/
//notifyMe('这是通知的标题', options);
function notifyMe(title, options) {
// 先检查浏览器是否支持
if (!window.Notification) {
console.log('浏览器不支持通知');
} else {
// 检查用户曾经是否同意接受通知
if (Notification.permission === 'granted') {
var notification = new Notification(title, options); // 显示通知
} else if (Notification.permission === 'default') {
// 用户还未选择,可以询问用户是否同意发送通知
Notification.requestPermission().then(permission => {
if (permission === 'granted') {
console.log('用户同意授权');
var notification = new Notification(title, options); // 显示通知
} else if (permission === 'default') {
console.warn('用户关闭授权 未刷新页面之前 可以再次请求授权');
switch (getBrowerInfo().client.name) {
case "QQ浏览器":
alert("QQ浏览器请在“设置-高级-内容设置-通知-选择允许通知或者点击管理例外情况按钮,添加www.jnd.kim");
break;
case "edge浏览器":
alert("edge浏览器请在“设置-高级-cookie和网站权限-所有权限-通知-选择允许通知或者点击管理例外情况按钮,添加www.jnd.kim");
break;
case "edg浏览器":
alert("edg浏览器请在“设置-高级-cookie和网站权限-所有权限-通知-选择允许通知或者点击管理例外情况按钮,添加www.jnd.kim");
break;
case "Chrome":
alert("Chrome浏览器请在“设置-隐私设置和安全性-通知-选择允许通知或者在允许列表点击添加,添加www.jnd.kim");
break;
default:
alert("请自行百度如何允许浏览器发送通知")
break;
}
} else {
// denied
console.log('用户拒绝授权 不能显示通知');
switch (getBrowerInfo().client.name) {
case "QQ浏览器":
alert("QQ浏览器请在“设置-高级-内容设置-通知-选择允许通知或者点击管理例外情况按钮,添加www.jnd.kim");
break;
case "edge浏览器":
alert("edge浏览器请在“设置-高级-cookie和网站权限-所有权限-通知-选择允许通知或者点击管理例外情况按钮,添加www.jnd.kim");
break;
case "edg浏览器":
alert("edg浏览器请在“设置-高级-cookie和网站权限-所有权限-通知-选择允许通知或者点击管理例外情况按钮,添加www.jnd.kim");
break;
case "Chrome":
alert("Chrome浏览器请在“设置-隐私设置和安全性-通知-选择允许通知或者在允许列表点击添加,添加www.jnd.kim");
break;
default:
alert("请自行百度如何允许浏览器发送通知")
break;
}
}
});
} else {
// denied 用户拒绝
console.log('用户曾经拒绝显示通知');
switch (getBrowerInfo().client.name) {
case "QQ浏览器":
alert("QQ浏览器请在“设置-高级-内容设置-通知-选择允许通知或者点击管理例外情况按钮,添加www.jnd.kim");
break;
case "edge浏览器":
alert("edge浏览器请在“设置-高级-cookie和网站权限-所有权限-通知-选择允许通知或者点击管理例外情况按钮,添加www.jnd.kim");
break;
case "edg浏览器":
alert("edg浏览器请在“设置-高级-cookie和网站权限-所有权限-通知-选择允许通知或者点击管理例外情况按钮,添加www.jnd.kim");
break;
case "Chrome":
alert("Chrome浏览器请在“设置-隐私设置和安全性-通知-选择允许通知或者在允许列表点击添加,添加www.jnd.kim");
break;
default:
alert("请自行百度如何允许浏览器发送通知")
break;
}
}
}
}
//获取当前浏览器是啥,如果有匹配问题,自行修改
function getBrowerInfo(){
var Browser = Browser || (function(window) {
var document = window.document,
navigator = window.navigator,
agent = navigator.userAgent.toLowerCase(),
//IE8+支持.返回浏览器渲染当前文档所用的模式
//IE6,IE7:undefined.IE8:8(兼容模式返回7).IE9:9(兼容模式返回7||8)
//IE10:10(兼容模式7||8||9)
IEMode = document.documentMode,
//chorme
chrome = window.chrome || false,
System = {
//user-agent
agent: agent,
//是否为IE
isIE: /trident/.test(agent),
//Gecko内核
isGecko: agent.indexOf("gecko") > 0 && agent.indexOf("like gecko") < 0,
//webkit内核
isWebkit: agent.indexOf("webkit") > 0,
//是否为标准模式
isStrict: document.compatMode === "CSS1Compat",
//是否支持subtitle
supportSubTitle: function() {
return "track" in document.createElement("track");
},
//是否支持scoped
supportScope: function() {
return "scoped" in document.createElement("style");
},
//获取IE的版本号
ieVersion: function() {
var rMsie = /(msie\s|trident.*rv:)([\w.]+)/;
var ma = window.navigator.userAgent.toLowerCase()
var match = rMsie.exec(ma);
try {
return match[2];
} catch (e) {
// console.log("error");
return IEMode;
}
},
//Opera版本号
operaVersion: function() {
try {
if (window.opera) {
return agent.match(/opera.([\d.]+)/)[1];
} else if (agent.indexOf("opr") > 0) {
return agent.match(/opr\/([\d.]+)/)[1];
}
} catch (e) {
// console.log("error");
return 0;
}
}
};
try {
//浏览器类型(IE、Opera、Chrome、Safari、Firefox)
System.type = System.isIE ? "IE" :
window.opera || (agent.indexOf("opr") > 0) ? "Opera" :
(agent.indexOf("chrome") > 0) ? "Chrome" :
//safari也提供了专门的判定方式
window.openDatabase ? "Safari" :
(agent.indexOf("firefox") > 0) ? "Firefox" :
'unknow';
//版本号
System.version = (System.type === "IE") ? System.ieVersion() :
(System.type === "Firefox") ? agent.match(/firefox\/([\d.]+)/)[1] :
(System.type === "Chrome") ? agent.match(/chrome\/([\d.]+)/)[1] :
(System.type === "Opera") ? System.operaVersion() :
(System.type === "Safari") ? agent.match(/version\/([\d.]+)/)[1] :
"0";
//浏览器外壳
System.shell = function() {
if (agent.indexOf("edge") > 0) {
System.version = agent.match(/edge\/([\d.]+)/)[1] || System.version;
return "edge浏览器";
}
if (agent.indexOf("edg") > 0) {
System.version = agent.match(/edg\/([\d.]+)/)[1] || System.version;
return "edg浏览器";
}
//遨游浏览器
if (agent.indexOf("maxthon") > 0) {
System.version = agent.match(/maxthon\/([\d.]+)/)[1] || System.version;
return "傲游浏览器";
}
//QQ浏览器
if (agent.indexOf("qqbrowser") > 0) {
System.version = agent.match(/qqbrowser\/([\d.]+)/)[1] || System.version;
return "QQ浏览器";
}
//搜狗浏览器
if (agent.indexOf("se 2.x") > 0) {
return '搜狗浏览器';
}
//Chrome:也可以使用window.chrome && window.chrome.webstore判断
if (chrome && System.type !== "Opera") {
var external = window.external,
clientInfo = window.clientInformation,
//客户端语言:zh-cn,zh.360下面会返回undefined
clientLanguage = clientInfo.languages;
//猎豹浏览器:或者agent.indexOf("lbbrowser")>0
if (external && 'LiebaoGetVersion' in external) {
return '猎豹浏览器';
}
//百度浏览器
if (agent.indexOf("bidubrowser") > 0) {
System.version = agent.match(/bidubrowser\/([\d.]+)/)[1] ||
agent.match(/chrome\/([\d.]+)/)[1];
return "百度浏览器";
}
//360极速浏览器和360安全浏览器
if (System.supportSubTitle() && typeof clientLanguage === "undefined") {
//object.key()返回一个数组.包含可枚举属性和方法名称
var storeKeyLen = Object.keys(chrome.webstore).length,
v8Locale = "v8Locale" in window;
return storeKeyLen > 1 ? '360极速浏览器' : '360安全浏览器';
}
return "Chrome";
}
return System.type;
};
//浏览器名称(如果是壳浏览器,则返回壳名称)
System.name = System.shell();
//对版本号进行过滤过处理
// System.version = System.versionFilter(System.version);
} catch (e) {
// console.log(e.message);
}
return {
client: System
};
})(window);
if (Browser.client.name == undefined || Browser.client.name=="") {
Browser.client.name = "Unknown";
Browser.client.version = "Unknown";
}else if(Browser.client.version == undefined){
Browser.client.version = "Unknown";
}
// document.write(Browser.client.name + " " + Browser.client.version);
return Browser ;
}
以上就是我对桌面通知的一点心得,大家看着玩吧