Android google云消息传递示例无效

时间:2013-06-18 18:03:39

标签: cloud messaging google-cloud-messaging

我按照此处的教程http://developer.android.com/google/gcm/gs.html来设置GCM。 我目前正在尝试注册该设备。 但是,出于某种原因,应用程序似乎总是崩溃 gcm = GoogleCloudMessaging.getInstance(this);

stacktrace似乎指向以下内容: 06-18 13:42:20.909:I / dalvikvm(11613):找不到方法com.google.android.gms.gcm.GoogleCloudMessaging.getInstance,引用方法pushNotification.PushNotification $ 1.run

以下是我目前所拥有的样本

public PushNotification(Context c, Activity activity)
{
    context = c;
    this.activity = activity;
    regid = getRegistrationId(context);

    if (regid.length() == 0) {
        Log.d("IN PUSHNOTIFICATION ","NOT REGISTERED. REGISTERING NOW.....");
        registerBackground();
    }
    Log.d("IN PUSHNOTIFICATION ","REGISTRATION COMPLETE.....");
    Log.d("IN PUSHNOTIFICATION ","REGISTRATION ID IS: " + regid);
    gcm = GoogleCloudMessaging.getInstance(activity); //never reaches this code

}

private void registerBackground() {

    Thread thread = new Thread(new Runnable()
    {
        public void run()
        {
            String msg = "";
            try {
                if (gcm == null) {
                    Log.d("IN PUSHNOTIFICATION", "IN BACKGROUND. gcm == NULL");
                    gcm = GoogleCloudMessaging.getInstance(context);
                    Log.d("IN PUSHNOTIFICATION", "IN BACKGROUND. AFTER gcm.GETINSTANCE");
                }
                Log.d("IN PUSHNOTIFICATION", "IN BACKGROUND. BEFORE gcm.register");
                regid = gcm.register(SENDER_ID);
                msg = "Device registered, registration id=" + regid;
                Log.d("IN PUSHNOTIFICATION", "IN BACKGROUND. regid == " + 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.

                // For this demo: we don't need to send it because the device
                // will send upstream messages to a server that echo back the message
                // using the 'from' address in the message.

                // Save the regid - no need to register again.
                setRegistrationId(context, regid);
            } catch (IOException ex) {
                Log.d("IN PUSHNOTIFICATION", "IN BACKGROUND. EXCEPTION ERROR ERROR: ex = " + ex.getMessage());
                msg = "Error :" + ex.getMessage();
            }
        }
    }
    );
    thread.start();
}

这是完整的堆栈跟踪

  

06-18 13:42:20.909:D / IN PUSHNOTIFICATION(11613):未注册。   现在注册..... 06-18 13:42:20.909:我/ dalvikvm(11613):不能   找到方法   com.google.android.gms.gcm.GoogleCloudMessaging.getInstance,   从方法pushNotification.PushNotification $ 1.run 06-18引用   13:42:20.909:W / dalvikvm(11613):VFY:无法解析静态方法   5967:Lcom / google / android / gms / gcm / GoogleCloudMessaging; .getInstance   (Landroid /内容/上下文)LCOM /谷歌/机器人/克/ GCM / Google云端通讯;   06-18 13:42:20.909:D / dalvikvm(11613):VFY:替换操作码0x71 at   0x0015 06-18 13:42:20.909:I / dalvikvm(11613):找不到方法   引用了com.google.android.gms.gcm.GoogleCloudMessaging.register   来自方法pushNotification.PushNotification $ 1.run 06-18   13:42:20.909:W / dalvikvm(11613):VFY:无法解析虚方法   5969:Lcom / google / android / gms / gcm / GoogleCloudMessaging; .register   ([Ljava /郎/字符串;)Ljava /郎/字符串; 06-18 13:42:20.909:   D / dalvikvm(11613):VFY:在0x0039 06-18处替换操作码0x6e   13:42:20.909:D / IN PUSHNOTIFICATION(11613):注册完成.....   06-18 13:42:20.909:D / IN PUSHNOTIFICATION(11613):注册ID为:   06-18 13:42:20.909:D / AndroidRuntime(11613):关闭VM 06-18   13:42:20.909:W / dalvikvm(11613):threadid = 1:线程退出   未捕获的异常(组= 0x2b542210)06-18 13:42:20.909:D / IN   PUSHNOTIFICATION(11613):在背景中。 gcm == NULL 06-18   13:42:20.909:W / dalvikvm(11613):threadid = 20:线程退出   未捕获的异常(组= 0x2b542210)06-18 13:42:20.909:   I /处理(11613):发送信号。 PID:11613 SIG:9 06-18   13:42:20.919:I / ActivityManager(278):处理com.gotoohlala(pid   11613)已经死了。

3 个答案:

答案 0 :(得分:4)

我解决了这个问题。 虽然我正在链接到google-play-services_lib项目,但我还必须将jar文件google-play-services_lib.jar添加到项目中。

答案 1 :(得分:1)

对于其他人可能会因为像我这样的错误而疯狂。 以下这些错误意味着同样的事情:

无法找到方法com.google.android.gms.gcm.GoogleCloudMessaging.getInstance java.lang.NoClasDefFound:..... GoogleCloudMessaging

  
    
      

GoogleCloudMessaging类的jar文件未导出到Android运行时。

    
  

因为默认情况下,Android构建器不会从Android Private Library

导出jar文件

您可以通过查看bin/dexedLibs文件夹进行仔细检查,如果仍然看不到google-play-services-4064834317375a1fffbbc48c3994c697.jar< - 该文件未导出到运行时。

解决方案: Project Properties/Java Build Path/Order and Export/检查Android Private Library 现在你的所有jar文件都会出现在android设备上,并且不再出现运行时错误。

答案 2 :(得分:0)

我正在通过Microsoft Azure平台使用推送通知并遇到同样的问题。

我通过检查这里的8个步骤解决了这个问题: http://azure.microsoft.com/en-us/documentation/articles/mobile-services-android-get-started-push/#add-push

更准确地说,问题是未正确添加库google-play-services-lib,即使我添加了jar文件(步骤6和7)