5

我一直在努力让示例从下面运行:

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

我遇到的第一个问题是没有在 Android SDK 中安装“Google Cloud Messaging for Android Library”(显然我知道)。

但现在我对 Android 项目中两个文件中的自动生成代码有疑问:GCMIntentService.java 和 RegisterActivity.java

错误是:

  • 未为 Deviceinfoendpoint GCMIntentService.java 类型定义方法 getDeviceInfo(String)
  • 未定义 MessageEndpoint RegisterActivity.java 类型的方法 listMessages()
  • 未定义类型 Deviceinfoendpoint GCMIntentService.java 的方法 insertDeviceInfo(DeviceInfo)
  • 未为 Deviceinfoendpoint GCMIntentService.java 类型定义方法 removeDeviceInfo(String)

我在 Ubuntu 上使用 Java SDK v1.7.0_15,但我也尝试在 Windows 7 上使用 Java SDK v1.6 并遇到同样的问题。最新的 Android 平台 4.2.2 和 Google App Engine 1.7.7。Eclipse 是 Juno Service Release 2。

问题看起来像是他们做错了一些转换,因为在 Deviceinfoendpoint 中有一个方法 getDeviceInfo 用于内部类 DeviceInfoEndpoint (不同的capatilisations)。

我可以尝试修复它,但只是想知道我的设置是否有问题导致这种情况发生?

任何帮助,将不胜感激。

4

2 回答 2

3

在您的 GCMIntentService.java 类中,在出现错误的行中的端点对象之后添加 .deviceInfoEndpoint(),如下所示:

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

在 RegisterActivity.java 中更改行

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

messageEndpoint.messageEndpoint().listMessages().setLimit(5).execute();
于 2013-04-17T15:40:24.150 回答
2

我会确保您使用与 JAR 相同版本的 GCM API。已经进行了相当多的修订。

我将以下代码与 gcm-server.jar 一起使用,以 19718 字节列出。

我成功用于向设备发送 GCM 消息的代码是:

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();
    }
}
于 2013-04-16T23:11:21.907 回答