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()); @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(); } @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(); } @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(); }
source share