1 page.title=GCM Demo Application 2 @jd:body 3 4 <div id="qv-wrapper"> 5 <div id="qv"> 6 7 <h2>Quickview</h2> 8 9 <ul> 10 <li>Build and run the GCM demo app.</li> 11 <li>Understand how to set up both the client and server sides of a GCM app.</li> 12 <li>Become familiar with the GCM helper libraries.</li> 13 </ul> 14 15 16 <h2>In this document</h2> 17 18 <ol> 19 <li><a href="#requirements">Requirements</a> </li> 20 <li><a href="#gcm-setup">Setting Up GCM</a></li> 21 <li><a href="#server-setup">Setting Up the Server</a> 22 <ol> 23 <li><a href="#webserver-setup">Using a standard web server</a></li> 24 <li><a href="#appengine-setup">Using App Engine for Java</a></li> 25 </ol> 26 </li> 27 <li><a href="#device-setup">Setting Up the Device</a></li> 28 </ol> 29 30 </div> 31 </div> 32 33 <p>The Google Cloud Messaging (GCM) Demo demonstrates how to use the Google Cloud Messaging framework in your Android application. This tutorial walks you through setting up and running the demo.</p> 34 35 36 <p>This demo consists of the following pieces: </p> 37 <ul> 38 <li>A web server containing a page where you can send messages.</li> 39 <li>An Android application that receives and displays such messages.</li> 40 </ul> 41 <p>Here is the API reference documentation for the helper libraries on which the demo is based:</p> 42 <ul> 43 <li><a href="{@docRoot}guide/google/gcm/client-javadoc/index.html">Client Reference</a></li> 44 <li><a href="{@docRoot}guide/google/gcm/server-javadoc/index.html">Server Reference</a></li> 45 </ul> 46 47 <p>The sections below describe how to download the demo code and helper libraries from the SDK Manager. The demo code and helper libraries are also available at the <a href="http://code.google.com/p/gcm">open source site</a>. 48 49 <h2 id="requirements">Requirements</h2> 50 <p>For the web server:</p> 51 <ul> 52 <li> <a href="http://ant.apache.org/">Ant 1.8</a> (it might work with earlier versions, but it's not guaranteed).</li> 53 <li>One of the following: 54 <ul> 55 <li>A running web server compatible with Servlets API version 2.5, such as <a href="http://tomcat.apache.org/">Tomcat 6</a> or <a href="http://jetty.codehaus.org/">Jetty</a>, or</li> 56 <li><a href="http://code.google.com/appengine/">Java App Engine SDK</a>version 1.6 or later.</li> 57 </ul> 58 </li> 59 <li>A Google account registered to use GCM.</li> 60 <li>The API key for that account.</li> 61 </ul> 62 <p>For the Android application:</p> 63 <ul> 64 <li>Emulator (or device) running Android 2.2 with Google APIs.</li> 65 <li>The Google API project ID of the account registered to use GCM.</li> 66 </ul> 67 <h2 id="gcm-setup">Setting Up GCM</h2> 68 <p>Before proceeding with the server and client setup, it's necessary to register a Google account with the Google API Console, enable Google Cloud Messaging in GCM, and obtain an API key from the <a href="https://code.google.com/apis/console">Google API Console</a>.</p> 69 <p>For instructions on how to set up GCM, see <a href="gs.html">Getting Started</a>.</p> 70 71 72 <h2 id="server-setup">Setting Up the Server</h2> 73 <p>This section describes the different options for setting up a server.</p> 74 <h3 id="webserver-setup">Using a standard web server</h3> 75 <p>To set up the server using a standard, servlet-compliant web server:</p> 76 <ol> 77 <li> From the SDK Manager, install <strong>Extras > Google Cloud Messaging for Android Library</strong>. 78 79 80 <p>This creates a <code>gcm</code> directory under <code><em>YOUR_SDK_ROOT</em>/extras/google/</code> containing these subdirectories: <code>gcm-client</code>, <code>gcm-server</code>, <code>samples/gcm-demo-client</code>, <code>samples/gcm-demo-server</code>, and <code>samples/gcm-demo-appengine</code>.</p> 81 82 <p class="note"><strong>Note:</strong> If you don't see <strong>Extras > Google Cloud Messaging for Android Library</strong> in the SDK Manager, make sure you are running version 20 or higher. Be sure to restart the SDK Manager after updating it.</p> 83 </li> 84 85 <li>In a text editor, edit the <code>samples/gcm-demo-server/WebContent/WEB-INF/classes/api.key</code> and replace the existing text with the API key obtained above.</li> 86 <li>In a shell window, go to the <code>samples/gcm-demo-server</code> directory.</li> 87 <li>Generate the server's WAR file by running <code>ant war</code>:</li> 88 89 <pre class="prettyprint">$ ant war 90 91 Buildfile:build.xml 92 93 init: 94 [mkdir] Created dir: build/classes 95 [mkdir] Created dir: dist 96 97 compile: 98 [javac] Compiling 6 source files to build/classes 99 100 war: 101 [war] Building war: <strong>dist/gcm-demo.war</strong> 102 103 BUILD SUCCESSFUL 104 Total time: 0 seconds 105 </pre> 106 107 <li>Deploy the <code>dist/gcm-demo.war</code> to your running server. For instance, if you're using Jetty, copy <code>gcm-demo.war</code> to the <code>webapps</code> directory of the Jetty installation.</li> 108 <li>Open the server's main page in a browser. The URL depends on the server you're using and your machine's IP address, but it will be something like <code>http://192.168.1.10:8080/gcm-demo/home</code>, where <code>gcm-demo</code> is the application context and <code>/home</code> is the path of the main servlet. 109 110 </li> 111 </ol> 112 <p class="note"><strong>Note:</strong> You can get the IP by running<code>ifconfig</code>on Linux or MacOS, or<code>ipconfig</code>on Windows. </p> 113 <p><img src="{@docRoot}images/gcm/gcm-demo-homepage.png" class="screenshot" /></p> 114 <p> You server is now ready.</p> 115 <h3 id="appengine-setup">Using App Engine for Java</h3> 116 117 <p>To set up the server using a standard App Engine for Java:</p> 118 <ol> 119 <li> From the SDK Manager, install <strong>Extras > Google Cloud Messaging for Android Library</strong>. 120 <p>This creates a <code>gcm</code> directory under <code><em>YOUR_SDK_ROOT</em>/extras/google/</code> containing these subdirectories: <code>gcm-client</code>, <code>gcm-server</code>, <code>samples/gcm-demo-client</code>, <code>samples/gcm-demo-server</code>, and <code>samples/gcm-demo-appengine</code>.</p> 121 </li> 122 <li>In a text editor, edit <code>samples/gcm-demo-appengine/src/com/google/android/gcm/demo/server/ApiKeyInitializer.java</code> and replace the existing text with the API key obtained above. 123 <p class="note"><strong>Note:</strong> The API key value set in that class will be used just once to create a persistent entity on App Engine. If you deploy the application, you can use App Engine's <code>Datastore Viewer</code> to change it later.</p> 124 125 </li> 126 <li>In a shell window, go to the <code>samples/gcm-demo-appengine</code> directory.</li> 127 <li>Start the development App Engine server by <code>ant runserver</code>, using the <code>-Dsdk.dir</code> to indicate the location of the App Engine SDK and <code>-Dserver.host</code> to set your server's hostname or IP address:</li> 128 129 <pre class="prettyprint"> 130 $ ant -Dsdk.dir=/opt/google/appengine-java-sdk runserver -Dserver.host=192.168.1.10 131 Buildfile: gcm-demo-appengine/build.xml 132 133 init: 134 [mkdir] Created dir: gcm-demo-appengine/dist 135 136 copyjars: 137 138 compile: 139 140 datanucleusenhance: 141 [enhance] DataNucleus Enhancer (version 1.1.4) : Enhancement of classes 142 [enhance] DataNucleus Enhancer completed with success for 0 classes. Timings : input=28 ms, enhance=0 ms, total=28 ms. Consult the log for full details 143 [enhance] DataNucleus Enhancer completed and no classes were enhanced. Consult the log for full details 144 145 runserver: 146 [java] Jun 15, 2012 8:46:06 PM com.google.apphosting.utils.jetty.JettyLogger info 147 [java] INFO: Logging to JettyLogger(null) via com.google.apphosting.utils.jetty.JettyLogger 148 [java] Jun 15, 2012 8:46:06 PM com.google.apphosting.utils.config.AppEngineWebXmlReader readAppEngineWebXml 149 [java] INFO: Successfully processed gcm-demo-appengine/WebContent/WEB-INF/appengine-web.xml 150 [java] Jun 15, 2012 8:46:06 PM com.google.apphosting.utils.config.AbstractConfigXmlReader readConfigXml 151 [java] INFO: Successfully processed gcm-demo-appengine/WebContent/WEB-INF/web.xml 152 [java] Jun 15, 2012 8:46:09 PM com.google.android.gcm.demo.server.ApiKeyInitializer contextInitialized 153 [java] SEVERE: Created fake key. Please go to App Engine admin console, change its value to your API Key (the entity type is 'Settings' and its field to be changed is 'ApiKey'), then restart the server! 154 [java] Jun 15, 2012 8:46:09 PM com.google.appengine.tools.development.DevAppServerImpl start 155 [java] INFO: The server is running at http://192.168.1.10:8080/ 156 [java] Jun 15, 2012 8:46:09 PM com.google.appengine.tools.development.DevAppServerImpl start 157 [java] INFO: The admin console is running at http://192.168.1.10:8080/_ah/admin 158 </pre> 159 160 <li>Open the server's main page in a browser. The URL depends on the server you're using and your machine's IP address, but it will be something like <code>http://192.168.1.10:8080/home</code>, where <code>/home</code> is the path of the main servlet.</li> 161 162 <p class="note"><strong>Note:</strong> You can get the IP by running <code>ifconfig</code> on Linux or MacOS, or <code>ipconfig</code> on Windows.</p> 163 164 <p><img src="{@docRoot}images/gcm/gcm-demo-homepage-appengine.png" class="screenshot" /></p> 165 </ol> 166 <p> You server is now ready.</p> 167 <h2 id="device-setup">Setting Up the Device</h2> 168 <p>To set up the device:</p> 169 <ol> 170 <li> From the SDK Manager, install <strong>Extras > Google Cloud Messaging for Android Library</strong>. 171 <p>This creates a <code>gcm</code> directory under <code><em>YOUR_SDK_ROOT</em>/extras/google</code> containing these subdirectories: <code>gcm-client</code>, <code>gcm-server</code>, <code>samples/gcm-demo-client</code>, <code>samples/gcm-demo-server</code>, and <code>samples/gcm-demo-appengine</code>.</p> 172 </li> 173 <li>Using a text editor, open <code>samples/gcm-demo-client/src/com/google/android/gcm/demo/app/CommonUtilities.java</code> and set the proper values for the <code>SENDER_ID</code> and <code>SERVER_URL</code> constants. For example:</li> 174 175 <pre class="prettyprint pretty-java"> 176 static final String SERVER_URL = "http://192.168.1.10:8080/gcm-demo"; 177 static final String SENDER_ID = "4815162342";</pre> 178 <p>Note that the <code>SERVER_URL</code> is the URL for the server and the application's context (or just server, if you are using App Engine), and it does not include the forward slash (<code>/</code>). Also note that <code>SENDER_ID</code> is the Google API project ID you obtained in the server setup steps above.</p> 179 180 <li>In a shell window, go to the <code>gcm-demo-client</code> directory.</li> 181 <li>Use the SDK's <code>android</code> tool to generate the <code>ant</code> build files:</li> 182 183 <pre class="prettyprint"> 184 $ android update project --name GCMDemo -p . --target android-16 185 Updated project.properties 186 Updated local.properties 187 Updated file ./build.xml 188 Updated file ./proguard-project.txt 189 </pre> 190 <p>If this command fails becase <code>android-16</code> is not recognized, try a different target (as long as it is at least <code>android-15</code>).</p> 191 192 <li>Use <code>ant</code> to build the application's APK file:</li> 193 194 <pre class="prettyprint"> 195 $ ant clean debug 196 Buildfile: build.xml 197 198 ... 199 200 201 -do-debug: 202 [zipalign] Running zip align on final apk... 203 [echo] Debug Package: bin/GCMDemo-debug.apk 204 [propertyfile] Creating new property file: <strong>bin/build.prop</strong> 205 [propertyfile] Updating property file: bin/build.prop 206 [propertyfile] Updating property file: bin/build.prop 207 [propertyfile] Updating property file: bin/build.prop 208 209 -post-build: 210 211 debug: 212 213 BUILD SUCCESSFUL 214 Total time: 3 seconds 215 </pre> 216 217 <li>Start the Android emulator:</li> 218 <pre class="prettyprint">$emulator -avd my_avd 219 </pre> 220 221 <p> This example assumes there is an AVD (Android Virtual Device) named <code>my_avd</code> previously configured with Android 2.2 and Google APIs level 8. For more information on how to run an Android emulator, see <a href="{@docRoot}tools/devices/index.html">Managing Virtual Devices</a> in the Android Developers Guide.</p> 222 223 <li>Make sure there is a Google account added to the emulator. It doesn't have to be any account (like the <code>senderId</code>) in particular. </li> 224 225 <p> If the emulator is running Android 4.0.4 or later, this step is optional as GCM does not require an account from this version on.</p> 226 227 <li>Install the application in the emulator:</li> 228 229 <pre class="prettyprint"> 230 $ ant installd 231 Buildfile: gcm-demo-client/build.xml 232 233 -set-mode-check: 234 235 -set-debug-files: 236 237 install: 238 [echo] Installing gcm-demo-client/bin/GCMDemo-debug.apk onto default emulator or device... 239 [exec] 1719 KB/s (47158 bytes in 0.026s) 240 [exec] pkg: /data/local/tmp/GCMDemo-debug.apk 241 [exec] Success 242 243 installd: 244 245 BUILD SUCCESSFUL 246 Total time: 3 seconds 247 </pre> 248 <li>In the emulator, launch the GCM Demo app. The initial screen should look like this:</li> 249 <p><img src="{@docRoot}images/gcm/gcm-avd-home-auto-reg.png" class="screenshot" /></p> 250 <p class="note"><strong>Note:</strong> What happened? When the device received a registration callback intent from GCM, it contacted the server to register itself, using the register servlet and passing the registration ID received from GCM; the server then saved the registration ID to use it to send messages to the phone.</p> 251 <li> Now go back to your browser and refresh the page. It will show that there is one device registered:</li> 252 253 <p><img src="{@docRoot}images/gcm/gcm-device-reg.png" class="screenshot" /></p> 254 255 <li>Click on <strong>Send Message</strong>. The browser should show:</li> 256 <p><img src="{@docRoot}images/gcm/gcm-sent-server.png" class="screenshot" /></p> 257 258 <p>And in your emulator:</p> 259 260 <p><img src="{@docRoot}images/gcm/gcm-avd-first-msg.png" class="screenshot" /></p> 261 262 <p class="note"><strong>Note:</strong> What happened? When you clicked the button, the web server sent a message to GCM addressed to your device (more specifically, to the registration ID returned by GCM during the registration step). The device then received the message and displayed in the main activity; it also issued a system notification so the user would be notified even if the demo application was not running.</p> 263 </ol> 264 265