Thursday, May 09, 2013
Event pages keep apps and extensions efficient by allowing them to respond to a variety of events such as timers or navigation to a particular site, without having to remain running persistently. But what if you need to respond to something that occurs outside of Chrome, such as a news alert, a message sent to a user or a stock hitting a price threshold? Until now, you had to do this by repeatedly polling a server. This process consumed bandwidth and reduced the battery life of your users’ machines. For a more efficient solution, starting today you can use Google Cloud Messaging for Chrome (GCM) - across all channels of Chrome.
GCM will be familiar to developers who have used Google Cloud Messaging for Android. To send a message, all you need to do is:
- Request a token (channel ID) via chrome.pushMessaging.getChannelId().
- Pass the returned token to your server.
- Whenever you need to send a message to your app or extension, post the message along with the token to the GCM server-side API.
To use GCM, your users must be signed into Chrome, as the service relies on an efficient push channel that’s only established for signed-in users. Messages are automatically delivered to all the devices where the user has signed in and installed your app/extension.
To add GCM to your app/extension, take a look at the overview of the service or start with some of the sample apps—and start pushing!