【问题标题】:Google App Engine Java and Android Getting StartedGoogle App Engine Java 和 Android 入门
【发布时间】:2013-04-16 21:50:00
【问题描述】:

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

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 内部类 DeviceInfoEndpoint 有一个 getDeviceInfo 方法(不同的capatilisations)。

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

任何帮助将不胜感激。

【问题讨论】:

  • 您的问题出在您的应用项目或应用引擎项目上?你可以为这些错误发布一些堆栈跟踪吗?
  • 问题出在应用项目上。没有堆栈跟踪,因为它还不会编译。
  • 昨天晚上遇到了完全相同的问题。还尝试了两台具有不同软件设置的不同机器。

标签: java android google-app-engine google-cloud-messaging


【解决方案1】:

在您的 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();

【讨论】:

  • 非常感谢!那么他们的自动生成代码肯定有一些错误。
  • 没问题。但是你真的让示例项目工作了吗?我让 Android 应用程序正常运行,它注册得很好,但后端没有看到任何设备。
【解决方案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();
    }
}

【讨论】:

    猜你喜欢
    • 2015-03-05
    • 1970-01-01
    • 2011-04-02
    • 1970-01-01
    • 2015-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多