有没有办法在 Chrome 或 Firefox 关闭时显示桌面通知? [英] Is there a way to display desktop notifications even when Chrome or Firefox is closed?

查看:93
本文介绍了有没有办法在 Chrome 或 Firefox 关闭时显示桌面通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

解决方案

如果您有背景" 权限,即使 Chrome 窗口关闭,您的后台页面也能显示通知.

"permissions": [背景"],

文档中所述:

<块引用>

当任何已安装的托管应用、打包应用或扩展程序具有后台"权限时,Chrome 会在用户登录计算机后立即运行(不可见)——在用户启动 Chrome 之前.后台"权限还使 Chrome 继续运行(即使在其最后一个窗口关闭后),直到用户明确退出 Chrome.

您需要在背景页面中使用背景"权限,事件页面后台窗口.

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

We're developing a website which sends push notifications to end users using GCM. We've gone through Service Worker and all. We have developed a prototype using this codelab tutorial. It is working so far, but the only issue is the notifications are displayed only when Chrome is opened. When Chrome is closed, the notifications don't reach the users.

I want to know is there any way we can overcome this and display the notifications even when the browser is closed, similar to Safari Push Notification. Thanks in advance!

解决方案

If you have a "background" permission in manifest.json, your background page will be able to show notifications even when Chrome window is closed.

"permissions": [
    "background"
],

As stated in the 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.

You need to use the "background" permission with a background page, event page or a background window for hosted apps.

For web, use Push API for Chrome and other browsers. The advantage of using push messages is that even if your page is closed, your service worker will be woken up and be able to show a notification. Web Sockets and EventSource have their connection closed when the page or browser is closed so it's not recommended. Here is the documentation and example.

这篇关于有没有办法在 Chrome 或 Firefox 关闭时显示桌面通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆