javascript - 如何在我的客户应用程序中实现桌面通知?

标签 javascript html

我正在尝试在我的客户应用程序中开发桌面通知 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/

相关文章:

javascript - 在javascript中合并数组内的数组

javascript - 如何使用javascript提取属性值

html - 表格行与表格标题的宽度不匹配

jquery - CSS - 设置高度页眉/页脚,动态内容..不适用于 osXsafari/iOSsafari? (jsBin)

jquery - 为什么图像不是 100% 以及如何使其适应浏览器?

javascript - 滑动和淡出一个 div 元素

javascript - 选择 "other"字段时将选择选项发布到数据库

javascript - Promise `.then` 、 `.catch` 和 `.finally` 如何以及何时进入 EventLoop 微任务队列?

javascript - 在没有jquery的情况下,将类添加到javascript中指定元素的所有子级(第一级或层次结构中)

javascript - jquery选项卡选择问题