2012-09-05 3 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 계정으로 로그인되어 있는지 확인하십시오. 같은 문제가 발생했습니다. 나는 장치의 Google 계정에 간단하게 서명했고, lolz, 나는 regId를 얻었다.

관련 문제