Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 <meta name="doc-family" content="apps"> | |
| 2 <h1 id="gcmForChrome">Google Cloud Messaging for Chrome</h1> | |
| 3 | |
| 4 <p>Google Cloud Messaging for Chrome (GCM) is a service for signed-in Chrome use rs | |
| 5 that helps developers send and receive message data from servers to their Chrome | |
| 6 apps and extensions. The service works even if an app or extension isn't | |
| 7 currently running. For example, calendar updates could be pushed to users even | |
| 8 when their calendar app isn't open. The service could also be used to send | |
| 9 upstream messages to the server.</p> | |
| 10 | |
| 11 <p>To use both API and service, you must agree to the <a href="https://developer s.google.com/terms/">Google APIs Terms of | |
| 12 Service</a> and <a href="https://developers.google.com/cloud/terms/">Google Clou d Platform Terms | |
| 13 of Service</a>.</p> | |
| 14 | |
| 15 <p>This document describes how to set up and use GCM. For additional information | |
| 16 see the reference documentation for the <a href="gcm.html">GCM Chrome API</a> an d | |
| 17 the <a href="http://developer.android.com/google/gcm/index.html">GCM Service</a> . To get | |
| 18 help with GCM or to give us feedback, please see | |
| 19 <a href="#feedback">Feedback</a>.</p> | |
| 20 | |
| 21 <p>Please note that GCM Chrome API is experimental. It is only available to Chro me | |
| 22 users on the dev channel.</p> | |
| 23 | |
| 24 <h2 id="t1">Enable GCM</h2> | |
| 25 | |
| 26 <p>To use GCM for your app or extension, do the following:</p> | |
| 27 | |
| 28 <ol> | |
| 29 <li>Upload your app or extension client to the <a href="https://developers.googl e.com/chrome/web-store/docs/publish">Chrome Web | |
| 30 Store</a>.</li> | |
| 31 <li>A user installs your app or extension.</li> | |
| 32 <li>Your app or extension client requests a registration ID using $ref:gcm.regis ter | |
| 33 function and sends this ID to your server together with information | |
| 34 identifying the user. For more details, please see <a href="#obtain_registration _id">Obtain registration | |
| 35 ID</a>.</li> | |
| 36 </ol> | |
| 37 | |
| 38 <h2 id="t2">Receive a message</h2> | |
| 39 | |
| 40 <p>At a glance, receiving a message works like this:</p> | |
| 41 | |
| 42 <ol> | |
| 43 <li>Your app or extension client should register a handler to receive the | |
| 44 $ref:gcm.onMessage event.</li> | |
| 45 <li>When your server sends a message to the user, it specifies all of the | |
| 46 registration IDs that are related to that user and passes the message to the | |
| 47 GCM service.</li> | |
| 48 <li>GCM servers route the message to all instances of Chrome running apps or | |
| 49 extensions with one of the registration IDs.</li> | |
| 50 <li>When the message arrives on the client, Chrome starts the app or extension, | |
| 51 if it is not already running, and calls the registered handler to process the | |
| 52 message.</li> | |
| 53 </ol> | |
| 54 | |
| 55 <p>For more details, please see <a href="#receive_messages">Receive messages</a> .</p> | |
| 56 | |
| 57 <h2 id="t3">Send a message</h2> | |
| 58 | |
| 59 <p>At a glance, sending a message works like this:</p> | |
| 60 | |
| 61 <ol> | |
| 62 <li>Your app or extension generates a unique message ID, so that it is possible | |
| 63 to find out which message failed to be queued or delivered.</li> | |
| 64 <li>Your app or extension calls $ref:gcm.send function with an object containing | |
| 65 message ID, destination ID that identifies the server, and data that consist | |
| 66 of a list of key value pairs. In this step you can also provide an optional | |
| 67 time-to-live for the message.</li> | |
| 68 <li>The callback passed to $ref:gcm.send function will be called to notify the | |
| 69 result. Your app or extension should check $ref:runtime.lastError to find out if | |
| 70 the message has been successfully queued for delivery. Please refer to <a href=" #error_reference">Error | |
| 71 reference</a> for possible error codes that could be | |
| 72 returned.</li> | |
| 73 <li>In cases that the queued message could not be delivered to the GCM server, | |
| 74 like network error, a $ref:gcm.onSendError event will be fired. Your app or | |
| 75 extension can listen to this event and react to it, e.g. by trying to resend | |
| 76 the message. Please refer to <a href="#error_reference">Error reference</a> for | |
| 77 possible error codes that could be returned.</li> | |
| 78 </ol> | |
| 79 | |
| 80 <p>For more details, please see <a href="#send_messages">Send messages</a>.</p> | |
| 81 | |
| 82 <h2 id="t4">Set up project</h2> | |
| 83 | |
| 84 <h3 id="t5">Create Google API project</h3> | |
| 85 | |
| 86 <ol> | |
| 87 <li>Login to the <a href="https://cloud.google.com/console/project">Google Devel opers | |
| 88 Console</a> using the same Google <br> | |
| 89 Account that you will use to upload your app or extension.</li> | |
| 90 <li>If you haven't created an API project yet, click <strong>Create Project</str ong>.</li> | |
| 91 <li>Supply a project name and click <strong>Create</strong>.</li> | |
| 92 <li>Once the project has been created, a page appears that displays your project | |
| 93 number. For example, <strong>Project Number: 670330094152</strong>.</li> | |
| 94 <li>Copy down your project number. You will use it later on as the GCM sender ID .</li> | |
| 95 </ol> | |
| 96 | |
| 97 <h3 id="t6">Enable the GCM service</h3> | |
| 98 | |
| 99 <ol> | |
| 100 <li>In the sidebar on the left, select <strong>APIs & auth</strong>.</li> | |
| 101 <li>In the displayed list of APIs, turn the <strong>Google Cloud Messaging for | |
| 102 Android</strong> toggle to ON.</li> | |
| 103 </ol> | |
| 104 | |
| 105 <h2 id="t7">Set up Chrome App or Extension</h2> | |
| 106 | |
| 107 <h3 id="t8">Add permission to manifest</h3> | |
| 108 | |
| 109 <p>To use the gcm service, you must declare the <code>gcm</code> permission in | |
| 110 <code>manifest.json</code>.</p> | |
| 111 | |
| 112 <pre data-filename="manifest.json"><code> | |
| 113 "permissions": [ | |
| 114 "gcm", "storage", ... | |
| 115 ] | |
| 116 </code></pre> | |
| 117 | |
| 118 <p>Please note that the <code>storage</code> permission is also provided here be cause the | |
| 119 sample codes below needs to persist some data via the | |
| 120 <a href="storage.html">chrome.storage</a> API.</p> | |
| 121 | |
| 122 <h2 id="t9">Write Chrome App or Extension</h2> | |
| 123 | |
| 124 <h3 id="obtain_registration_id">Obtain registration ID</h3> | |
| 125 | |
| 126 <p>Your app or extension needs to register with GCM servers before it can receiv e | |
| 127 messages. When an app or extension registers, it receives a registration ID, | |
| 128 that is linked to your app or extension that is installed to this profile.</p> | |
| 129 | |
| 130 <p>When your app or extension launches, it needs to call $ref:gcm.register and s pecify | |
| 131 the list of senders (project IDs from Google Developers Console). Your app or | |
| 132 extension should pass a callback function to verify that registration was | |
| 133 successful and send the registration ID back to your application server if the | |
| 134 registration ID is produced for the first time or it is different from last | |
| 135 time. For example, your app or extension could send an XHR request to a RESTful | |
| 136 API on your server. Whatever method your app or extension uses to send the | |
| 137 registration ID to the server, it must be secured (https, for instance).</p> | |
| 138 | |
| 139 <p>When registration fails, the app or extension should retry the failed operati on | |
| 140 using exponential back-off. In exponential back-off, each time there is a | |
| 141 failure, it should wait twice the previous amount of time before trying again.</ p> | |
| 142 | |
| 143 <pre data-filename="background.js"><code> | |
| 144 function registerCompleted(registrationId) { | |
| 145 if (chrome.runtime.lastError) { | |
| 146 // When registration fails, try to register again using | |
| 147 // exponential back-off. | |
| 148 return; | |
| 149 } | |
| 150 | |
| 151 // The registration ID returned from the GCM might change. | |
| 152 // Read the registration ID that is obtained last time | |
| 153 // when your app or extension launches. | |
| 154 chrome.storage.local.get("regID", function(result) { | |
| 155 if (registrationId != result["regID"]) { | |
| 156 // Send the registration ID to your application server. | |
| 157 | |
| 158 // Save the new registration ID. | |
| 159 chrome.storage.local.set({regID: registrationId}); | |
| 160 } | |
| 161 }; | |
| 162 } | |
| 163 | |
| 164 chrome.runtime.onLaunched.addListener(function() { | |
| 165 // Up to 100 senders are allowed. | |
| 166 var senderIds = ["Your-Sender-ID"]; | |
| 167 chrome.gcm.register(senderIds, registerCompleted); | |
| 168 }); | |
| 169 </code></pre> | |
| 170 | |
| 171 <p>Your app or extension is automatically unregistered from the GCM service when a | |
| 172 user uninstalls it. You can't unregister using the GCM API.</p> | |
| 173 | |
| 174 <h3 id="receive_messages">Receive messages</h3> | |
| 175 | |
| 176 <p>When your server sends a message to the user, it specifies all of the | |
| 177 registration IDs that are related to that user and passes the message to the GCM | |
| 178 service. GCM servers route the message to all instances of Chrome running apps | |
| 179 or extensions with one of the registration IDs. If your app or extension has | |
| 180 been installed in more than one profiles in a single Chrome instance, all of | |
| 181 them can receive messages independently based on their unique registration IDs.< /p> | |
| 182 | |
| 183 <p>Messages from the server are delivered via the $ref:gcm.onMessage event. Your app | |
| 184 or extension must register a handler to receive this event when loading, similar | |
| 185 to how they'd register for launch events.</p> | |
| 186 | |
| 187 <pre data-filename="background.js"><code> | |
| 188 chrome.app.runtime.onLaunched.addListener(function() { | |
| 189 chrome.gcm.onMessage.addListener(function(message) { | |
| 190 // A message is an object with a data property that | |
| 191 // consists of key-value pairs. | |
| 192 }); | |
| 193 }); | |
| 194 </code></pre> | |
| 195 | |
| 196 <p>As long as Chrome is running, even if the extension or app is not running, it is | |
| 197 woken up to deliver a message.</p> | |
| 198 | |
| 199 <h3 id="send_messages">Send messages</h3> | |
| 200 | |
| 201 <p>To send messages upstream, your app or extension should call $ref:gcm.send wi th an | |
| 202 object containing:</p> | |
| 203 | |
| 204 <ul> | |
| 205 <li>Message ID that identifies the message when it fails to be queued or | |
| 206 delivered. The message ID should be unique across the lifetime of your app or | |
| 207 extension, even after it restarts. If an auto-increment counter is used to | |
| 208 create the message ID, your app or extension should persist the counter value | |
| 209 via <a href="storage.html">chrome.storage</a> API and restore it when the app | |
| 210 reloads.</li> | |
| 211 <li>Destination ID that identifies the server. This is the project number return ed | |
| 212 from the Google Developers Console plus the suffix "@gcm.googleapis.com".</li> | |
| 213 <li>Data that consist of a list of string-to-string key value pairs (up to 4KB | |
| 214 total).</li> | |
| 215 <li>Time-to-live (TTL, optional). This property value must be a duration from 0 to | |
| 216 2,419,200 seconds (4 weeks) and it corresponds to the maximum period of time | |
| 217 for which GCM will store and try to deliver the message. If this property is | |
| 218 not set, it is default to the maximum value. When a TTL is set to 0, GCM will | |
| 219 try to deliver the message immediately. If the immediate effort fails, the | |
| 220 message will be discarded.</li> | |
| 221 </ul> | |
| 222 | |
| 223 <p>When the callback passed in $ref:gcm.send is called without runtime error, it does | |
| 224 not mean that the message was already delivered to the GCM server. Rather, it | |
| 225 means that it was queued for delivery. If the message fails to reach the GCM | |
| 226 server, for example due to network error, the $ref:gcm.onSendError will be fired .</p> | |
| 227 | |
| 228 <pre data-filename="background.js"><code> | |
| 229 // Substitute your own sender ID here. This is the project | |
| 230 // number you got from the Google Developers Console. | |
| 231 var senderId = "Your-Sender-ID"; | |
| 232 | |
| 233 // Message ID should be unique across the lifetime of your app. | |
| 234 // One way to achieve this is to use the auto-increment counter | |
| 235 // that is persisted to local storage. | |
| 236 | |
| 237 // Message ID is saved to and restored from local storage. | |
| 238 var messageId = 0; | |
| 239 | |
| 240 chrome.app.runtime.onLaunched.addListener(function() { | |
| 241 // Restores the message ID from local storage. | |
| 242 chrome.storage.local.get("messageId", function(result) { | |
| 243 if (chrome.runtime.lastError) | |
| 244 return; | |
| 245 messageId = parseInt(result["messageId"]); | |
| 246 if (isNaN(messageId)) | |
| 247 messageId = 0; | |
| 248 }); | |
| 249 | |
| 250 // Sets up an event listener for send error. | |
| 251 chrome.gcm.onSendError.addListener(sendError); | |
| 252 }); | |
| 253 | |
| 254 // Returns a new ID to identify the message. | |
| 255 function getMessageId() { | |
| 256 messageId++; | |
| 257 chrome.storage.local.set({messageId: messageId}); | |
| 258 return messageId.toString(); | |
| 259 } | |
| 260 | |
| 261 function sendMessage() { | |
| 262 var message = { | |
| 263 messageId: getMessageId(), | |
| 264 destinationId: senderId + "@gcm.googleapis.com", | |
| 265 timeToLive: 86400, // 1 day | |
| 266 data: { | |
| 267 "key1": "value1", | |
| 268 "key2": "value2" | |
| 269 } | |
| 270 }; | |
| 271 chrome.gcm.send(message, function(messageId) { | |
| 272 if (chrome.runtime.lastError) { | |
| 273 // Some error occurred. Fail gracefully or try to send | |
| 274 // again. | |
| 275 return; | |
| 276 } | |
| 277 | |
| 278 // Message is accepted and queued. | |
| 279 }); | |
| 280 } | |
| 281 | |
| 282 function sendError(error) { | |
| 283 console.log("Message " + error.messageId + | |
| 284 " failed to be sent: " + error.errorMessage); | |
| 285 } | |
| 286 </code></pre> | |
| 287 | |
| 288 <h3 id="t10">Maximum number of messages stored in GCM</h3> | |
|
fgorski
2014/03/05 23:45:19
we could add an <a href="#on_messages_deleted"> so
| |
| 289 | |
| 290 <p>GCM will store up to 100 messages. After that, all messages are discarded fro m | |
| 291 GCM, and an event $ref:gcm.onMessagesDeleted will be fired, which tells the clie nt | |
| 292 that it falls behind. Your app or extension should respond by syncing with your | |
| 293 application server to recover the discarded messages.</p> | |
| 294 | |
| 295 <pre data-filename="background.js"><code> | |
| 296 chrome.app.runtime.onLaunched.addListener(function() { | |
| 297 chrome.gcm.onMessagesDeleted.addListener(messagesDeleted); | |
| 298 }); | |
| 299 | |
| 300 function messagesDeleted() { | |
| 301 // All messages have been discarded from GCM. Sync with | |
| 302 // your application server to recover from the situation. | |
| 303 } | |
| 304 </code></pre> | |
| 305 | |
| 306 <h2 id="t11">Publish your app</h2> | |
| 307 | |
| 308 <p>To use the GCM service, you must publish your app in the <a href="https://dev elopers.google.com/chrome/web-store/docs/publish">Chrome Web | |
| 309 Store</a>.</p> | |
| 310 | |
| 311 <h2 id="error_reference">Error reference</h2> | |
| 312 | |
| 313 <p>An error could occur when a gcm API function is called. Your app or extension | |
| 314 should check $ref:runtime.lastError for more information in your callback. The | |
| 315 error code will also be passed as a parameter to $ref:gcm.onSendError event.</p> | |
| 316 | |
| 317 <p>Here's a brief summary of the gcm errors:</p> | |
| 318 | |
| 319 <ul> | |
| 320 <li><strong>Function was called with invalid parameters</strong>: this could hap pen when gcm | |
| 321 functions are called with bad parameters.</li> | |
| 322 <li><strong>Profile was not signed in</strong>: this could happen when gcm funct ions are called | |
| 323 from a profile that was not signed in.</li> | |
| 324 <li><strong>Asynchronous operation is pending</strong>: this could happen when c ertain gcm | |
| 325 function is called again without waiting for the callback passed in previous | |
| 326 function to be called.</li> | |
| 327 <li><strong>Network error occurred</strong>: this could happen when GCM server f ails to reach | |
| 328 due to network problem, like losing Internet connection.</li> | |
| 329 <li><strong>Server error occurred</strong>: this could happen when GCM server fa ils to reach | |
| 330 due to server problem, like server busy.</li> | |
| 331 <li><strong>Time-to-live exceeded</strong>: this could happen when a message cou ld not be | |
| 332 delivered within the specific time-to-live period.</li> | |
| 333 <li><strong>Unknown error occurred</strong>: this could happen due to any other internal | |
| 334 errors.</li> | |
| 335 </ul> | |
| 336 | |
| 337 <h2 id="feedback">Feedback</h2> | |
| 338 | |
| 339 <p>You can provide feedback about Google Cloud Messaging and the gcm API through | |
| 340 the Google Group <a href="https://groups.google.com/forum/#!forum/gcm-for-chrome -v2-feedback">GCM for Chrome V2 | |
| 341 Feedback</a>. | |
| 342 Use this group to ask for help, file bug reports, and request features.</p> | |
| OLD | NEW |