Home | History | Annotate | Download | only in gcm
      1 page.title=GCM Server
      2 @jd:body
      3 
      4 <div id="qv-wrapper">
      5 <div id="qv">
      6 
      7 <h2>Quickview</h2>
      8 
      9 <ul>
     10 <li>Understand how to set up the server side of a GCM app.</li>
     11 <li>Become familiar with the <a href="{@docRoot}reference/com/google/android/gcm/server/package-summary.html">GCM server helper library</a>.</li>
     12 </ul>
     13 
     14 
     15 <h2>In this document</h2>
     16 
     17 <ol>
     18   <li><a href="#requirements">Requirements</a> </li>
     19   <li><a href="#gcm-setup">Setting Up GCM</a></li>
     20   <li><a href="#server-setup">Setting Up an HTTP Server</a>
     21     <ol>
     22       <li><a href="#webserver-setup">Using a standard web server</a></li>
     23       <li><a href="#appengine-setup">Using App Engine for Java</a></li>
     24     </ol>
     25   </li>
     26 </ol>
     27 
     28 <h2>See Also</h2>
     29 
     30 <ol class="toc">
     31 <li><a href="gs.html">Getting Started</a></li>
     32 <li><a href="client.html">GCM Client</a></li>
     33 <li><a href="ccs.html">Cloud Connection Server</a></li>
     34 
     35 
     36 </ol>
     37 
     38 </div>
     39 </div>
     40 
     41 
     42 
     43 
     44 <p>This document gives examples of GCM server-side code for HTTP. For an example of an XMPP server (<a href="ccs.html">Cloud Connection Server</a>), see <a href="gs.html#server">Getting Started</a>. Note that a full GCM implementation requires a client-side implementation, in addition to the server. For a complete working example that includes client and server-side code, see <a href="gs.html">Getting Started</a>.</a>
     45 
     46 <h2 id="requirements">Requirements</h2>
     47 <p>For the web server:</p>
     48 <ul>
     49   <li> <a href="http://ant.apache.org/">Ant 1.8</a> (it might work with earlier versions, but it's not guaranteed).</li>
     50   <li>One of the following:
     51     <ul>
     52       <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>
     53       <li><a href="http://code.google.com/appengine/">Java App Engine SDK</a>version 1.6 or later.</li>
     54     </ul>
     55   </li>
     56   <li>A Google account registered to use GCM.</li>
     57   <li>The API  key for that account.</li>
     58 </ul>
     59 <p>For the Android application:</p>
     60 <ul>
     61   <li>Emulator (or device) running Android 2.2 with Google APIs.</li>
     62   <li>The Google API project number of the account registered to use GCM.</li>
     63 </ul>
     64 <h2 id="gcm-setup">Setting Up GCM</h2>
     65 <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>
     66 <p>For instructions on how to set up GCM, see <a href="gs.html">Getting Started</a>.</p>
     67 
     68 
     69 <h2 id="server-setup">Setting Up an HTTP Server</h2>
     70 <p>This section describes the different options for setting up an HTTP server.</p>
     71 <h3 id="webserver-setup">Using a standard web server</h3>
     72 <p>To set up the server using a standard, servlet-compliant web server:</p>
     73 <ol>
     74   <li>From the <a href="http://code.google.com/p/gcm">open source site</a>, download the following directories: <code>gcm-server</code>, <code>samples/gcm-demo-server</code>, and <code>samples/gcm-demo-appengine</code>.</p>
     75 
     76 
     77   <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>
     78   <li>In a shell window, go to the <code>samples/gcm-demo-server</code> directory.</li>
     79   <li>Generate the server's WAR file by running <code>ant war</code>:</li>
     80   
     81   <pre class="prettyprint">$ ant war
     82 
     83 Buildfile:build.xml
     84 
     85 init:
     86    [mkdir] Created dir: build/classes
     87    [mkdir] Created dir: dist
     88 
     89 compile:
     90    [javac] Compiling 6 source files to build/classes
     91 
     92 war:
     93      [war] Building war: <strong>dist/gcm-demo.war</strong>
     94 
     95 BUILD SUCCESSFUL
     96 Total time: 0 seconds
     97 </pre>
     98   
     99   <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>
    100   <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.
    101     
    102   </li>
    103 </ol>
    104 <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>
    105 
    106 <p> You server is now ready.</p>
    107 
    108 <h3 id="appengine-setup">Using App Engine for Java</h3>
    109 
    110 <p>To set up the server using a standard App Engine for Java:</p>
    111 <ol>
    112   <li>Get the files from the <a href="http://code.google.com/p/gcm">open source site</a>, as described above.</p>
    113   </li>
    114   <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.
    115 
    116   <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>
    117   
    118   </li>
    119   <li>In a shell window, go to the <code>samples/gcm-demo-appengine</code> directory.</li>
    120   <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>
    121 
    122 <pre class="prettyprint">
    123 $ ant -Dsdk.dir=/opt/google/appengine-java-sdk runserver -Dserver.host=192.168.1.10
    124 Buildfile: gcm-demo-appengine/build.xml
    125 
    126 init:
    127     [mkdir] Created dir: gcm-demo-appengine/dist
    128 
    129 copyjars:
    130 
    131 compile:
    132 
    133 datanucleusenhance:
    134   [enhance] DataNucleus Enhancer (version 1.1.4) : Enhancement of classes
    135   [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
    136   [enhance] DataNucleus Enhancer completed and no classes were enhanced. Consult the log for full details
    137 
    138 runserver:
    139      [java] Jun 15, 2012 8:46:06 PM com.google.apphosting.utils.jetty.JettyLogger info
    140      [java] INFO: Logging to JettyLogger(null) via com.google.apphosting.utils.jetty.JettyLogger
    141      [java] Jun 15, 2012 8:46:06 PM com.google.apphosting.utils.config.AppEngineWebXmlReader readAppEngineWebXml
    142      [java] INFO: Successfully processed gcm-demo-appengine/WebContent/WEB-INF/appengine-web.xml
    143      [java] Jun 15, 2012 8:46:06 PM com.google.apphosting.utils.config.AbstractConfigXmlReader readConfigXml
    144      [java] INFO: Successfully processed gcm-demo-appengine/WebContent/WEB-INF/web.xml
    145      [java] Jun 15, 2012 8:46:09 PM com.google.android.gcm.demo.server.ApiKeyInitializer contextInitialized
    146      [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!
    147      [java] Jun 15, 2012 8:46:09 PM com.google.appengine.tools.development.DevAppServerImpl start
    148      [java] INFO: The server is running at http://192.168.1.10:8080/
    149      [java] Jun 15, 2012 8:46:09 PM com.google.appengine.tools.development.DevAppServerImpl start
    150      [java] INFO: The admin console is running at http://192.168.1.10:8080/_ah/admin
    151 </pre>
    152 
    153   <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>
    154   
    155   <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>
    156   
    157 </ol>
    158 <p> You server is now ready.</p>
    159 
    160 
    161