我终于获得了使用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>
你能告诉我们你的清单文件吗? –