Google App Engine Java and Android Getting Started

徘徊边缘 提交于 2020-02-21 12:53:26

问题


I've been struggling to get the example running from below:

https://developers.google.com/eclipse/docs/getting_started

The first problem I had was didn't have installed 'Google Cloud Messaging for Android Library' in the Android SDK (obvious I know).

But now I have an issue with the auto-generated code in two files in the Android project: GCMIntentService.java and RegisterActivity.java

The errors are:

  • The method getDeviceInfo(String) is undefined for the type Deviceinfoendpoint GCMIntentService.java
  • The method listMessages() is undefined for the type MessageEndpoint RegisterActivity.java
  • The method insertDeviceInfo(DeviceInfo) is undefined for the type Deviceinfoendpoint GCMIntentService.java
  • The method removeDeviceInfo(String) is undefined for the type Deviceinfoendpoint GCMIntentService.java

I'm using Java SDK v1.7.0_15 on Ubuntu but I also tried on Windows 7 with Java SDK v1.6 and had the same issue. Latest Android Platform 4.2.2 and Google App Engine 1.7.7. Eclipse is Juno Service Release 2.

The problem looks like they are doing some casting wrong, because there is a method getDeviceInfo for inner class DeviceInfoEndpoint inside Deviceinfoendpoint (different capatilisations).

I could try and fix it, but just wondering if I have something wrong in my setup for this to be happening?

Any help would be appreciated.


回答1:


In your GCMIntentService.java class, add .deviceInfoEndpoint() after the endpoint object in the lines with errors as shown below:

DeviceInfo existingInfo = endpoint.getDeviceInfo(registration)
DeviceInfo existingInfo = endpoint.deviceInfoEndpoint().getDeviceInfo(registration)

In RegisterActivity.java change the line

messageEndpoint.listMessages().setLimit(5).execute();

to

messageEndpoint.messageEndpoint().listMessages().setLimit(5).execute();



回答2:


I would make sure you are using the same version of GCM APIs as you have JARs for. There have been quite a few revisions.

I am using the following code with gcm-server.jar, listed at 19718 bytes.

The code I successfully use to send GCM messages to a device is:

public void sendMessage() {
    String notificationToken = mobileDevice.getPushNotificationCode();
    String deviceType = mobileDevice.getDeviceType();

    Sender sender = new Sender(BROWSER_API_KEY);
    Message message = new Message.Builder().addData("message", "blah blah").build();
    String device = "<the key for the device you are sending to goes here>";

    try {
        System.out.println("Sending message...");
        Result result = sender.send(message, device, 5);
        System.out.println("Done sending message");
        if (result.getMessageId() != null) {
            System.out.println("Got message ID: " + result.getMessageId());
            System.out.println("Got error code name: " + result.getErrorCodeName());
            System.out.println("result: " + result);
            String canonicalRegId = result.getCanonicalRegistrationId();
            if (canonicalRegId != null) {
                // Database has more than one record for this device.
                // Replace all of this device's records with this new id
                System.out.println("Got new canonical reg id: " + canonicalRegId);
            }
        } else {
            String error = result.getErrorCodeName();
            if (error.equals(com.google.android.gcm.server.Constants.ERROR_NOT_REGISTERED)) {
                // application has been removed from device - unregister from database
                System.out.println("Got error: " + error);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}


来源:https://stackoverflow.com/questions/16047737/google-app-engine-java-and-android-getting-started

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!