如何注册Android设备接收通过App Engine后端的推送通知? [英] How to register android device for receiving Push notifications via App Engine backend?

查看:103
本文介绍了如何注册Android设备接收通过App Engine后端的推送通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用本教程。但在步骤2.4中,我停留在显示来自GCM后端的推送通知。当我调试时,我发现我的RegistrastionRecord中有0个注册的设备。那么,如何使用App Engine本地后端正确注册接收推送通知的应用程序?



我得到这个错误:

 无法完成令牌刷新com.google.api.client.googleapis.json.GoogleJsonResponseException:404未找到
< html>< head>< title>错误404< / title>< / head>
< body>< h2>错误404< / h2>< / body>
< / html>

以下是我的注册端点:

<$ p






$ b $ owner $ = myapplication.Mikhail.example.com,
ownerName =backend.myapplication.Mikhail.example.com,
packagePath =


public类RegistrationEndpoint {

private static final Logger log = Logger.getLogger(RegistrationEndpoint.class.getName());
$ b $ / **
*注册一个设备到后端
*
* @param regId Google Cloud Messaging注册ID添加
* /
@ApiMethod(name =register)
public void registerDevice(@Named(regId)String regId){
if(findRecord(regId)!= null){
log.info(设备+ regId +已经注册,跳过注册);
return;
}
RegistrationRecord record = new RegistrationRecord();
record.setRegId(regId);
ofy()。save()。entity(record).now();
}

/ **
*从后端取消注册设备
*
* @param regId Google Cloud Messaging注册ID将删除
$ /
@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();
}

/ **
*返回已注册设备的集合
*
* @param count要列出的设备数量
* @返回Google Cloud Messaging注册列表Ids
* /
@ApiMethod(name =listDevices)
public CollectionResponse< RegistrationRecord> listDevices(@Named(count)int count){
List< RegistrationRecord>记录= ofy().load().type(RegistrationRecord.class).limit(count).list();
返回CollectionResponse。< RegistrationRecord> builder().setItems(records).build();

$ b $ private注册记录findRecord(String regId){
return ofy()。load().type(RegistrationRecord.class).filter(regId,regId)。 。第一()现在();
}

}

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

  private void sendRegistrationToServer(String token)throws IOException {
//根据需要添加自定义实现。

Registration.Builder builder = new Registration.Builder(AndroidHttp.newCompatibleTransport(),$ b $ new AndroidJsonFactory(),null)
//需要setRootUrl和setGoogleClientRequestInitializer仅用于本地测试,
//否则可以跳过
.setRootUrl(http://10.0.2.2:8080/_ah/api/)

.setGoogleClientRequestInitializer(新的GoogleClientRequestInitializer() {
@Override
public void initialize(AbstractGoogleClientRequest<> abstract>> abstractGoogleClientRequest)
throws IOException {
abstractGoogleClientRequest.setDisableGZipContent(true);
}
} );
注册regService = builder.build();
regService.register(token).execute();

//regService.register(token).execute();
}


解决方案

很长时间。在我将所有库升级到最新版本之后,它对我来说工作得很好。



插件:



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



库:

  ext {
appEngineVersion ='1.9.38'
}

相关性{
appengineSdkcom.google.appengine:appengine-java-sdk:$ {appEngineVersion}
compilecom.google.appengine:appengine-endpoints:$ {appEngineVersion}
编译com.google.appengine:appengine-endpoints-deps:$ {appEngineVersion}
...
}

希望这会有帮助。


I have created the sample android project with Android app and App Engine as a backend using this tutorial. 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?

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 the sendRegistrationToServer(). I call it from 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();
}

解决方案

I used to have this problem for long. After I upgrade the plugin all libraries to the latest version, it works so fine 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 will help.

这篇关于如何注册Android设备接收通过App Engine后端的推送通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆