title: How to register android device for receiving Push notifications via App Engine backend?
body: <p>I have created the sample android project with Android app and App Engine as a backend using <a href="https://github.com/GoogleCloudPlatform/gradle-appengine-templates/tree/master/GcmEndpoints" rel="nofollow">this tutorial</a>. But on the step 2.4, I am stuck in showing push notifications from GCM backend. When I debug, I found that there are 0 registered device in my RegistrastionRecord. So, how to correctly register the app for receiving push notifications using App Engine local backend?</p>
  
  <p>I got this error:</p>
  
  <pre><code>Failed to complete token refresh  com.google.api.client.googleapis.json.GoogleJsonResponseException: 404 Not Found
                                                                       &lt;html&gt;&lt;head&gt;&lt;title&gt;Error 404&lt;/title&gt;&lt;/head&gt;
                                                                       &lt;body&gt;&lt;h2&gt;Error 404&lt;/h2&gt;&lt;/body&gt;
                                                                       &lt;/html&gt;
  </code></pre>
  
  <p>Here is my Registration Endpoint:</p>
  
  <pre><code>@Api(
      name = "registration",
      version = "v1",
      namespace = @ApiNamespace(
              ownerDomain = "backend.myapplication.Mikhail.example.com",
              ownerName = "backend.myapplication.Mikhail.example.com",
              packagePath=""
      )
   )
    public class RegistrationEndpoint {
  
  private static final Logger log = Logger.getLogger(RegistrationEndpoint.class.getName());
  
  /**
   * Register a device to the backend
   *
   * @param regId The Google Cloud Messaging registration Id to add
   */
  @ApiMethod(name = "register")
  public void registerDevice(@Named("regId") String regId) {
      if (findRecord(regId) != null) {
          log.info("Device " + regId + " already registered, skipping register");
          return;
      }
      RegistrationRecord record = new RegistrationRecord();
      record.setRegId(regId);
      ofy().save().entity(record).now();
  }
  
  /**
   * Unregister a device from the backend
   *
   * @param regId The Google Cloud Messaging registration Id to remove
   */
  @ApiMethod(name = "unregister")
  public void unregisterDevice(@Named("regId") String regId) {
      RegistrationRecord record = findRecord(regId);
      if (record == null) {
          log.info("Device " + regId + " not registered, skipping unregister");
          return;
      }
      ofy().delete().entity(record).now();
  }
  
  /**
   * Return a collection of registered devices
   *
   * @param count The number of devices to list
   * @return a list of Google Cloud Messaging registration Ids
   */
  @ApiMethod(name = "listDevices")
  public CollectionResponse&lt;RegistrationRecord&gt; listDevices(@Named("count") int count) {
      List&lt;RegistrationRecord&gt; records = ofy().load().type(RegistrationRecord.class).limit(count).list();
      return CollectionResponse.&lt;RegistrationRecord&gt;builder().setItems(records).build();
  }
  
  private RegistrationRecord findRecord(String regId) {
      return ofy().load().type(RegistrationRecord.class).filter("regId", regId).first().now();
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Here is the sendRegistrationToServer(). I call it from onHandleIntent() from RegistrationIntentService:</p>
  
  <pre><code>   private void sendRegistrationToServer(String token) throws IOException {
      // Add custom implementation, as needed.
  
      Registration.Builder builder = new Registration.Builder(AndroidHttp.newCompatibleTransport(),
              new AndroidJsonFactory(), null)
              // Need setRootUrl and setGoogleClientRequestInitializer only for local testing,
              // otherwise they can be skipped
              .setRootUrl("http://10.0.2.2:8080/_ah/api/")
  
              .setGoogleClientRequestInitializer(new GoogleClientRequestInitializer() {
                  @Override
                  public void initialize(AbstractGoogleClientRequest&lt;?&gt; abstractGoogleClientRequest)
                          throws IOException {
                      abstractGoogleClientRequest.setDisableGZipContent(true);
                  }
              });
      Registration regService = builder.build();
      regService.register(token).execute();
  
      //regService.register(token).execute();
  }
  </code></pre>