我是新增浏览器通知。如何使用浏览器通知?我无法弄清楚从哪里开始。任何人都可以提供关于如何开始这个建议。如何使用浏览器通知
1
A
回答
0
对于浏览器通知工作您的网站或应用程序应通过https否则浏览器不会允许它
mesg = {
title: "notification title",
body: "notification body",
icon: "location to an .ico image",
timer: true //for auto closing
}
// Let's check if the browser supports notifications
if (!('Notification' in window)) {
console.log('Browser does not support this feature.');
}else if (Notification.permission === 'granted') {
Notification.requireInteraction = false;
if (mesg.title !== 'undefined') {
const notification = new Notification(mesg.title, {
body: mesg.body,
icon: mesg.icon
});
if (mesg.timer) {
setTimeout(function() {
notification.close();
}, 5000);
}
return notification;
}// close if undefined
} else if (Notification.permission !== 'denied') {
alert('Please click Allow for enabling browser notifications');
Notification.requestPermission(function (permission) {
// If the user accepts, let's create a notification
if (permission === 'granted') {
if (mesg.title !== 'undefined') {
const notification = new Notification(mesg.title, {
body: mesg.body,
icon: mesg.icon
});
if (mesg.timer) {
setTimeout(function() {
notification.close();
}, 5000);
}
return notification;
}// close if undefined
} else {
alert('Permission Denied :[');
}
});
}
我用这个我的应用程序,你可以重构它来除去重复的代码。
0
检查Notification API为引用
代码请求权限
document.addEventListener('DOMContentLoaded', function() {
if (!Notification) {
alert('Desktop notifications not available in your browser. Try Chromium.');
return;
}
if (Notification.permission !== "granted")
Notification.requestPermission();
});
和用于显示通知
if (Notification.permission !== "granted")
Notification.requestPermission();
else {
var notification = new Notification('Notification title', {
icon: 'Icon Link',
body: "Notification Body",
});
notification.onclick = function() {
window.open("Href Here");
};
}
0
例如
function notifyMe() {
// Let's check if the browser supports notifications
if (!("Notification" in window)) {
alert("This browser does not support desktop notification");
}
// Let's check whether notification permissions have already been granted
else if (Notification.permission === "granted") {
// If it's okay let's create a notification
var notification = new Notification("Hi there!");
}
// Otherwise, we need to ask the user for permission
else if (Notification.permission !== "denied") {
Notification.requestPermission(function (permission) {
// If the user accepts, let's create a notification
if (permission === "granted") {
var notification = new Notification("Hi there!");
}
});
}
// At last, if the user has denied notifications, and you
// want to be respectful there is no need to bother them any more.
}
notifyMe();
月re info https://developer.mozilla.org/id/docs/Web/API/notification
相关问题
- 1. 浏览器通知使用AJAX
- 2. SNS通知浏览器
- 3. 如果浏览器关闭,Chrome浏览器推送通知不起作用?
- 4. 如何通过浏览器
- 5. 如何禁用浏览器浏览器?
- 6. 如何通知Javascript是否在浏览器中被禁用
- 7. 如何向选定用户的浏览器发送通知
- 8. 跨浏览器替代Webkit/Html通知
- 9. 浏览器大小更改通知?
- 10. android推送通知打开浏览器
- 11. 通知 - HTML /浏览器/ WebKit的/桌面
- 12. 移动Web浏览器声音通知
- 13. 通知当浏览器退出
- 14. JavaScript - 浏览器中的通知API
- 15. 浏览器如何知道scss文件?
- 16. 如何知道用户正在使用的浏览器?
- 17. 如何在浏览器中使用网页浏览器控件?
- 18. 可嵌入浏览器(chrome)使用GCM进行推送通知
- 19. 如何使用Java将推送通知发送到Firefox浏览器
- 20. 如何使用java处理selenium中的浏览器通知弹出窗口?
- 21. 如何将通知推送到浏览器窗口?
- 22. 如何在Conkeror浏览器中配置桌面通知?
- 23. 如何捕获或收听浏览器通知?
- 24. 如何从.Net中的线程向浏览器发送通知?
- 25. 如何在特定时间发送浏览器通知?
- 26. android-如何打开浏览器的通知点击
- 27. 如何自定义浏览器webpush通知正文和图标?
- 28. WP7网页浏览器控件使用IE浏览器,如何使用不同的浏览器
- 29. 使用Cookie进行浏览器通信
- 30. 浏览器通知Chrome浏览器扩展JS事件+修改方法