2

마침내 GCM (Google 클라우드 메시징)을 사용하는 앱이 생겼습니다. 그러나 나는 문제가있다. 등록이 완료된 직후에 나는 registrationId를 얻고 싶습니다. registrationId를 제외하고는 비어 있습니다. 앱을 다시 시작하지 않으면 값이 반환됩니다.GCM - 등록 완료 후 바로 등록 ID가 비어 있습니다.

기본적으로 등록이 완료되면 바로 등록 아이디를 가져옵니다.

checkNotNull(SENDER_ID, "SENDER_ID"); 

GCMRegistrar.checkDevice(this); 
GCMRegistrar.checkManifest(this); 

TextView mDisplay = (TextView) findViewById(R.id.display); 

String regId = ""; 

if (!GCMRegistrar.isRegistered(this)) 
{ 
    mDisplay.setText("registering"); 
    GCMRegistrar.register(this, SENDER_ID); 

    regId = GCMRegistrar.getRegistrationId(this); 
    String url = "localhost/Google-Cloud-Messaging-Server-Test/registration.php?regId=" + regId; 

    Log.i(TAG, "registration url: " + url); 
    HttpRequest httprequest = new HttpRequest(url); 
} 
else 
{ 
    regId = GCMRegistrar.getRegistrationId(this); 

    mDisplay.setText(regId); 
    Log.v(TAG, "Already registered"); 

    GCMRegistrar.unregister(this); 
} 

문제는 코드 후이 지점에 도달한다는 것입니다 :

regId = GCMRegistrar.getRegistrationId(this); 
String url = "localhost/Google-Cloud-Messaging-Server-Test/registration.php?regId=" + regId; 

REGID이 비어

내가 사용하고있는 코드입니다. 등록에 성공 했으므로 값이 있어야합니다. 내가 잘못하고 있니? 실제로 등록 프로세스를 진행하는 동안 registrationId를 가져올 가능성이 있습니까? 사전에

감사합니다,
마크

편집 : 올바르게 또는 모든 용도 - 허가와 수신기를 추가 가질 수 있도록

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.codeglue.google.cloud.messaging.test" 
android:versionCode="1" 
android:versionName="1.0" > 

<uses-sdk 
    android:minSdkVersion="10" 
    android:targetSdkVersion="15" /> 

<permission 
    android:name="com.codeglue.google.cloud.messaging.test.permission.C2D_MESSAGE" 
    android:protectionLevel="signature" /> 

<uses-permission android:name="com.codeglue.google.cloud.messaging.test.permission.C2D_MESSAGE" /> 
<uses-permission android:name="android.permission.GET_ACCOUNTS" /> 
<uses-permission android:name="android.permission.WAKE_LOCK" /> 
<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /> 

<application 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" > 

    <activity 
     android:name=".MainActivity" 
     android:label="@string/app_name" > 

     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 

    <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="com.codeglue.google.cloud.messaging.test" /> 
     </intent-filter> 
    </receiver> 

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

</application> 

</manifest> 
+0

매니페스트 파일을 표시 할 수 있습니까? –

답변

0

에서, manifest.xml을 확인하십시오 manifest.xml 내용 .

 <uses-permission android:name="android.permission.INTERNET"/> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 
    <uses-permission android:name="android.permission.GET_ACCOUNTS"/> 
<uses-permission android:name="android.permission.WAKE_LOCK"/> 
<permission 
    android:name="you_package_name.permission.C2D_MESSAGE" 
    android:protectionLevel="signature" /> 
<uses-permission 
    android:name="you_package_name.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="you_package_name" /> 
     </intent-filter> 
    </receiver> 
+0

앱을 다시 시작하면 regId에 값이 있기 때문에 모든 권한이 올바르게 부여됩니다. – DijkeMark

2
  1. GCMregistrar.register() 방법은 com.google.android.c2dm.intent.REGISTER은 (비동기) (Registering for GCM 살펴 걸릴 의도 구글로 전송 docs 및 GCMregistrar 출처, 특히 registerinternalRegister 방법).

  2. Google은 사용자의 com.google.android.c2dm.intent.REGISTER 의도를 처리하고 com.google.android.c2dm.intent.REGISTRATION 인 텐트를 등록 ID와 함께 응답으로 보냅니다.

  3. 다음 com.google.android.c2dm.intent.REGISTRATION 의도 (응답에) 구글에서 올 때, 브로드 캐스트 리시버는 (텐트의 유형을 처리하기 위해 내 등록되어있는)가 호출되고, 해당 등록 ID가 SharedPreferences에 저장됩니다 후. 먼저이 저장해야 -

getRegistrationId() 방법을 사용 SharedPreferences에서 ID를 얻을 수 있습니다 전에 것을 의미한다 (Google이 아닌) 장치의 SharedPreferences에서 ID를 얻을 생각 나게 해요. (그리고 ID는 com.google.android.c2dm.intent.REGISTRATION이 Google에서 온 경우에만 저장됩니다.) 정확히 정확히 어디 IDMC를 SharedPreferences에 저장 로직 배치 (내가 대답은 GCMRegistrar에 있어야한다고 생각하지 않습니다 sources)

그럼.

  1. 정확히 GCMRegistrar.register 방법 후 GCMRegistrar.getRegistrationId 메소드를 호출의 때문에 programm에에 registrationID 문자열이 비어 있습니다.register 메소드는 비 의도적으로 Google에 인 텐트를 보냅니다. 즉, getRegistrationId 메소드를 호출하는 동안 ID가있는 Google의 응답이 이미 수신되었고 ID가 이미 SharedPreferences에 저장되어 있다는 보장이 없음을 의미합니다. com.google.android.c2dm.intent.REGISTRATION 의도가 구글에서 올 때 당신이 순간을 잡으려고합니다

-GCMIntentServiceonRegistered 방법을 살펴 걸릴.

관련 문제