GCMRegistrar getRegistrationId返回空ID

F4K*_*4Ke 11 android google-cloud-messaging

可能重复:
Android GCM:GCMRegistrar提供空注册ID

我已经按照编写服务器端应用程序在我的应用程序中实现GCM,但是

        final String regId = GCMRegistrar.getRegistrationId(this);
Run Code Online (Sandbox Code Playgroud)

回归empy.每时每刻.

我正确地放了所有权限.

有代码:

    public void registerPush(){

  GCMRegistrar.checkDevice(this);
  GCMRegistrar.checkManifest(this);
  final String regId = GCMRegistrar.getRegistrationId(this);
  REG_ID = regId;
  if (regId.equals("")) {
   GCMRegistrar.register(this, this.getString(R.string.SENDER_ID));
  } else {
   if (GCMRegistrar.isRegisteredOnServer(this)) {
    // Skips registration.
   } else {
    // Try to register again, but not in the UI thread.
    // It's also necessary to cancel the thread onDestroy(),
    // hence the use of AsyncTask instead of a raw thread.
    final Context context = this;
    mRegisterTask = new AsyncTask<Void, Void, Void>() {
     @Override
     protected Void doInBackground(Void... params) {
      boolean registered = forceRegister(context, regId);
      // At this point all attempts to register with the app
      // server failed, so we need to unregister the device
      // from GCM - the app will try to register again when
      // it is restarted. Note that GCM will send an
      // unregistered callback upon completion, but
      // GCMIntentService.onUnregistered() will ignore it.


      if (!registered) {
       GCMRegistrar.unregister(context);
      }
      return null;
     }

     @Override
     protected void onPostExecute(Void result) {
      mRegisterTask = null;
     }

    };
    mRegisterTask.execute(null, null, null);
   }
  }
 }
Run Code Online (Sandbox Code Playgroud)

谢谢您的帮助!

编辑: manifest.xml

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

<permission
    android:name="package.permission.C2D_MESSAGE"
    android:protectionLevel="signature" />
<uses-permission android:name="package.permission.C2D_MESSAGE" />

<application>
      <receiver
        android:name="com.google.android.gcm.GCMBroadcastReceiver"
        android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <action android:name="com.google.android.c2dm.intent.REGISTRATION" />

            <category android:name="package" />
        </intent-filter>
    </receiver>

    <service android:name="package.GCMIntentService" />

</application>
Run Code Online (Sandbox Code Playgroud)

HGP*_*GPB 20

为我修好的是该设备卡在了WAKE LOCK中.

长话短说,日志指出它试图在这里找到我的服务的事实:

IntentService类:com.myapppackage.GCMIntentService

我希望它指向这里:

IntentService类:com.myapppackage.gcm.GCMIntentService

所以我扩展了广播接收器如下:

package com.myapppackage.gcm;

import android.content.Context;

public class GCMBroadcastReceiver extends
        com.google.android.gcm.GCMBroadcastReceiver {

    @Override
    protected String getGCMIntentServiceClassName(Context context) {

        return "com.myapppackage.gcm.GCMIntentService";
    }
}
Run Code Online (Sandbox Code Playgroud)

然后我更新了我的清单以反映变化:

<receiver 
    android:name="com.myapppackage.gcm.GCMBroadcastReceiver" 
    android:permission="com.google.android.c2dm.permission.SEND" >
    <intent-filter>
        <action android:name="com.google.android.c2dm.intent.RECEIVE" />
        <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
        <category android:name="com.myapppackage" />
    </intent-filter>
</receiver>
Run Code Online (Sandbox Code Playgroud)

这在文档中解释(第5点):

http://developer.android.com/guide/google/gcm/gs.html#android-app

此意图服务将由GCMBroadcastReceiver(由GCM库提供)调用,如下一步所示.它必须是com.google.android.gcm.GCMBaseIntentService的子类,必须包含公共构造函数,并且应命名为my_app_package.GCMIntentService(除非您使用GCMBroadcastReceiver的子类来覆盖用于命名服务的方法).

我能说什么,我是金牛座我喜欢正确地组织我的东西!