How to register an Android device to receive push notifications through the App Engine backend?

I created a sample android project with an Android app and App Engine as a backend using this tutorial . But in step 2.4, I was stuck in showing push notifications from the GCM server. When I debug, I found that my RegistrastionRecord has 0 registered devices. So, how to properly register an application to receive push notifications using the local App Engine backend?

I got this error:

Failed to complete token refresh com.google.api.client.googleapis.json.GoogleJsonResponseException: 404 Not Found <html><head><title>Error 404</title></head> <body><h2>Error 404</h2></body> </html> 

Here is my registration endpoint:

 @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<RegistrationRecord> listDevices(@Named("count") int count) { List<RegistrationRecord> records = ofy().load().type(RegistrationRecord.class).limit(count).list(); return CollectionResponse.<RegistrationRecord>builder().setItems(records).build(); } private RegistrationRecord findRecord(String regId) { return ofy().load().type(RegistrationRecord.class).filter("regId", regId).first().now(); } 

}

Here is sendRegistrationToServer (). I call it onHandleIntent () from RegistrationIntentService:

  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<?> abstractGoogleClientRequest) throws IOException { abstractGoogleClientRequest.setDisableGZipContent(true); } }); Registration regService = builder.build(); regService.register(token).execute(); //regService.register(token).execute(); } 
+1
source share
2 answers

I had this problem for a long time. After I upgrade the plugin for all libraries to the latest version, it works so well for me.

Plugin:

classpath 'com.google.appengine:gradle-appengine-plugin:1.9.34'

Libraries:

 ext { appEngineVersion = '1.9.38' } dependencies { appengineSdk "com.google.appengine:appengine-java-sdk:${appEngineVersion}" compile "com.google.appengine:appengine-endpoints:${appEngineVersion}" compile "com.google.appengine:appengine-endpoints-deps:${appEngineVersion}" ... } 

Hope this helps.

+1
source

You received an error message Failed to update com.google.api.client.googleapis.json.GoogleJsonResponseException token: 404 Not Found because the resource associated with the request was not found. Redeploy the App Engine application and try calling him again.

Based on this question fooobar.com/questions/940779 / ... , you need to connect to https to connect to the endpoint, if you connect to http in your setRootUrl , then you can get 404 error. When using setRootUrl you should specify the full Endpoints URL using https . You also need to set the application identifier in appengine-web.xml when creating client libraries. This is returning to the final point in the generation.

This thread can also help.

0
source

All Articles