[Storage] Blob Storage Refactoring pt 1:
[chromium-blink-merge.git] / chrome / common / extensions / docs / templates / articles / cloudMessagingV1.html
blob619f53226813fe134c8fa161f4f039a68d1b1333
1 <meta name="doc-family" content="apps">
2 <h1>Google Cloud Messaging for Chrome V1</h1>
4 <p class="note">
5 Google Cloud Messaging for Chrome V1 is deprecated since Chrome 38.
6 It will be removed in Chrome 41.
7 Use the new <a href="cloudMessaging">Google Cloud Messaging</a>.
8 </p>
10 <p>
11 Google Cloud Messaging for Chrome (GCM) is a service
12 for signed-in Chrome users
13 that helps developers send message data from servers
14 to their Chrome apps and extensions.
15 The service is intended to wake up an app or extension,
16 and/or alert a user.
17 For example, calendar updates could be pushed to users
18 even when their calendaring app isn't open.
19 </p>
21 <p>This document describes how to set up and use GCM.
22 For additional information see the reference documentation
23 for the <a href="pushMessaging">pushMessaging Chrome API</a> and the
24 <a href="gcm_server">GCM service</a>.
25 To get help with GCM or to give us feedback, see <a href="#feedback">Feedback</a>.
26 </p>
28 <h2 id="one">How push messaging works</h2>
30 <p>
31 At a glance, push messaging works like this:
32 </p>
34 <ol>
35 <li>You upload your app or extension client to the Chrome Web Store.</li>
36 <li>A user installs your app or extension.</li>
37 <li>Your app or extension client requests the user's channel ID
38 and sends this ID to your server.</li>
39 <li>Your app or extension server sends a message
40 to the push messaging service.</li>
41 <li>The push messaging service routes the message
42 to all instances of Chrome where the user is signed in.</li>
43 <li>When the app or extension starts,
44 it needs to register a handler to receive the
45 $(ref:pushMessaging.onMessage) event.</li>
46 <li>When the message arrives on the client,
47 Chrome starts the app or extension, if it is not already running,
48 and calls the registered handler.</li>
49 </ol>
51 <p>
52 Diving in a bit more,
53 the Chrome Web Store assigns your newly published app
54 or extension a unique app ID.
55 When a user installs your app or extension,
56 the client needs to call $(ref:pushMessaging.getChannelId).
57 The push messaging service returns a channel ID to the client;
58 this ID is specifically linked to your app ID and to the user.
59 Whatever method your client uses to send the channel ID to the server,
60 it must be secured (https, for instance).
61 For example,
62 the client could send an XHR request
63 to a RESTful API on your server.
64 </p>
66 <p>
67 As long as Chrome is running in the background or foreground,
68 even if the extension or app is not running,
69 it is woken up to deliver a message.
70 For this to work,
71 your app or extension must register a handler to receive the event,
72 similar to how they’d register for launch events.
73 </p>
75 <p>
76 Your app/extension server is responsible
77 for sending a push message to the service.
78 In all push message requests,
79 your server must include the user's channel ID
80 and a valid OAuth 2.0 access token:
81 the access token authorizes use of the service and
82 the channel ID identifies the user and app to receive the message.
83 </p>
85 <p>
86 Any messages sent are delivered
87 to all instances of that application installed
88 in a Chrome profile signed in as that user.
89 The most recent message sent on each subchannel is automatically queued
90 for delivery to instances of Chrome which are not connected to the push
91 messaging service at the time. If multiple messages are sent on one subchannel
92 while Chrome is disconnected, then Chrome may only receive the last one sent
93 when it reconnects.
94 </p>
96 <p>
97 Subchannels can also be used to implement priority schemes.
98 For example,
99 if you had an instant messaging app,
100 requests for a phone call or video chat can go through immediately,
101 instead of waiting for all the backed up chat messages to be cleared.
102 </p>
104 <h2 id="checklist">To Do Checklist</h2>
107 Here's a quick checklist of what you need to do
108 to use the push messaging service
109 (the remainder of this doc covers the steps in detail):
110 </p>
112 <ol>
113 <li>Register your app or extension:
114 <ul>
115 <li>Create the client ID in the Google APIs Console.</li>
116 <li>Get the refresh token to set up authorization to use the service.</li>
117 </ul>
118 </li>
119 <li>Set up your app or extension to use the service:
120 <ul>
121 <li>Add the permission to the manifest.</li>
122 <li>Include a call to <code>getChannelId</code>
123 for any user who is to receive a message.</li>
124 <li>Register a handler to receive the <code>onMessage</code> event.</li>
125 </ul>
126 </li>
127 <li>Publish your app in the Chrome Web Store. </li>
128 <li>Use refresh token to get a valid access token.</li>
129 <li>Send message to user.</li>
130 </ol>
132 <h2 id="two">Register app or extension</h2>
134 <h3 id="clientid">Create client ID</h3>
137 Complete the following steps to create the client ID:
138 </p>
140 <ol>
141 <li>Login to the
142 <a href="https://code.google.com/apis/console/">Google APIs Console</a>
143 using the same Google Account that you will use to upload your app.</li>
144 <li> Create a new project by expanding the drop-down menu in the top-left corner
145 and selecting the <strong>Create...</strong> menu item.</li>
146 <li>Go to the "Services" navigation menu item and
147 turn on the <strong>Google Cloud Messaging for Chrome API</strong>.</li>
148 <li>Go to the "API Access" pane and click on the
149 <strong>Create an OAuth 2.0 client ID...</strong> blue button.</li>
150 <li>Enter the requested branding information, if needed</li>
151 <li>For “Application type” select “Web application”.</li>
152 <li>Click "more options" beside "Your site or hostname"
153 and under "Authorized Redirect URIs", enter the following URL:
154 <code>https://developers.google.com/oauthplayground</code>.</li>
155 <li>Click "Create client ID" button.</li>
156 </ol>
159 The client ID and the client secret
160 from this step are used in further steps.
161 Be sure to keep the client ID and secret in a safe place,
162 and don't expose them to outsiders.
163 </p>
165 <h3 id="refresh">Get refresh token</h3>
168 You need two types of OAuth 2.0 tokens to authorize
169 each call to the push messaging service:
170 the refresh token and the access token.
171 The access token authorizes each call to the service;
172 however, this token expires after about an hour.
173 The refresh token is used
174 to 'refresh' the access token over time.
175 These tokens are scoped to only send messages on behalf
176 of your application or extension and nothing else.
177 </p>
180 To get the refresh token and initial access token:
181 </p>
183 <ol>
184 <li>Open an Incognito window in Chrome;
185 this ensures that you are logged into the correct Google Account.
186 If you only have one Google Account,
187 you don't need to use an incognito window.</li>
188 <li>Go to the
189 <a href="https://developers.google.com/oauthplayground/">OAuth 2.0 Playground</a>.</li>
190 <li>Click the <img src="{{static}}/images/gearsicon.png" width="29" height="23" align="middle"/>
191 <strong>OAuth 2.0 Configuration</strong> button in the top right corner.</li>
192 <li>Check the box "Use your own OAuth credentials",
193 enter the client ID and client secret, and click "Close".</li>
194 <li>In the "Step 1" section, enter the scope
195 <code>https://www.googleapis.com/auth/gcm_for_chrome</code> into the
196 "Input your own scopes" text box and click "Authorize APIs" button.</li>
197 <li>Assuming you are in Incognito mode,
198 you should be redirected to the Google log in page.
199 Login with the same Google Account that you will use to upload your app or extension
200 to the Chrome Web Store.</li>
201 <li>After successful log in, you are redirected to a page to authorize the scopes.
202 Click "Allow access" button, redirecting you back to the OAuth 2.0 playground.</li>
203 <li>In "Step 2", click "Exchange authorization code for tokens" button.</li>
204 </ol>
207 The refresh token never expires until you explicitly revoke access.
208 You need to record and embed the refresh token in the app or extension server side.
209 </p>
211 <p class="caution">
212 <b>Be careful:</b>
213 The refresh token should not be shown to anyone outside your organization;
214 it should never be exposed on the client.
215 If anyone gets your refresh token,
216 they could potentially send messages as your server.
217 </p>
219 <h2 id="three">Set up app or extension</h2>
221 <h3 id="manifest">Add permission to manifest</h3>
224 To use the push messaging service,
225 you must declare the <code>pushMessaging</code>
226 permission in <code>manifest.json</code>:
227 </p>
229 <pre data-filename="manifest.json">
230 "permissions": [
231 "pushMessaging",
233 </pre>
235 <h3 id="channelid">Get channel ID</h3>
238 Similar to an email address,
239 the channel ID is used to identify and send messages
240 to a specific user of your app or extension.
241 Your app or extension needs to send this value
242 to its application server so that the server
243 can trigger push messages back.
244 To get the user's channel ID,
245 call $(ref:pushMessaging.getChannelId).
246 Use the callback function
247 to send the channel ID back to your app or extension.
248 </p>
250 <pre>
251 chrome.pushMessaging.getChannelId(boolean interactive, function ChannelIdCallback)
252 </pre>
255 When the <code>interactive</code> flag is set to true,
256 the user is asked to log in if they haven't already done so
257 with a warning dialog that looks something like this:
258 "You must log into Chrome for the Calendar extension to receive push messages.
259 Log in now?"
260 </p>
263 To provide your users with a better experience,
264 the interactive flag should be set to false the first time
265 your app or extension calls <code>getChannelId</code>.
266 Otherwise users will see the sign-in dialog
267 with no context,
268 even before they start your app or extension.
269 If the first call fails because the user is not logged in,
270 then <code>getChannelId</code> can be called again
271 with the flag set to true.
272 You should provide a context dialog
273 before the second call is made.
274 </p>
276 <h3 id="registerPush">Register message event handler</h3>
279 Whenever Chrome receives a pushed message for an application/extension,
280 it delivers the push message to the app or extension client.
281 Your app or extension must register a handler to receive the event
282 whenever the app or extension starts up,
283 similar to how they’d register for launch events.
284 This gets added to the <code>background.js</code>, for example:
285 </p>
287 <pre>
288 function setupPush() {
289 chrome.pushMessaging.onMessage.addListener(messageCallback);
291 </pre>
294 The app or extension need not be running when the message arrives;
295 the handler can be registered after the message arrives.
296 </p>
298 <h2 id="store">Publish your app</h2>
300 {{^is_apps}}
302 To use the push messaging service,
303 you must publish your extension in the
304 <a href="https://developers.google.com/chrome/web-store/docs/get_started_simple">Chrome Web Store</a>.
305 </p>
306 {{/is_apps}}
308 {{?is_apps}}
310 To use the push messaging service,
311 you must publish your app in the
312 <a href="https://developers.google.com/chrome/web-store/docs/get_started_simple">Chrome Web Store</a>.
313 </p>
314 {{/is_apps}}
316 <h2 id="five">Send messages</h2>
318 <h3 id="access">Get new access token</h3>
321 You need a valid access token to push messages
322 to your app or extension.
323 To obtain a new access token,
324 make an <code>HTTPS POST</code>
325 that includes your client ID and refresh token.
326 <a href="https://developers.google.com/accounts/docs/OAuth2WebServer">Using OAuth 2.0 for
327 Web Server Applications</a>
328 describes this in greater detail.
329 A sample request would like something like this:
330 </p>
332 <pre>
333 POST /o/oauth2/token HTTP/1.1
334 Host: accounts.google.com
335 Content-Type: application/x-www-form-urlencoded
337 client_id=291796959215.apps.googleusercontent.com&
338 client_secret=0bKUtXN6ykk7Mj1lQxoBZ2mh&
339 refresh_token=1%wMfyZvGcCxMSNEX4iTRdE0H1_Yt0wvImBz_iCuXF-UM&
340 grant_type=refresh_token
341 </pre>
344 A response from such a request is shown below:
345 </p>
347 <pre>
349 "access_token":"1/fFBGRNJru1FQd44AzqT3Zg",
350 "expires_in":3920,
351 "token_type":"Bearer"
353 </pre>
355 <p class="note">
356 <b>Reminder:</b>
357 You should cache the access token for use
358 until it expires.
359 There is a rate limit on how often you can ask for access tokens.
360 You may find yourself locked out of sending messages for awhile
361 if you get a new access token every time you send a push message.
362 </p>
364 <h3 id="message">Send message to user</h3>
367 Send a <code>POST</code> body that includes the channel ID and subchannel ID
368 along with the message payload to the API endpoint
369 <code>https://www.googleapis.com/gcm_for_chrome/v1/messages</code>.
370 Here's what a sample HTTP call would look like:
371 </p>
373 <pre>
374 POST /gcm_for_chrome/v1/messages
375 Host: www.googleapis.com
376 Content-Type: application/json
377 Authorization: Bearer 1/fFBGRNJru1FQd44AzqT3Zg
380 "channelId": "08144192009958038014/aaaaaaaaaabbbbbbbbbbcccccccccc",
381 "subchannelId": "0",
382 "payload": "Thanks for installing my app!"
384 </pre>
387 Messages can be coalesced.
388 If you send multiple messages on subchannel 1, for instance,
389 you may only see the last message and its payload.
390 Also, payloads can sometimes be dropped;
391 treat a payload as an optimization.
392 You can always go back to the server to check
393 for the contents of previous messages and
394 to get data if the payload is not present.
395 </p>
397 <p>Here's a simple example that shows a push message
398 as a text notification when it arrives:
399 </p>
401 <pre>
402 function showPushMessage(message) {
403 new Notification('New notification', {
404 body: message.payload + " [" + message.subchannelId + "]"
407 </pre>
410 You need to add the "notifications" permission
411 to <code>manifest.json</code>
412 to use text notifications
413 (see <a href="desktop_notifications">Desktop Notifications</a>):
414 </p>
416 <pre data-filename="manifest.json">
417 "permissions": [
418 "pushMessaging",
419 "notifications"
421 </pre>
423 <h2 id="six">Error reference</h2>
426 Push messaging error codes indicate whether the push request was accepted or rejected.
427 Rejection reasons include sender errors (for example, malformed message),
428 permission errors (for example, revoked push messaging token),
429 and operational errors (for example, push messaging service is currently down).
430 </p>
433 Here's a brief summary of the push messaging errors:
434 </p>
436 <ul>
437 <li>Channel ID is invalid.</li>
438 <li>Subchannel is invalid (four subchannels available;
439 subchannel value must be 0, 1, 2, or 3).</li>
440 <li>Payload is too long (must be 256 bytes or less).</li>
441 <li>Daily message quota exceeded (10,000 message requests allowed per day).</li>
442 <li>Google Account calling the push messaging service does not own the app or extension.</li>
443 <li>An internal error has occurred.
444 This indicates something went wrong on the Google server side
445 (for example, some backend not working
446 or errors in the HTTP post such as a missing access token).</li>
447 </ul>
449 <h2 id="test">Testing</h2>
451 <h3 id="test-local">Testing locally</h3>
454 To test push messaging locally:
455 </p>
457 <ol>
458 <li><a href="packaging">Package</a> a test version of
459 your app or extension on the Extensions management page
460 (chrome://extensions).
461 Your app or extension doesn't need to be running; it just needs
462 to be installed.</li>
464 <li>Get the channel ID at install time using
465 {{?is_apps}}$(ref:app.runtime.onLaunched).{{/is_apps}}
466 {{^is_apps}}$(ref:runtime.onInstalled).{{/is_apps}}</li>
468 <li>Use that channel ID on the server to send a test
469 push message through the system.
470 If all goes well,
471 your app or extension should start
472 and you should receive the test push message.
473 </li>
474 </ol>
476 <h3 id="test-cloud">Testing in the cloud</h3>
478 <p>To test push messaging in the cloud, you must first make sure that the
479 app or extension you are testing passes an ownership check.
480 The Push Messaging server checks that the ID of an app or extension
481 that calls the pushMessaging API matches the ID of the app or extension
482 in the Chrome Web Store. This ownership check is designed to prevent people
483 from sending messages to your app or extension without your permission.
484 If your app or extension attempts to use the pushMessaging API and
485 the ownership check fails, it will receive
486 HTTP status code 500 (Internal Server Error).
487 </p>
490 One circumstance in which the ownership check commonly fails is when you are
491 developing an app and you run the app without uploading it and re-downloading
492 it from the Chrome Web Store. In this situation your app may not have a
493 <a href="manifest/key">key</a> field in its manifest.json file.
494 The <code>key</code> field gives an app its Chrome Web Store ID
495 (a 32 character alphabetic code, such as "bafimiidcfafikaonocgmmcpbbhfjjik").
496 If you run a version of your app without a key, the app will use a
497 randomly generated ID that will not match the app's ID in the Chrome Web Store.
498 For example, if you upload your app to the Chrome Web Store from the directory
499 original_app_dir, then download the app and unpack it to downloaded_app_dir,
500 and then run the exact same app as an unpacked extension from original_app_dir,
501 the manifest.json file of the app in original_app_dir would not have
502 the downloaded key, and the app's ID would appear to be different than
503 the ID of the downloaded app.
504 </p>
507 To test push messaging in the cloud:
508 </p>
510 <ol>
511 <li>Publish your app or extension to the Chrome Web Store.</li>
513 <li>Determine the Chrome Web Store ID of your app or extension.
514 The Chrome Web Store ID is in the URL of any dashboard
515 or Chrome Web Store page that's dedicated to your app or extension.
516 For example, the URL
517 <code>https://chrome.google.com/extensions/detail/aaaaaaaaaabbbbbbbbbbcccccccccc?hl=en</code>
518 has the ID <code>aaaaaaaaaabbbbbbbbbbcccccccccc</code>.</li>
521 <li>Install your app or extension from the Chrome Web Store.</li>
523 <li>Get the key from the installed app or extension:
525 <ol style="list-style-type: lower-alpha;">
526 <li>Go to your
527 <a href="http://www.chromium.org/user-experience/user-data-directory">user data directory</a>.
528 </li>
530 <li>Look in the file <code>Default/Extensions/&lt;<i>ID</i>&gt;/&lt;<i>versionString</i>&gt;/manifest.json</code>.
531 </li>
533 <li>Copy the key field.</li>
534 </ol>
535 </li>
537 <li>Paste the key field into manifest.json in
538 the test version of your app or extension.</li>
540 <li>Install a test version of your app or extension on the
541 Extensions management page (chrome://extensions).</li>
542 </ol>
545 Each time you reload your app or extension for testing,
546 you need to check that the key is present in the manifest file.
547 And anytime you wish to update the published version in the Chrome Web Store,
548 you need to remove the key because the Store does not currently allow manifests
549 with keys.
550 </p>
552 <h2 id="feedback">Feedback</h2>
554 <p>You can provide feedback about Google Cloud Messaging
555 and the pushMessaging API through the Google Group
556 <a target="_blank" href="https://groups.google.com/forum/#!forum/gcm-for-chrome-feedback">GCM for Chrome feedback</a>.
557 Use this group to ask for help, file bug reports, and request features.</p>