在Android上实施GCM

时间:2015-07-05 09:34:40

标签: android google-cloud-messaging android-support-library

我想将GCM添加​​到我的应用中,所以我首先按照此链接中的教程进行操作:http://hmkcode.com/android-google-cloud-messaging-tutorial/

但是我无法从服务器发送任何消息,因为它总是返回InvalidRegistration,即使我只是复制粘贴我在我的应用程序中获得的注册,这是由gcm.register()方法生成的。所以我查看了官方页面

https://developers.google.com/cloud-messaging/android/client

表示不推荐使用register(),应该使用InstanceID API。现在我按照页面上的说明进行操作。我收到以下错误:

 07-05 14:49:17.066: E/AndroidRuntime(25823): FATAL EXCEPTION: AsyncTask #1
07-05 14:49:17.066: E/AndroidRuntime(25823): Process: com.example.studentapp, PID: 25823
07-05 14:49:17.066: E/AndroidRuntime(25823): java.lang.RuntimeException: An error occured while executing doInBackground()
07-05 14:49:17.066: E/AndroidRuntime(25823):    at android.os.AsyncTask$3.done(AsyncTask.java:300)
07-05 14:49:17.066: E/AndroidRuntime(25823):    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
07-05 14:49:17.066: E/AndroidRuntime(25823):    at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
07-05 14:49:17.066: E/AndroidRuntime(25823):    at java.util.concurrent.FutureTask.run(FutureTask.java:242)
07-05 14:49:17.066: E/AndroidRuntime(25823):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
07-05 14:49:17.066: E/AndroidRuntime(25823):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
07-05 14:49:17.066: E/AndroidRuntime(25823):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
07-05 14:49:17.066: E/AndroidRuntime(25823):    at java.lang.Thread.run(Thread.java:818)
07-05 14:49:17.066: E/AndroidRuntime(25823): Caused by: java.lang.NoSuchMethodError: No virtual method getNoBackupFilesDir(Landroid/content/Context;)Ljava/io/File; in class Landroid/support/v4/content/ContextCompat; or its super classes (declaration of 'android.support.v4.content.ContextCompat' appears in /data/app/com.example.studentapp-2/base.apk)
07-05 14:49:17.066: E/AndroidRuntime(25823):    at com.google.android.gms.iid.zzd.zzde(Unknown Source)
07-05 14:49:17.066: E/AndroidRuntime(25823):    at com.google.android.gms.iid.zzd.<init>(Unknown Source)
07-05 14:49:17.066: E/AndroidRuntime(25823):    at com.google.android.gms.iid.zzd.<init>(Unknown Source)
07-05 14:49:17.066: E/AndroidRuntime(25823):    at com.google.android.gms.iid.InstanceID.zza(Unknown Source)
07-05 14:49:17.066: E/AndroidRuntime(25823):    at com.google.android.gms.iid.InstanceID.getInstance(Unknown Source)
07-05 14:49:17.066: E/AndroidRuntime(25823):    at com.example.studentapp.register_gcm.doInBackground(register_gcm.java:41)
07-05 14:49:17.066: E/AndroidRuntime(25823):    at com.example.studentapp.register_gcm.doInBackground(register_gcm.java:1)
07-05 14:49:17.066: E/AndroidRuntime(25823):    at android.os.AsyncTask$2.call(AsyncTask.java:288)
07-05 14:49:17.066: E/AndroidRuntime(25823):    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
07-05 14:49:17.066: E/AndroidRuntime(25823):    ... 4 more

所以我在下面的链接中跟着另一个类似的问题:

Problems implementing the new GCM Client for Android

我有支持库的修订版19.1修订版,我无法使用sdk管理器进一步升级。

我的注册代码是

package com.example.studentapp;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;

import com.google.android.gms.gcm.GoogleCloudMessaging;
import com.google.android.gms.iid.InstanceID;

public  class register_gcm extends AsyncTask<Object, Integer, String> {


     GoogleCloudMessaging gcm;
     Context c;
     String regid;
     String PROJECT_NUMBER = "981827135766";






                @Override
                protected String doInBackground(Object... arg0) {
                    // TODO Auto-generated method stub


                     String msg = "";
                        try {
                          /*  if (gcm == null) {
                                gcm = GoogleCloudMessaging.getInstance(c);
                            }
                            regid = gcm.register(PROJECT_NUMBER);
                            msg = "Device registered, registration ID=" + regid;
                            Log.d("GCM",  msg + regid);
                            */
                            InstanceID instanceID = InstanceID.getInstance(c);
                            String token = instanceID.getToken(PROJECT_NUMBER,
                                    GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
                            Log.d("GCM",  token);

                        } catch (IOException ex) {
                            msg = "Error :" + ex.getMessage();

                        }
                        return regid;
                    }


                @Override
                protected void onPostExecute(String msg) {

                      try {

                            FileOutputStream fos = c.openFileOutput("register_status.zaf", Context.MODE_PRIVATE);

                            fos.write(regid.getBytes());

                            fos.close();

                        } catch (Exception e) {

                            e.printStackTrace();

                        }




                }



}

所以我该如何解决这个问题呢?

1 个答案:

答案 0 :(得分:0)

没关系,

我所做的是,在较新版本的Eclipse上安装最新的ADT插件并更新支持库并将构建目标更改为API 22