android - 如何从 GCM 获取注册 ID

标签 android google-cloud-messaging

我正在开发一个应用程序,因为我必须将 GCM 的注册 ID 从应用程序发送到我自己的服务器我写了一些代码它不起作用请告诉我哪里出错了

myAsynTask 类

语境语境;

    public static final String EXTRA_MESSAGE = "message";
    public static final String PROPERTY_REG_ID = "registration_id";

    String SENDER_ID = "538459826415";

    static final String TAG = "GCMDemo";
    GoogleCloudMessaging gcm;

    String msg;


    String regid;

@Override
protected Void doInBackground(DetailsDTO... params) {
    // TODO Auto-generated method stub
    httpClient  = new DefaultHttpClient();
    httpPost = new HttpPost(TrafficConstants.USER_REG_URL);


    try{
    gcm = GoogleCloudMessaging.getInstance(context);
    regid = gcm.register(SENDER_ID);
    msg = "Dvice registered, registration ID=" + regid;
    Log.d("111", msg);

    }
    catch(IOException e){
        e.printStackTrace();
    }

    try{
    arrayList = new ArrayList<NameValuePair>(=1);

    arrayList.add(new BasicNameValuePair("rigid", regid));

    httpPost.setEntity(new UrlEncodedFormEntity(arrayList));
    HttpResponse response = httpClient.execute(httpPost);
    }
    catch(Exception e){
        e.printStackTrace();
    }
    return null;

}

日志猫

 02-20 15:26:50.915: W/System.err(5400): java.io.IOException: INVALID_SENDER
   02-20 15:26:50.925: W/System.err(5400):  at     
   com.google.android.gms.gcm.GoogleCloudMessaging.register(Unknown Source)
   02-20 15:26:50.925: W/System.err(5400):  at    
   com.technowellServices.locationfind.async.UserDetailsAsync.doInBackground
    (UserDetailsAsync.j    ava:82)
    02-20 15:26:50.925: W/System.err(5400):     at 

    com.technowellServices.locationfind.async.UserDetailsAsync.doInBackground
   (UserDetailsAsync.j    ava:1)
   02-20 15:26:50.925: W/System.err(5400):  at   
   android.os.AsyncTask$2.call(AsyncTask.java:287)
    02-20 15:26:50.925: W/System.err(5400):     at   
   android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
   02-20 15:26:50.935: W/System.err(5400):  at    
   java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
   02-20 15:26:50.935: W/System.err(5400):  at   
   java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
   02-20 15:26:50.935: W/System.err(5400):  at java.lang.Thread.run(Thread.java:856)

最佳答案

看来你的发件人id不正确

Supply a project name and click Create.

Once the project has been created, a page appears that displays your project ID and project number.

For example, Project Number: 670330094152.

您应该在 Google API 控制台内的项目页面中激活 GCM 服务

Server Application Key
Android Application Key

选项也生成了。

然后在 Android Application Key 中,您应该使用分号分隔散列码和您的应用程序包名称。

AA:98:0C:AA:03:BD:E7:DA:AA:4A:FA:AF:73:3F:72:E7:D4:11:4S:B1;com.example.myapp

项目编号是发件人 ID。

检查一下,然后重试。

关于android - 如何从 GCM 获取注册 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21904293/

相关文章:

android - 检索项目 : No resource found that matches the given name 'Theme.AppCompat.Light.DarkActionBar' . 的父项时出错 visual studio xamarin

google-cloud-platform - Google Cloud Pub-Sub 的 Avro 消息?

android - 从服务器检查设备可用性的最佳方法是什么

android - 打开通知时出现空指针异常

c# - 添加 HttpClient header 会生成具有某些值的 FormatException

android - 如何用android 4.0.4源代码制作android sdk 4.0.3?

android - 对设备使用 OAuth 2.0 :Some requested scopes were invalid

javascript - 使用谷歌云消息和服务 worker 的桌面推送通知

android - KotterKnife - 某些类不能使用 bindView(R.id.example_id)

android - 自定义 ArrayAdapter getView 提供了错误的位置