有没有办法在 Chrome 或 Firefox 关闭时显示桌面通知?

Is there a way to display desktop notifications even when Chrome or Firefox is closed?

我们正在开发一个使用 GCM 向最终用户发送推送通知的网站。我们已经了解了 Service Worker 等等。我们使用此 codelab 教程开发了一个原型。到目前为止它正在运行,但唯一的问题是只有在 Chrome 打开时才会显示通知。当 Chrome 关闭时,通知不会到达用户。

我想知道有什么方法可以克服这个问题,即使在浏览器关闭时也能显示通知,类似于 Safari 推送通知。提前致谢!

如果您在 manifest.json 中拥有“background”权限,即使 Chrome window 已关闭,您的后台页面也将能够显示通知。

"permissions": [
    "background"
],

documentation 中所述:

When any installed hosted app, packaged app, or extension has "background" permission, Chrome runs (invisibly) as soon as the user logs into their computer—before the user launches Chrome. The "background" permission also makes Chrome continue running (even after its last window is closed) until the user explicitly quits Chrome.

您需要将 "background" 权限与 background page, event page or a background window 一起用于托管应用程序。

对于网络,请为 Chrome 和其他浏览器使用 Push API。使用推送消息的好处是,即使您的页面关闭,您的 service worker 也会被唤醒并能够显示通知。 Web Sockets 和 EventSource 会在页面或浏览器关闭时关闭它们的连接,因此不推荐这样做。这是 documentation and example.

在Chrome。当 chrome 不是 "running" 时,只有那些碰巧安装了需要后台模式的扩展程序(如环聊)的用户才能收到推送通知。依赖它似乎不是一个好主意。

chrome 团队似乎也在考虑将其用于网络推送,但到目前为止还没有预计到达时间。

https://code.google.com/p/chromium/issues/detail?id=402456

当浏览器或标签页关闭时显示通知需要 service worker 和第三方服务(如 google firebase)来触发 service worker。

https://github.com/web-push-libs/ - 检查这些库列表以在不同平台上实现它。

https://github.com/rijoshrc/php-service-worker-push-notification - 克隆此 git 存储库以查看 PHP 中的简单实现。