Google Cloud Messaging
Google Cloud Messaging (GCM) is a service for both Android-powered device and Chrome instances to send and receive message data from servers. GCM Chrome API allows the Chrome apps or extensions to access the GCM service for the signed-in Chrome users. The service works even if an app or extension isn't currently running. For example, calendar updates could be pushed to users even when their calendar app isn't open.
To use both API and service, you must agree to the Google APIs Terms of Service and Google Cloud Platform Terms of Service.
This document describes how to set up and use GCM. For additional information see the reference documentation for the GCM Chrome API and the GCM Service. To get help with GCM or to give us feedback, please see Feedback.
Please note that GCM Chrome API is experimental. It is only available to Chrome users on the dev channel.
API Samples: Want to play with the code? Check out the gcm-notifications sample.
Enable GCM
To use GCM for your app or extension, do the following:
- Upload your app or extension client to the Chrome Web Store.
- A user installs your app or extension.
- Your app or extension client requests a registration ID using gcm.register function and sends this ID to your server together with information identifying the user. For more details, please see Obtain registration ID.
Receive a message
At a glance, receiving a message works like this:
- Your app or extension client should register a handler to receive the gcm.onMessage event.
- When your server sends a message to the user, it specifies all of the registration IDs that are related to that user and passes the message to the GCM service.
- GCM servers route the message to all instances of Chrome running apps or extensions with one of the registration IDs.
- When the message arrives on the client, Chrome starts the app or extension, if it is not already running, and calls the registered handler to process the message.
For more details, please see Receive messages.
Send a message
At a glance, sending a message works like this:
- Your app or extension generates a unique message ID, so that it is possible to find out which message failed to be queued or delivered.
- Your app or extension calls gcm.send function with an object containing message ID, destination ID that identifies the server, and data that consist of a list of key value pairs. In this step you can also provide an optional time-to-live for the message.
- The callback passed to gcm.send function will be called to notify the result. Your app or extension should check runtime.lastError to find out if the message has been successfully queued for delivery. Please refer to Error reference for possible error codes that could be returned.
- In cases that the queued message could not be delivered to the GCM server, like network error, a gcm.onSendError event will be fired. Your app or extension can listen to this event and react to it, e.g. by trying to resend the message. Please refer to Error reference for possible error codes that could be returned.
For more details, please see Send messages.
Set up project
Create Google API project
- Login to the Google Developers
Console using the same Google
Account that you will use to upload your app or extension. - If you haven't created an API project yet, click Create Project.
- Supply a project name and click Create.
- Once the project has been created, a page appears that displays your project number. For example, Project Number: 670330094152.
- Copy down your project number. You will use it later on as the GCM sender ID.
Enable the GCM service
- In the sidebar on the left, select APIs & auth.
- In the displayed list of APIs, turn the Google Cloud Messaging for Android toggle to ON.
Set up Chrome App or Extension
Add permission to manifest
To use the gcm service, you must declare the gcm
permission in
manifest.json
.
"permissions": [
"gcm", "storage", ...
]
Please note that the storage
permission is also provided here because the
sample codes below needs to persist some data via the
chrome.storage API.
Write Chrome App or Extension
Obtain registration ID
Your app or extension needs to register with GCM servers before it can receive
messages. When an app or extension registers, it receives a registration ID.
This is achieved by calling gcm.register and specifying a list of senders
identified by project numbers from Google Developers Console. Your app or
extension should pass a callback function to verify that the registration was
successful. If successful, the received registration ID should be sent back to
your application server in a secure way, for example, via https. Otherwise, your
app or extension should handle the error identified by
chrome.runtime.lastError
and retry later.
If your app or extension wishes to receive messages from the different senders, it can call gcm.register again with the new sender list and the new registration ID will be returned.
function registerCallback(registrationId) {
if (chrome.runtime.lastError) {
// When the registration fails, handle the error and retry the
// registration later.
return;
}
// Send the registration ID to your application server.
sendRegistrationId(function(succeed) {
// Once the registration ID is received by your server,
// set the flag such that register will not be invoked
// next time when the app starts up.
if (succeed)
chrome.storage.local.set({registered: true});
});
}
function sendRegistrationId(callback) {
// Send the registration ID to your application server
// in a secure way.
}
chrome.runtime.onStartup.addListener(function() {
chrome.storage.local.get("registered", function(result) {
// If already registered, bail out.
if (result["registered"])
return;
// Up to 100 senders are allowed.
var senderIds = ["Your-Sender-ID"];
chrome.gcm.register(senderIds, registerCallback);
});
});
If your app or extension is installed in different profiles and/or in different Chrome instances, each of them will receive a different registration ID.
Your app or extension could call gcm.unregister to revoke the registration ID. The unregistration should only be done in rare cases, such as if your app or extension does not want to receive further messages, or the registration ID is suspected to be compromised.
function unregisterCallback() {
if (chrome.runtime.lastError) {
// When the unregistration fails, handle the error and retry
// the unregistration later.
return;
}
}
chrome.gcm.unregister(unregisterCallback);
Your app or extension is automatically unregistered from the GCM service when a user uninstalls it.
Receive messages
When your server sends a message to the user, it specifies all of the registration IDs that are related to that user and passes the message to the GCM service. GCM servers route the message to all instances of Chrome running apps or extensions with one of the registration IDs. If your app or extension has been installed in more than one profiles in a single Chrome instance, all of them can receive messages independently based on their unique registration IDs.
Messages from the server are delivered via the gcm.onMessage event. Your app or extension must register a handler to receive this event.
chrome.gcm.onMessage.addListener(function(message) {
// A message is an object with a data property that
// consists of key-value pairs.
});
As long as Chrome is running, even if the extension or app is not running, it is woken up to deliver a message.
Send messages
To send messages upstream, your app or extension should call gcm.send with an object containing:
- Message ID that identifies the message when it fails to be queued or delivered. The message ID can be any kind of string. However, it is recommended to stay unique across the lifetime of your app or extension, even after it restarts. If you use the same message ID, there may be a chance that the previous message gets overridden. If an auto-increment counter is used to create the message ID, your app or extension should persist the counter value via chrome.storage API and restore it when the app reloads.
- Destination ID that identifies the server. This is the project number from the Google Developers Console plus the suffix "@gcm.googleapis.com".
- Data that consist of a list of string-to-string key value pairs (up to 4KB total).
- Time-to-live (TTL, optional). This property value must be a duration from 0 to 2,419,200 seconds (4 weeks) and it corresponds to the maximum period of time for which GCM will store and try to deliver the message. If this property is not set, it is default to the maximum value. When a TTL is set to 0, GCM will try to deliver the message immediately. If the immediate effort fails, the message will be discarded.
When the callback passed in gcm.send is called without runtime error, it does not mean that the message was already delivered to the GCM server. Rather, it means that it was queued for delivery. If the message fails to reach the destination within the specified TTL period, for example due to network error, the gcm.onSendError will be fired.
// Substitute your own sender ID here. This is the project
// number you got from the Google Developers Console.
var senderId = "Your-Sender-ID";
// Make the message ID unique across the lifetime of your app.
// One way to achieve this is to use the auto-increment counter
// that is persisted to local storage.
// Message ID is saved to and restored from local storage.
var messageId = 0;
chrome.storage.local.get("messageId", function(result) {
if (chrome.runtime.lastError)
return;
messageId = parseInt(result["messageId"]);
if (isNaN(messageId))
messageId = 0;
});
// Sets up an event listener for send error.
chrome.gcm.onSendError.addListener(sendError);
// Returns a new ID to identify the message.
function getMessageId() {
messageId++;
chrome.storage.local.set({messageId: messageId});
return messageId.toString();
}
function sendMessage() {
var message = {
messageId: getMessageId(),
destinationId: senderId + "@gcm.googleapis.com",
timeToLive: 86400, // 1 day
data: {
"key1": "value1",
"key2": "value2"
}
};
chrome.gcm.send(message, function(messageId) {
if (chrome.runtime.lastError) {
// Some error occurred. Fail gracefully or try to send
// again.
return;
}
// The message has been accepted for delivery. If the message
// can not reach the destination, onSendError event will be
// fired.
});
}
function sendError(error) {
console.log("Message " + error.messageId +
" failed to be sent: " + error.errorMessage);
}
Messages deleted event
GCM will store up to 100 non-collapsible messages. After that, all messages are discarded from GCM, and an event gcm.onMessagesDeleted will be fired, which tells the client that it falls behind. Your app or extension should respond by syncing with your application server to recover the discarded messages.
chrome.gcm.onMessagesDeleted.addListener(messagesDeleted);
function messagesDeleted() {
// All messages have been discarded from GCM. Sync with
// your application server to recover from the situation.
}
Collapsible messages
GCM messages are often a tickle, telling the app or extension to contact the
server for fresh data. In GCM, it's possible to create collapsible messages for
this situation, wherein new messages replace older ones. When a collapse key is
provided and multiple messages are queued up in the GCM servers for the same
user, only the last one with any given collapse key is delivered to your app or
extension. As a result the message
object passed to the gcm.onMessage event
will contain a collapseKey
field. For more details about sending collapsible
messages, please refer to GCM Advance
Topics.
Publish your app
To use the GCM service, you must publish your app in the Chrome Web Store.
Error reference
An error could occur when a gcm API function is called. Your app or extension should check runtime.lastError for more information in your callback. The error code will also be passed as a parameter to gcm.onSendError event.
Here's a brief summary of the gcm errors:
- Function was called with invalid parameters: this could happen when gcm functions are called with bad parameters.
- Profile was not signed in: this could happen when gcm functions are called from a profile that was not signed in.
- Asynchronous operation is pending: this could happen when certain gcm function is called again without waiting for the callback passed in previous function to be called.
- Network error occurred: this could happen when GCM server fails to reach due to network problem, like losing Internet connection.
- Server error occurred: this could happen when GCM server fails to reach due to server problem, like server busy.
- Time-to-live exceeded: this could happen when a message could not be delivered within the specific time-to-live period.
- Unknown error occurred: this could happen due to any other internal errors.
Feedback
You can provide feedback about Google Cloud Messaging and the gcm API through the Google Group GCM for Chrome Feedback. Use this group to ask for help, file bug reports, and request features.