Google App Engine后端加上Google Cloud Messaging [英] Google App Engine backend with Google Cloud Messaging

查看:126
本文介绍了Google App Engine后端加上Google Cloud Messaging的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用下面的页面上的示例代码来试用基于GAE的后端:
https://github.com/GoogleCloudPlatform/gradle-appengine-templates/tree/master/GcmEndpoints



已能够部署后端,我可以查看&在API explorer上通过appspot.com执行API - project-id.appspot.com

当我执行客户端应用程序(基于Android)并调用
regService.register(regId).execute();
在服务器端,我在Google Developer Console上获得以下日志条目 -



'pre> POST /注册/ V1 / registerDevice / APA91bHCCvjkMFdvf6YHh_rbdqdKMYoRnwm6iswQtTpztwCfNVWq_7xwSq1y9naiipYmfTrREInybypeLb5mc7LCzYGBSpC9jFM-Co_6xGUBiEjLyo1UT375ak7p0nrOiTdHFNwW7r31WYQJP7ojigRLxBTYvST4XTeNIufD6GHb3SbDFGl1hsc HTTP / 1.1 404 0 - 20773xxxxxxx谷歌-HTTP-Java的客户机/ 1.17.0- rc(gzip)verlllll-auyyyy-zzz.appspot.comms = 19 cpu_ms = 0 app_engine_release = 1.9.7 trace_id = ddfbcf4e13e27e2aa2a6c5e77bb8cc6f

其中:


  1. 注册/ v1 / registerDevice /是API /版本/后端服务的方法

  2. APA91bHCCvjkMFdvf6YHh_rbdqdKMYoRnwm6iswQtTpztwCfNVWq_7xwSq1y9naiipYmfTrREInybypeLb5mc7 LCzYGBSpC9jFM-Co_6xGUBiEjLyo1UT375ak7p0nrOiTdHFNwW7r31WYQJP7ojigRLxBTYvST4XTeNIufD6GHb3SbDFGl1hsc
    ..是由gcm.register(SENDER_ID)返回的设备注册ID; gcm是GoogleCloudMessaging的类型

20773xxxxxxx或SENDER_ID Google开发者控制台。
& verllll-auyyyy-zzz.appspot.com是项目ID。



你能告诉我为什么我会在响应中获得HTTP / 1.1 404吗?



在此先感谢您。






共享regService的构建 -

  public class GcmRegistrationAsync extends AsyncTask< Context,Void,String> {
私人注册regService; //服务器中的Stub API
私有GoogleCloudMessaging gcm;
私有上下文上下文;

// TODO:将您自己的发件人ID更改为Google Developers Console项目
//数字,按照上述说明
private static final String SENDER_ID =20773xxxxxxx;

public void GcmRegistrationAsyncTask(int i){
Registration.Builder builder = new Registration.Builder(AndroidHttp.newCompatibleTransport(),new AndroidJsonFactory(),null)
// Need setRootUrl和setGoogleClientRequestInitializer仅用于本地测试,
//否则可以跳过
.setRootUrl(https://verlllll-auyyyy-zzz.appspot.com)
.setGoogleClientRequestInitializer(new GoogleClientRequestInitializer(){
@Override
public void initialize(AbstractGoogleClientRequest<> abstract> abstractGoogleClientRequest)throws IOException {
abstractGoogleClientRequest.setDisableGZipContent(true);
}
}) ;

i = this.test();
builder.setApplicationName(SENDER_ID);
regService = builder.build();
}

public int test(){
return 1;
}

@Override
保护字符串doInBackground(Context ... params){
int i = params.length;
context =(Context)params [0];

String msg =test;
尝试{
if(gcm == null){
gcm = GoogleCloudMessaging.getInstance(context);
}
字符串regId = gcm.register(SENDER_ID);
msg =设备注册,注册ID =+ regId;

//您应该通过HTTP将注册ID发送到您的服务器
//以便它可以使用GCM / HTTP或CCS将消息发送到您的应用程序。
//如果您的应用程序
//正在使用帐户,则应向您的服务器发送请求。
regService.register(regId).execute();

} catch(IOException ex){
ex.printStackTrace();
msg =错误:+ ex.getMessage();
}
return msg;
}

}

在MyActivity.java中

  protected void onCreate(Bundle savedInstanceState){


gcmRegistrationAsync = new GcmRegistrationAsync();
gcmRegistrationAsync.GcmRegistrationAsyncTask(1);
gcmRegistrationAsync.execute(this);
}


解决方案

必须连接 https ,如果你连接 http 就像你一样(在你的setUrl中),那么你得到404错误。



另外你的代码可能看起来更像:

  Registration.Builder builder = new Registration.Builder(AndroidHttp.newCompatibleTransport(),new AndroidJsonFactory(),null); 

rootUrl不需要设置,默认情况下它会使用您的appspot位置,将端点引导至您的devapp服务器(app引擎本地测试服务器)是非常有用的。



禁用压缩的代码也仅用于与devapp服务器兼容


I'm trying out a GAE based backend using the sample code on the page below: https://github.com/GoogleCloudPlatform/gradle-appengine-templates/tree/master/GcmEndpoints

Have been able to deploy the backend and I can view & execute the APIs on API explorer through appspot.com link - project-id.appspot.com

When I execute the client app (Android based) and call regService.register(regId).execute(); On the server side I get the following log entry on Google Developer Console -

"POST /registration/v1/registerDevice/APA91bHCCvjkMFdvf6YHh_rbdqdKMYoRnwm6iswQtTpztwCfNVWq_7xwSq1y9naiipYmfTrREInybypeLb5mc7LCzYGBSpC9jFM-Co_6xGUBiEjLyo1UT375ak7p0nrOiTdHFNwW7r31WYQJP7ojigRLxBTYvST4XTeNIufD6GHb3SbDFGl1hsc HTTP/1.1" 404 0 - "20773xxxxxxx Google-HTTP-Java-Client/1.17.0-rc (gzip)" "verlllll-auyyyy-zzz.appspot.com" ms=19 cpu_ms=0 app_engine_release=1.9.7 trace_id=ddfbcf4e13e27e2aa2a6c5e77bb8cc6f

where:

  1. registration/v1/registerDevice/ are API/version/Method of the backend service
  2. APA91bHCCvjkMFdvf6YHh_rbdqdKMYoRnwm6iswQtTpztwCfNVWq_7xwSq1y9naiipYmfTrREInybypeLb5mc7LCzYGBSpC9jFM-Co_6xGUBiEjLyo1UT375ak7p0nrOiTdHFNwW7r31WYQJP7ojigRLxBTYvST4XTeNIufD6GHb3SbDFGl1hsc .. is the device registration id returned by gcm.register(SENDER_ID); gcm is of type GoogleCloudMessaging

20773xxxxxxx or SENDER_ID is the Project number provided on the Google Developer Console. & verlllll-auyyyy-zzz.appspot.com is the Project Id.

Can you please tell me why am I getting HTTP/1.1 404 in the response?

Thanks in advance..


Sharing the building of regService --

public class GcmRegistrationAsync extends AsyncTask<Context, Void, String> {
private Registration regService;    // A Stub API from Server
private GoogleCloudMessaging gcm;
private Context context;

// TODO: change to your own sender ID to Google Developers Console project 
// number, as per instructions above
private static final String SENDER_ID = "20773xxxxxxx";

public void GcmRegistrationAsyncTask(int i) {
    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("https://verlllll-auyyyy-zzz.appspot.com")
    .setGoogleClientRequestInitializer(new GoogleClientRequestInitializer() {
        @Override
        public void initialize(AbstractGoogleClientRequest<?> abstractGoogleClientRequest) throws IOException {
            abstractGoogleClientRequest.setDisableGZipContent(true);
        }
    });

    i = this.test();
    builder.setApplicationName(SENDER_ID);
    regService = builder.build();
}

public int test () {
    return 1;
}

@Override
protected String doInBackground(Context... params) {
    int i = params.length;
    context = (Context) params[0];

    String msg = "test";
    try {
        if (gcm == null) {
            gcm = GoogleCloudMessaging.getInstance(context);
        }
        String regId = gcm.register(SENDER_ID);
        msg = "Device registered, registration ID=" + regId;

        // You should send the registration ID to your server over HTTP,
        // so it can use GCM/HTTP or CCS to send messages to your app.
        // The request to your server should be authenticated if your app
        // is using accounts.
        regService.register(regId).execute();

    } catch (IOException ex) {
        ex.printStackTrace();
        msg = "Error: " + ex.getMessage();
    }
    return msg;
}

}

In MyActivity.java

protected void onCreate(Bundle savedInstanceState) {
:
:
    gcmRegistrationAsync = new GcmRegistrationAsync();
    gcmRegistrationAsync.GcmRegistrationAsyncTask(1);
    gcmRegistrationAsync.execute(this);
}

解决方案

To connect to an endpoint you have to connect with https, if you connect with http like you did (in your setUrl) then you get the 404 error.

Also your code can probably look more like :

Registration.Builder builder = new  Registration.Builder(AndroidHttp.newCompatibleTransport(), new AndroidJsonFactory(), null);

The rootUrl doesn't need be set, by default it uses your appspot location, when running locally, it's useful to direct the endpoint to your devapp server (app engine local testing server).

The code that disables compression is also just used to be compatible with the devapp server

这篇关于Google App Engine后端加上Google Cloud Messaging的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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