2012-09-05 245 views
0

我正在執行GCM到我的應用程序中。我以教程here爲起點。regId始終爲空

的問題是,我的regId總是null

GCMRegistrar.checkDevice(this); 
GCMRegistrar.checkManifest(this); 
final String regId = GCMRegistrar.getRegistrationId(this); 

if (regId.equals("")) 
{ 
    GCMRegistrar.register(this, "627xxxx78"); 
} 
else 
{ 
    if (GCMRegistrar.isRegisteredOnServer(this)) 
    { 
     Log.i(TAG, "Already registered regId : " + regId); 
    } 
    else 
    { 
     final Context context = this; 

     mRegisterTask = new AsyncTask<Void, Void, Void>() { 
      @Override 
      protected Void doInBackground(Void... params) { 
       boolean registered = 
         ServerUtilities.register(context, regId); 

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

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

     mRegisterTask.execute(null, null, null); 
    } 
} 

在上面的代碼,ServerUtilities.register不會被調用,因爲GCMRegistrar.getRegistrationId(this);總是返回一個空字符串。

任何想法?

+1

你可以發佈你的清單文件 – Chet

+0

你在'GCMIntentService'中註冊請求的結果是什麼? – CommonsWare

回答

2

我有同樣的問題。添加以下在你的應用程序清單文件:

  package="yourApplicationPackage" 

     <uses-permission android:name="android.permission.GET_ACCOUNTS"/> 
     <uses-permission android:name="android.permission.WAKE_LOCK"/> 
    <permission 
     android:name="yourApplicationPackage.permission.C2D_MESSAGE" 
     android:protectionLevel="signature" /> 
    <uses-permission 
     android:name="yourApplicationPackage.permission.C2D_MESSAGE" /> 
    <!-- This app has permission to register and receive data message. --> 
    <uses-permission 
     android:name="com.google.android.c2dm.permission.RECEIVE" /> 

    <receiver 
     android:name="com.google.android.gcm.GCMBroadcastReceiver" 
     android:permission="com.google.android.c2dm.permission.SEND" > 
     <intent-filter> 
      <!-- Receives the actual messages. --> 
      <action android:name="com.google.android.c2dm.intent.RECEIVE" /> 
      <!-- Receives the registration id. --> 
      <action android:name="com.google.android.c2dm.intent.REGISTRATION" /> 
      <category android:name="yourApplicationPackage" /> 
     </intent-filter> 
    </receiver> 
    <service android:name="yourApplicationPackage.GCMIntentService"/> 

必須保持這種「yourApplicationPackage.GCMIntentService」在應用程序包

yourApplicationPackage.GCMIntentService代碼是這樣的。

 public class GCMIntentService extends GCMBaseIntentService { 


@Override 
protected void onError(Context arg0, String arg1) { 
    // TODO Auto-generated method stub 

} 

@Override 
protected void onMessage(Context context, Intent intent) { 
    // TODO Auto-generated method stub 


} 

@Override 
protected void onRegistered(Context context, String registrationId) { 
    // TODO Auto-generated method stub 
     Log.i(TAG, "Device registered: regId = " + registrationId); 
    GcmServer.register(context, registrationId); 
} 

@Override 
protected void onUnregistered(Context arg0, String arg1) { 
    // TODO Auto-generated method stub 

} 
2

確保您的模擬器或設備使用Google帳戶登錄。我遇到了同樣的問題。我只是登錄設備的谷歌帳戶,和lolz,我得到了regId。