问题
The code is very simple and perfectly works on API >= 3.0, but if I started at API 2.3 I'm getting the problem - Google send only registration events:
protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
String messageType = gcm.getMessageType(intent);
if (!extras.isEmpty()) {
if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
//sendNotification("Send error: " + extras.toString());
} else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) {
//sendNotification("Deleted messages on server: " + extras.toString());
// If it's a regular GCM message, do some work.
} else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
// we are here if API >= 3.0
}
else if (isRegistrationIntent(intent)) {
// WE ARE HERE IF API 2.3 <----- BUG
}
}
// Release the wake lock provided by the WakefulBroadcastReceiver.
GcmBroadcastReceiver.completeWakefulIntent(intent);
}
Registration is successful. But after that, my Sony Хperia (API 2.3) get only the registration's events: from another device sent GCM-message, but Xperia receive a notification of registration. Magic; (
Does anyone know the possible causes?
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.fenix14"
android:versionCode="2"
android:versionName="DVA" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<!-- GCM settings -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<permission android:name="com.fenix14.utils.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.fenix14.utils.permission.C2D_MESSAGE" />
<!-- /GCM settings -->
<application
android:allowBackup="true"
android:name="com.fenix14.utils.FenixApp"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/FenixActionBarTheme" >
<!-- GCM settings -->
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<receiver
android:name="com.fenix14.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.fenix14.gcm" />
</intent-filter>
</receiver>
<service android:name="com.fenix14.gcm.GcmIntentService" />
<!-- /GCM settings -->
<activity
android:name="com.fenix14.entry.MainActivity"
android:theme="@style/FenixActionBar.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
回答1:
Your manifest has many inconsistencies :
package="com.fenix14"
<permission android:name="com.fenix14.utils.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.fenix14.utils.permission.C2D_MESSAGE" />
<receiver
android:name="com.fenix14.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.fenix14.gcm" />
</intent-filter>
</receiver>
You should use the same package name in all the GCM relevant places, so it should be :
package="com.fenix14"
<permission android:name="com.fenix14.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.fenix14.permission.C2D_MESSAGE" />
<receiver
android:name="com.fenix14.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.fenix14" />
</intent-filter>
</receiver>
来源:https://stackoverflow.com/questions/23105255/api-2-3-googlecloudmessaging-get-only-registration-event