1page.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>See the <a href="{@docRoot}reference/com/google/android/gcm/package-summary.html">reference</a> for the client and server helper libraries used in this demo.</p> 42 43<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>. 44 45<h2 id="requirements">Requirements</h2> 46<p>For the web server:</p> 47<ul> 48 <li> <a href="http://ant.apache.org/">Ant 1.8</a> (it might work with earlier versions, but it's not guaranteed).</li> 49 <li>One of the following: 50 <ul> 51 <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> 52 <li><a href="http://code.google.com/appengine/">Java App Engine SDK</a> version 1.6 or later.</li> 53 </ul> 54 </li> 55 <li>A Google account registered to use GCM.</li> 56 <li>The API key for that account.</li> 57</ul> 58<p>For the Android application:</p> 59<ul> 60 <li>Emulator (or device) running Android 2.2 with Google APIs.</li> 61 <li>The Google API project number of the account registered to use GCM.</li> 62</ul> 63<h2 id="gcm-setup">Setting Up GCM</h2> 64<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> 65<p>For instructions on how to set up GCM, see <a href="gs.html">Getting Started</a>.</p> 66 67 68<h2 id="server-setup">Setting Up the Server</h2> 69<p>This section describes the different options for setting up a server.</p> 70<h3 id="webserver-setup">Using a standard web server</h3> 71<p>To set up the server using a standard, servlet-compliant web server:</p> 72<ol> 73 <li> From the SDK Manager, install <strong>Extras > Google Cloud Messaging for Android Library</strong>. 74 75 76 <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> 77 78<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> 79 </li> 80 81 <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> 82 <li>In a shell window, go to the <code>samples/gcm-demo-server</code> directory.</li> 83 <li>Generate the server's WAR file by running <code>ant war</code>:</li> 84 85 <pre class="prettyprint">$ ant war 86 87Buildfile:build.xml 88 89init: 90 [mkdir] Created dir: build/classes 91 [mkdir] Created dir: dist 92 93compile: 94 [javac] Compiling 6 source files to build/classes 95 96war: 97 [war] Building war: <strong>dist/gcm-demo.war</strong> 98 99BUILD SUCCESSFUL 100Total time: 0 seconds 101</pre> 102 103 <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> 104 <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. 105 106 </li> 107</ol> 108<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> 109<p><img src="{@docRoot}images/gcm/gcm-demo-homepage.png" class="screenshot" /></p> 110<p> You server is now ready.</p> 111<h3 id="appengine-setup">Using App Engine for Java</h3> 112 113<p>To set up the server using a standard App Engine for Java:</p> 114<ol> 115 <li> From the SDK Manager, install <strong>Extras > Google Cloud Messaging for Android Library</strong>. 116 <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> 117 </li> 118 <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. 119 <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> 120 121 </li> 122 <li>In a shell window, go to the <code>samples/gcm-demo-appengine</code> directory.</li> 123 <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> 124 125<pre class="prettyprint"> 126$ ant -Dsdk.dir=/opt/google/appengine-java-sdk runserver -Dserver.host=192.168.1.10 127Buildfile: gcm-demo-appengine/build.xml 128 129init: 130 [mkdir] Created dir: gcm-demo-appengine/dist 131 132copyjars: 133 134compile: 135 136datanucleusenhance: 137 [enhance] DataNucleus Enhancer (version 1.1.4) : Enhancement of classes 138 [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 139 [enhance] DataNucleus Enhancer completed and no classes were enhanced. Consult the log for full details 140 141runserver: 142 [java] Jun 15, 2012 8:46:06 PM com.google.apphosting.utils.jetty.JettyLogger info 143 [java] INFO: Logging to JettyLogger(null) via com.google.apphosting.utils.jetty.JettyLogger 144 [java] Jun 15, 2012 8:46:06 PM com.google.apphosting.utils.config.AppEngineWebXmlReader readAppEngineWebXml 145 [java] INFO: Successfully processed gcm-demo-appengine/WebContent/WEB-INF/appengine-web.xml 146 [java] Jun 15, 2012 8:46:06 PM com.google.apphosting.utils.config.AbstractConfigXmlReader readConfigXml 147 [java] INFO: Successfully processed gcm-demo-appengine/WebContent/WEB-INF/web.xml 148 [java] Jun 15, 2012 8:46:09 PM com.google.android.gcm.demo.server.ApiKeyInitializer contextInitialized 149 [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! 150 [java] Jun 15, 2012 8:46:09 PM com.google.appengine.tools.development.DevAppServerImpl start 151 [java] INFO: The server is running at http://192.168.1.10:8080/ 152 [java] Jun 15, 2012 8:46:09 PM com.google.appengine.tools.development.DevAppServerImpl start 153 [java] INFO: The admin console is running at http://192.168.1.10:8080/_ah/admin 154</pre> 155 156 <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> 157 158 <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> 159 160 <p><img src="{@docRoot}images/gcm/gcm-demo-homepage-appengine.png" class="screenshot" /></p> 161</ol> 162<p> You server is now ready.</p> 163<h2 id="device-setup">Setting Up the Device</h2> 164<p>To set up the device:</p> 165<ol> 166 <li> From the SDK Manager, install <strong>Extras > Google Cloud Messaging for Android Library</strong>. 167 <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> 168 </li> 169 <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> 170 171<pre class="prettyprint pretty-java"> 172static final String SERVER_URL = "http://192.168.1.10:8080/gcm-demo"; 173static final String SENDER_ID = "4815162342";</pre> 174<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 number you obtained in the server setup steps above.</p> 175 176 <li>In a shell window, go to the <code>gcm-demo-client</code> directory.</li> 177 <li>Use the SDK's <code>android</code> tool to generate the <code>ant</code> build files:</li> 178 179<pre class="prettyprint"> 180$ android update project --name GCMDemo -p . --target android-16 181Updated project.properties 182Updated local.properties 183Updated file ./build.xml 184Updated file ./proguard-project.txt 185</pre> 186<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> 187 188<li>Use <code>ant</code> to build the application's APK file:</li> 189 190 <pre class="prettyprint"> 191$ ant clean debug 192Buildfile: build.xml 193 194... 195 196 197-do-debug: 198[zipalign] Running zip align on final apk... 199 [echo] Debug Package: bin/GCMDemo-debug.apk 200[propertyfile] Creating new property file: <strong>bin/build.prop</strong> 201[propertyfile] Updating property file: bin/build.prop 202[propertyfile] Updating property file: bin/build.prop 203[propertyfile] Updating property file: bin/build.prop 204 205-post-build: 206 207debug: 208 209BUILD SUCCESSFUL 210Total time: 3 seconds 211 </pre> 212 213<li>Start the Android emulator:</li> 214<pre class="prettyprint">$emulator -avd my_avd 215</pre> 216 217<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> 218 219<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> 220 221<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> 222 223<li>Install the application in the emulator:</li> 224 225<pre class="prettyprint"> 226$ ant installd 227Buildfile: gcm-demo-client/build.xml 228 229-set-mode-check: 230 231-set-debug-files: 232 233install: 234 [echo] Installing gcm-demo-client/bin/GCMDemo-debug.apk onto default emulator or device... 235 [exec] 1719 KB/s (47158 bytes in 0.026s) 236 [exec] pkg: /data/local/tmp/GCMDemo-debug.apk 237 [exec] Success 238 239installd: 240 241BUILD SUCCESSFUL 242Total time: 3 seconds 243</pre> 244 <li>In the emulator, launch the GCM Demo app. The initial screen should look like this:</li> 245 <p><img src="{@docRoot}images/gcm/gcm-avd-home-auto-reg.png" class="screenshot" /></p> 246<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> 247<li> Now go back to your browser and refresh the page. It will show that there is one device registered:</li> 248 249<p><img src="{@docRoot}images/gcm/gcm-device-reg.png" class="screenshot" /></p> 250 251<li>Click on <strong>Send Message</strong>. The browser should show:</li> 252<p><img src="{@docRoot}images/gcm/gcm-sent-server.png" class="screenshot" /></p> 253 254<p>And in your emulator:</p> 255 256<p><img src="{@docRoot}images/gcm/gcm-avd-first-msg.png" class="screenshot" /></p> 257 258<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> 259</ol> 260 261