Notifications while browser is closed

2019-05-24 03:18发布

问题:

I have a chrome-extension that sends notifications to the user when he visits a specific page. I wish to send the notification when the browser is closed or running in background, similar to the smartphones. Any idea or tip will be great!

回答1:

In Chrome extensions, you have 2 tools at your disposal:

  1. chrome.gcm API, which allows you to use Google Cloud Messaging to receive push notifications. Conveniently, it's the same system Android apps use. Your server will need to talk to GCM servers to keep track of subscriptions and send the message. Details are outside the scope of this question; there's a tutorial though.

  2. "background" permission, allowing your extension's background page to work even when the browser is closed. However, note that the user can disable Chrome running in the background altogether, and in that case you'll be limited to the time the browser is normally running (which is the normal way extensions operate). Note that the push message will be delivered ASAP even if the browser was closed at the time of sending it.

If you don't want to use Google-provided push tools, you can either poll regularly for notifications from a background script, or implement your own push messaging with, say, WebSockets (it's going to be expensive in terms of server resources, though, if you get many users).