我正在尝试在我的客户应用程序中开发桌面通知 JS。该代码在 Firefox 上运行良好,但在 Chrome 上则不行。我正在更改通知权限以允许设置 -> 高级 -> 内容设置 -> 通知 -> 允许/拒绝,但仍然不起作用。
这是我的代码:
notifyMe: function() {
if (!("Notification" in window)) {
alert("Ce navigateur ne supporte pas les notifications desktop");
} else if (Notification.permission !== 'denied') {
Notification.requestPermission(function(permission) {
if (!('permission' in Notification)) {
Notification.permission = permission;
}
if (permission === "granted") {
var notification = new Notification("Notify ")
}
});
}
}
有人可以帮助我吗?谢谢。
最佳答案
从 Chrome 版本 62 开始,仅在浏览器的安全上下文中支持通知。
When is a context considered secure?
- A context will be considered secure when it's delivered securely (or locally), and when it cannot be used to provide access to secure APIs to a context that is not secure. In practice, this means that for a page to have a secure context, it and all the pages along its parent and opener chain must have been delivered securely.
- Locally delivered files such as http://localhost and file:// paths are considered to have been delivered securely.
- Contexts that are not local must be served over https:// or wss:// and where the protocols used should not be considered deprecated.
有关安全上下文的更多详细信息,请阅读 MDN Secure Contexts
关于仅在安全上下文中支持通知,请检查Notification的浏览器兼容性部分。
关于javascript - 如何在我的客户应用程序中实现桌面通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54531826/