我終於獲得了使用GCM(Google Cloud Messaging)的應用程序。但我有一個問題。在註冊完成後的那一刻,我想獲得registrationId。除了registrationId是空的,除非我重新啓動應用程序,它會給出一個值。註冊完成後,GCM - registerID爲空
基本上我想要的是在註冊完成後取回registerId。
這是我使用的代碼:
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.xml的內容
<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>
你能告訴我們你的清單文件嗎? –