如何注册Android设备以通过App Engine后端接收推送通知?

时间:2016-03-14 16:48:58

标签: android google-app-engine google-cloud-messaging

我使用this tutorial创建了使用Android应用和App Engine作为后端的示例android项目。但是在步骤2.4中,我被困在显示来自GCM后端的推送通知。当我调试时,我发现在我的RegistrastionRecord中有0个已注册的设备。那么,如何使用App Engine本地后端正确注册应用程序以接收推送通知?

我收到了这个错误:

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>

这是我的注册端点:

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

}

这是sendRegistrationToServer()。我从RegistrationIntentService的onHandleIntent()调用它:

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

2 个答案:

答案 0 :(得分:1)

我曾经有过这个问题。在我将所有库的插件升级到最新版本后,它对我来说非常好。

插件:

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

库:

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}"
    ...
}

希望这会有所帮助。

答案 1 :(得分:0)

您收到错误无法完成令牌刷新com.google.api.client.googleapis.json.GoogleJsonResponseException:404 Not Found ,因为找不到与请求关联的资源。请重新部署App Engine应用并尝试再次调用它。

根据此SO question,您必须与https连接才能连接到端点,如果您与http中的setRootUrl相关联,则可能会获得404错误。使用setRootUrl时,您必须使用https包含完整的端点网址。生成客户端库时,还需要在appengine-web.xml中设置应用程序ID。这会在发电时回到端点。

thread也可能会有所帮助。

相关问题