Google App Engine backend with Google Cloud Messag

2019-05-20 09:31发布

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

1条回答
走好不送
2楼-- · 2019-05-20 09:46

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

查看更多
登录 后发表回答