我在 GCMIntentService 类中有以下两个方法,但是在注册 regID 时会调用 onRegistered() 方法,但是,当我发送消息时,不会调用 onMessage()。
@Override
protected void onRegistered(Context context, String registrationId)
{
Log.i(tag, "onRegistered called");
GCMRegistrar.setRegisteredOnServer(context, true);
}
@Override
protected void onMessage(Context context, Intent intent)
{
Log.i(TAG, "Received message");
sendNotification("A", "B");
}
下面是我的清单
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.myApp.permission.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.VIBRATE" />
<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="com.myApp" />
</intent-filter>
</receiver>
<service android:name="com.myApp.GCMIntentService" />
我得到这样的注册ID:
regid = getRegistrationId(context);
//Check if we have registered.
if (regid.length() == 0) {
GCMRegistrar.checkDevice(myActivity.this);
GCMRegistrar.checkManifest(myActivity.this);
GCMRegistrar.register(myActivity.this, SENDER_ID);
} else {
Log.i(tag, "You are already registered.");
Log.i(tag, "The registration ID is : " + regid);
}