OLD | NEW |
---|---|
(Empty) | |
1 <meta name="doc-family" content="apps"> | |
2 <h1 id="gcmForChrome">Google Cloud Messaging for Chrome</h1> | |
3 | |
4 <h2 id="t1">Overview</h2> | |
mkearney1
2014/02/04 21:23:45
This content doesn't need to be in an overview (h2
jianli
2014/02/05 22:04:56
Done.
| |
5 | |
6 <p>Google Cloud Messaging for Chrome (GCM) is a service for signed-in Chrome use rs | |
7 that helps developers send and receive message data from servers to their Chrome | |
8 apps and extensions. The service works even if an app or extension isn't | |
9 currently running. For example, calendar updates could be pushed to users even | |
10 when their calendar app isn't open. The service could also be used to send | |
11 upstream messages to the server.</p> | |
mkearney1
2014/02/04 21:23:45
Is there a working sample for new GCM? There defin
jianli
2014/02/05 22:04:56
We don't have sample yet. We are definitely going
| |
12 | |
mkearney1
2014/02/04 21:23:45
You don't mention anywhere that this version of GC
jianli
2014/02/05 22:04:56
We're going to ship GCM API to stable channel for
| |
13 <p>This document describes how to set up and use GCM. For additional information | |
14 see the reference documentation for the <a href="gcm.html">GCM Chrome API</a> an d | |
15 the <a href="http://developer.android.com/google/gcm/index.html">GCM Service</a> . To get | |
mkearney1
2014/02/04 21:23:45
Why are we pointing to android docs instead of gcm
jianli
2014/02/05 22:04:56
Our existing documentation for GCM service at http
| |
16 help with GCM or to give us feedback, see <a href="#feedback">Feedback</a>.</p> | |
mkearney1
2014/02/04 21:23:45
Why have we removed the Feedback section?
jianli
2014/02/05 22:04:56
This is due to conversion error. Fixed.
| |
17 | |
mkearney1
2014/02/04 21:23:45
Make headers consistent!
Suggestion-- h3 should b
jianli
2014/02/05 22:04:56
Done.
| |
18 <h3 id="t2">How to enable GCM</h3> | |
19 | |
20 <p>To use GCM for your app or extension, do the following:</p> | |
21 | |
22 <ol> | |
23 <li>Upload your app or extension client to the Chrome Web Store.</li> | |
mkearney1
2014/02/04 21:23:45
Link to webstore/publish.html.
jianli
2014/02/05 22:04:56
Done.
| |
24 <li>A user installs your app or extension.</li> | |
25 <li>Your app or extension client requests a registration ID using $ref:gcm.regis ter | |
mkearney1
2014/02/04 21:23:45
Link to related doc (obtaining registration ID) fo
jianli
2014/02/05 22:04:56
Done.
| |
26 function and sends this ID to your server together with information | |
27 identifying the user.</li> | |
28 </ol> | |
29 | |
30 <h3 id="t3">How to receive a message</h3> | |
mkearney1
2014/02/04 21:23:45
Make headers consistent!
Suggestion-- h3 should b
jianli
2014/02/05 22:04:56
If we get rid of <h2>Overview</h2> and promote "Re
| |
31 | |
32 <p>At a glance, receiving a message works like this:</p> | |
33 | |
34 <ol> | |
35 <li>Your app or extension client should register a handler to receive the | |
36 $ref:gcm.onMessage event.</li> | |
37 <li>When your server sends a message to the user, it specifies all of the | |
38 registration IDs that are related to that user and passes the message to GCM | |
39 service.</li> | |
40 <li>GCM servers route the message to all instances of Chrome running apps or | |
41 extensions with one of the registration IDs.</li> | |
42 <li>When the message arrives on the client, Chrome starts the app or extension, | |
43 if it is not already running, and calls the registered handler to process the | |
44 message.</li> | |
mkearney1
2014/02/04 21:23:45
Link to related doc (Receiving messages).
jianli
2014/02/05 22:04:56
Done.
| |
45 </ol> | |
46 | |
47 <h3 id="t4">How to send a message</h3> | |
mkearney1
2014/02/04 21:23:45
Make headers consistent!
Suggestion-- h3 should b
jianli
2014/02/05 22:04:56
Done.
| |
48 | |
49 <p>At a glance, sending a message works like this:</p> | |
50 | |
51 <ol> | |
52 <li>Your app or extension generates a unique message ID, so that it is possible | |
53 to find out which message failed to be queued or delivered.</li> | |
54 <li>Your app or extension calls $ref:gcm.send function with an object containing | |
55 message ID, destination ID that identifies the server, and data that consist | |
mkearney1
2014/02/04 21:23:45
Link to related doc (Sending messages).
jianli
2014/02/05 22:04:56
Done.
| |
56 of a list of key value pairs. In this step you can also provide an optional | |
57 time-to-live for the message.</li> | |
58 <li>The callback passed to $ref:gcm.send function will be called to notify the | |
59 result. Your app or extension should check $ref:runtime.lastError to find out if | |
60 the message has been successfully queued for delivery.</li> | |
61 <li>In cases that the queued message could not be delivered to the GCM server, | |
62 like network error, a $ref:gcm.onSendError event will be fired. Your app or | |
63 extension can listen to this event and react to it, e.g. by trying to resend | |
64 the message.</li> | |
mkearney1
2014/02/04 21:23:45
Link to Error reference section.
jianli
2014/02/05 22:04:56
Done.
| |
65 </ol> | |
66 | |
mkearney1
2014/02/04 21:23:45
Make headers consistent!
Slight rewording of titl
jianli
2014/02/05 22:04:56
Done.
| |
67 <h2 id="t5">Setting up Project</h2> | |
68 | |
69 <h3 id="t6">Creating a Google API project</h3> | |
mkearney1
2014/02/04 21:23:45
Make headers consistent!
Slight rewording of titl
jianli
2014/02/05 22:04:56
Done.
| |
70 | |
71 <ol> | |
72 <li>Login to the <a href="https://cloud.google.com/console/project">Google Devel opers | |
73 Console</a> using the same Google <br> | |
74 Account that you will use to upload your app or extension.</li> | |
75 <li>If you haven't created an API project yet, click <strong>Create Project</str ong>.</li> | |
76 <li>Supply a project name and click <strong>Create</strong>.</li> | |
77 <li>Once the project has been created, a page appears that displays your project | |
78 number. For example, <strong>Project Number: 670330094152</strong>.</li> | |
79 <li>Copy down your project number. You will use it later on as the GCM sender ID .</li> | |
80 </ol> | |
81 | |
82 <h3 id="t7">Enabling the GCM service</h3> | |
mkearney1
2014/02/04 21:23:45
Enable the GCM service
jianli
2014/02/05 22:04:56
Done.
| |
83 | |
84 <ol> | |
85 <li>In the sidebar on the left, select <strong>APIs & auth</strong>.</li> | |
86 <li>In the displayed list of APIs, turn the <strong>Google Cloud Messaging for C hrome | |
87 API</strong> toggle to ON.</li> | |
mkearney1
2014/02/04 21:23:45
Is it worth mentioning the terms of service here?
jianli
2014/02/05 22:04:56
I added the text to mention TOS at the beginning o
| |
88 </ol> | |
89 | |
90 <h2 id="t8">Setting up Chrome app or extension</h2> | |
mkearney1
2014/02/04 21:23:45
Set up Chrome App or Extension
jianli
2014/02/05 22:04:56
Done.
| |
91 | |
92 <h3 id="t9">Adding permission to manifest</h3> | |
93 | |
mkearney1
2014/02/04 21:23:45
Add permission to manifest
jianli
2014/02/05 22:04:56
Done.
| |
94 <p>To use the gcm service, you must declare the <code>gcm</code> permission in | |
95 <code>manifest.json</code>.</p> | |
96 | |
97 <pre data-filename="manifest.json"><code> | |
98 "permissions": [ | |
99 "gcm", | |
mkearney1
2014/02/04 21:23:45
The , looks funny here without the ...
jianli
2014/02/05 22:04:56
Done.
| |
100 ] | |
101 </code></pre> | |
102 | |
103 <h2 id="t10">Writing Chrome app or extension</h2> | |
mkearney1
2014/02/04 21:23:45
Write Chrome App or Extension
jianli
2014/02/05 22:04:56
Done.
| |
104 | |
105 <h3 id="t11">Obtaining registration ID</h3> | |
mkearney1
2014/02/04 21:23:45
Obtain registration ID
jianli
2014/02/05 22:04:56
Done.
| |
106 | |
107 <p>Your app or extension needs to register with GCM servers before it can receiv e | |
108 messages. When an app or extension registers, it receives a registration ID, | |
109 that is linked to your app or extension that is installed to this profile.</p> | |
110 | |
111 <p>When a user installs your app or extension, the client needs to call | |
112 $ref:gcm.register and specify the list of senders (project IDs from Google | |
113 Developers Console). Your app or extension should pass a callback function to | |
114 verify that registration was successful and send the registration ID back to | |
115 your application server. For example, your app or extension could send an XHR | |
116 request to a RESTful API on your server. Whatever method your app or extension | |
117 uses to send the registration ID to the server, it must be secured (https, for | |
118 instance).</p> | |
119 | |
120 <p>When registration fails, the app or extension should retry the failed operati on | |
121 using exponential back-off. In exponential back-off, each time there is a | |
122 failure, it should wait twice the previous amount of time before trying again.</ p> | |
123 | |
124 <pre><code> | |
125 chrome.runtime.onInstalled.addListener(function() { | |
126 // Up to 100 senders are allowed. | |
127 var senderIds = ["Your-Sender-ID"]; | |
128 | |
129 chrome.gcm.register(senderIds, function(registrationId) { | |
130 if (chrome.runtime.lastError) { | |
131 // When registration fails, try to register again using | |
132 // exponential back-off. | |
133 return; | |
134 } | |
135 | |
136 // Send the registration ID to your application server. | |
137 | |
138 // You can cache the registration ID here, but it is not | |
139 // necessary, as GCM API will do it for you. You can get | |
140 // back the same ID by calling gcm.register again with | |
141 // same sender IDs. | |
142 }); | |
143 }); | |
144 </code></pre> | |
145 | |
mkearney1
2014/02/04 21:23:45
Weird couple of sentences here. How about:
Your a
jianli
2014/02/05 22:04:56
Done.
| |
146 <p>Unregistration is not supported in gcm API. Your app or extension will be | |
147 automatically unregistered from the GCM server when it is uninstalled.</p> | |
148 | |
mkearney1
2014/02/04 21:23:45
Receive messages
jianli
2014/02/05 22:04:56
Done.
| |
149 <h3 id="t12">Receiving messages</h3> | |
150 | |
151 <p>When your server sends a message to the user, it specifies all of the | |
152 registration IDs that are related to that user and passes the message to GCM | |
153 service. GCM servers route the message to all instances of Chrome running apps | |
154 or extensions with one of the registration IDs. If your app or extension has | |
155 been installed in more than one profiles in a single Chrome instance, all of | |
156 them can receive messages independently based on their unique registration IDs.< /p> | |
157 | |
158 <p>Messages from the server are delivered via $ref:gcm.onMessage event. Your app or | |
mkearney1
2014/02/04 21:23:45
Nit-- via the instead of just via.
jianli
2014/02/05 22:04:56
Done.
| |
159 extension must register a handler to receive this event when loading, similar to | |
160 how they'd register for launch events.</p> | |
161 | |
162 <pre><code> | |
163 chrome.app.runtime.onLaunched.addListener(function() { | |
164 chrome.gcm.onMessage.addListener(function(message) { | |
165 // A message is an object with a data property that | |
166 // consists of key-value pairs. | |
mkearney1
2014/02/04 21:23:45
Code isn't valid. Check your blocks.
jianli
2014/02/05 22:04:56
Done.
| |
167 }); | |
168 </code></pre> | |
169 | |
170 <blockquote> | |
171 <p>});</p> | |
mkearney1
2014/02/04 21:23:45
I think you need to remove this blockquote.
jianli
2014/02/05 22:04:56
Done.
| |
172 </blockquote> | |
173 | |
174 <p>As long as Chrome is running, even if the extension or app is not running, it is | |
175 woken up to deliver a message.</p> | |
176 | |
177 <h3 id="t13">Sending messages</h3> | |
mkearney1
2014/02/04 21:23:45
Send messages
jianli
2014/02/05 22:04:56
Done.
| |
178 | |
179 <p>To send messages upstream, your app or extension should call $ref:gcm.send wi th an | |
180 object containing:</p> | |
181 | |
mkearney1
2014/02/04 21:23:45
IMPORTANT-- the terminology in this bullet list do
jianli
2014/02/05 22:04:56
Which part does not match?
| |
182 <ul> | |
mkearney1
2014/02/04 21:23:45
You should capitalize the first letters in each bu
jianli
2014/02/05 22:04:56
Done.
| |
183 <li>a unique message ID that identifies the message when it fails to be queued o r | |
184 delivered. The message ID should be unique across the lifetime of your app or | |
185 extension, even after it restarts. If an auto-increment counter is used to | |
186 create the message ID, your app or extension should persist the counter value | |
187 via chrome.storage API and restores it when the app reloads.</li> | |
mkearney1
2014/02/04 21:23:45
Nit-- 'restores it' should be 'restore it'.
jianli
2014/02/05 22:04:56
Done.
| |
188 <li>destination ID that identifies the server. This is the project number return ed | |
189 from the Google Developers Console plus the suffix "@gcm.googleapis.com".</li> | |
190 <li>data that consist of a list of string-to-string key value pairs (up to 4KB | |
191 total)</li> | |
mkearney1
2014/02/04 21:23:45
Nit-- Line 91 list item missing a full-stop.
jianli
2014/02/05 22:04:56
Not sure I understand this. What do you mean by fu
fgorski
2014/02/05 23:27:11
It's a period ('.') at the end of the sentence.
| |
192 <li>optional time-to-live (TTL). This property value must be a duration from 0 t o | |
193 2,419,200 seconds (4 weeks) and it corresponds to the maximum period of time | |
194 for which GCM will store and try to deliver the message. If this property is | |
195 not set, it is default to the maximum value. When a TTL is set to 0, GCM will | |
196 try to deliver the message immediately. If the immediate effort fails, the | |
197 message will be discarded.</li> | |
198 </ul> | |
199 | |
200 <p>When the callback passed in $ref:gcm.send is called without runtime error, it does | |
201 not mean that the message was already delivered to the GCM server. Rather, it | |
202 means that it was queued for delivery. If the message fails to reach the GCM | |
203 server, for example due to network error, the $ref:gcm.onSendError will be fired .</p> | |
204 | |
205 <pre><code> | |
206 // Substitute your own sender ID here. This is the project | |
207 // number you got from the Google Developers Console. | |
208 var senderId = "Your-Sender-ID"; | |
209 | |
210 // Message ID should be unique across the lifetime of your app. | |
211 // One way to achieve this is to use the auto-increment counter | |
212 // that is persisted to local storage. | |
213 | |
214 // Message ID is saved to and restored from local storage. | |
215 var messageId = 0; | |
216 chrome.app.runtime.onLaunched.addListener(function() { | |
217 chrome.storage.local.get("messageId", function(result) { | |
218 messageId = result["messageId"]; | |
219 }); | |
220 }); | |
221 | |
222 // Returns a new ID to identify the message. | |
223 function getMessageId() { | |
224 messageId++; | |
225 chrome.storage.local.set({messageId: messageId}); | |
226 return messageId; | |
227 } | |
228 | |
229 chrome.gcm.onSendError.addListener(function(error) { | |
230 console.log("Message " + error.messageId + | |
231 " failed to be sent: " + error.errorMessage); | |
232 } | |
233 | |
234 function sendMessage() { | |
235 var message = { | |
236 messageId: getMessageId(), | |
237 destinationId: senderId + "@gcm.googleapis.com", | |
238 timeToLive: 86400, // 1 day | |
239 data: { | |
240 "key1": "value1", | |
241 "key2": "value2" | |
242 } | |
243 }; | |
244 chrome.gcm.send(message, function(messageId) { | |
245 if (chrome.runtime.lastError) { | |
246 // Some error occurred. Fail gracefully or try to send | |
247 // again. | |
248 return; | |
249 } | |
250 | |
251 // Message is accepted and queued. | |
252 }); | |
253 } | |
254 </code></pre> | |
255 | |
256 <h3 id="t14">Maximum number of messages stored in GCM</h3> | |
257 | |
258 <p>GCM will store up to 100 messages. After that, all messages are discarded fro m | |
259 GCM, and an event $ref:gcm.onMessagesDeleted will be fired, which tells the clie nt | |
260 that it falls behind. Your app or extension should respond by syncing with your | |
261 app server to recover the discarded messages.</p> | |
262 | |
263 <h2 id="t15">Publishing your app</h2> | |
mkearney1
2014/02/04 21:23:45
Publish your app
jianli
2014/02/05 22:04:56
Done.
| |
264 | |
265 <p>To use the GCM service, you must publish your app in the <a href="https://dev elopers.google.com/chrome/web-store/docs/get_started_simple">Chrome Web | |
266 Store</a>.</p> | |
mkearney1
2014/02/04 21:23:45
Link instead to webstore/publish.html
jianli
2014/02/05 22:04:56
Done.
| |
267 | |
268 <h2 id="t16">Error reference</h2> | |
269 | |
270 <p>An error could occur when a gcm API function is called. Your app or extension | |
271 should check $ref:runtime.lastError for more information in your callback. The | |
272 error code will also be passed as a parameter to $ref:gcm.onSendError event.</p> | |
273 | |
274 <p>Here's a brief summary of the gcm errors:</p> | |
275 | |
276 <ul> | |
277 <li><strong>Function was called with invalid parameters</strong>: this could hap pen when gcm | |
278 functions are called with bad parameters.</li> | |
279 <li><strong>Profile was not signed in</strong>: this could happen when gcm funct ions are called | |
280 from a profile that was not signed in.</li> | |
281 <li><strong>Asynchronous operation is pending</strong>: this could happen when c ertain gcm | |
282 function is called again without waiting for the callback passed in previous | |
283 function to be called.</li> | |
284 <li><strong>Network error occurred</strong>: this could happen when GCM server f ails to reach | |
285 due to network problem, like losing Internet connection.</li> | |
286 <li><strong>Server error occurred</strong>: this could happen when GCM server fa ils to reach | |
287 due to server problem, like server busy.</li> | |
288 <li><strong>Time-to-live exceeded</strong>: this could happen when a message cou ld not be | |
289 delivered within the specific time-to-live period.</li> | |
290 <li><strong>Unknown error occurred</strong>: this could happen due to any other internal | |
291 errors.</li> | |
292 </ul> | |
293 | |
294 <h2 id="feedback"></h2> | |
mkearney1
2014/02/04 21:23:45
Missing text! Feedback
jianli
2014/02/05 22:04:56
Done.
| |
295 | |
296 <p>You can provide feedback about Google Cloud Messaging and the gcm API through | |
297 the Google Group <a href="https://groups.google.com/forum/#!forum/gcm-for-chrome -v2-feedback">GCM for Chrome V2 | |
mkearney1
2014/02/04 21:23:45
There isn't a group set up for this yet!
jianli
2014/02/05 22:04:56
Will create one once before we official launch the
| |
298 Feedback</a>. | |
299 Use this group to ask for help, file bug reports, and request features.</p> | |
OLD | NEW |