GCM android客户端未接收消息
我正在尝试在我的android应用程序中实现GCM,几乎所有教程都不是最新的,我遵循了android GCM快速入门,但不幸的是,我无法收到消息(GcmListenerService.onMessageReceived根本没有呼叫!)。 在服务器上,当尝试发送消息时,我收到以下结果GCM android客户端未接收消息,android,push-notification,google-cloud-messaging,push,Android,Push Notification,Google Cloud Messaging,Push,我正在尝试在我的android应用程序中实现GCM,几乎所有教程都不是最新的,我遵循了android GCM快速入门,但不幸的是,我无法收到消息(GcmListenerService.onMessageReceived根本没有呼叫!)。 在服务器上,当尝试发送消息时,我收到以下结果{“multicast_id”:6757219588450825294,“success”:1,“failure”:0,“canonical_id”:0,“results”:[{“message_id”:“0:1436
{“multicast_id”:6757219588450825294,“success”:1,“failure”:0,“canonical_id”:0,“results”:[{“message_id”:“0:1436444559760181%ad103adbf9fd7ecd”}
我的代码:manifest.xml:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.qaof.mkm.endakkhabar" >
<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.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="com.example.gcm.permission.C2D_MESSAGE" />
...
<application
...
<!-
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
</intent-filter>
</receiver>
<!-- [END gcm_receiver] -->
<!-- [START gcm_listener] -->
<service
android:name="com.qaof.mkm.endakkhabar.RegistrationIntentService"
android:exported="false" >
</service>
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
<!-- [END gcm_listener] -->
<!-- [START instanceId_listener] -->
<service
android:name="com.qaof.mkm.endakkhabar.MyInstanceIDListenerService"
android:exported="false" >
<intent-filter>
<action android:name="com.google.android.gms.iid.InstanceID" />
</intent-filter>
</service>
<!-- [END instanceId_listener] -->
MyInstanceIDListenerService:
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
</intent-filter>
</receiver>
<!-- [END gcm_receiver] -->
<!-- [START gcm_listener] -->
<service
android:name="com.qaof.mkm.endakkhabar.RegistrationIntentService"
android:exported="false" >
</service>
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
<!-- [END gcm_listener] -->
<!-- [START instanceId_listener] -->
<service
android:name="com.qaof.mkm.endakkhabar.MyInstanceIDListenerService"
android:exported="false" >
<intent-filter>
<action android:name="com.google.android.gms.iid.InstanceID" />
</intent-filter>
</service>
<!-- [END instanceId_listener] -->
public class MyGcmListenerService extends GcmListenerService {
private static final String TAG = "MyGcmListenerService";
/**
* Called when message is received.
*
* @param from SenderID of the sender.
* @param data Data bundle containing message data as key/value pairs.
* For Set of keys use data.keySet().
*/
// [START receive_message]
@Override
public void onMessageReceived(String from, Bundle data) {
String message = data.getString("message");
Log.d(TAG, "From: " + from);
Log.d(TAG, "Message: " + message);
sendNotification(message);
}
// [END receive_message]
private void sendNotification(String message) {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_setting_dark)
.setContentTitle("GCM Message")
.setContentText(message)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
}
public class MyInstanceIDListenerService extends InstanceIDListenerService {
private static final String TAG = "MyInstanceIDLS";
/**
* Called if InstanceID token is updated. This may occur if the security of
* the previous token had been compromised. This call is initiated by the
* InstanceID provider.
*/
// [START refresh_token]
@Override
public void onTokenRefresh() {
// Fetch updated Instance ID token and notify our app's server of any changes (if applicable).
Intent intent = new Intent(this, RegistrationIntentService.class);
startService(intent);
}
// [END refresh_token]
}
最后,在我的主要活动中:
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
</intent-filter>
</receiver>
<!-- [END gcm_receiver] -->
<!-- [START gcm_listener] -->
<service
android:name="com.qaof.mkm.endakkhabar.RegistrationIntentService"
android:exported="false" >
</service>
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
<!-- [END gcm_listener] -->
<!-- [START instanceId_listener] -->
<service
android:name="com.qaof.mkm.endakkhabar.MyInstanceIDListenerService"
android:exported="false" >
<intent-filter>
<action android:name="com.google.android.gms.iid.InstanceID" />
</intent-filter>
</service>
<!-- [END instanceId_listener] -->
@Override
protected void onCreate(Bundle savedInstanceState) {
...
SharedPreferences sharedPreferences =
PreferenceManager.getDefaultSharedPreferences(this);
boolean sentToken = sharedPreferences
.getBoolean(QuickstartPreferences.SENT_TOKEN_TO_SERVER, false);
if(!sentToken) {
if (checkPlayServices()) {
// Start IntentService to register this application with GCM.
Intent intent = new Intent(this, RegistrationIntentService.class);
startService(intent);
}
}
else{
Intent intent_receiver = new Intent(this, MyGcmListenerService.class);
startService(intent_receiver);
}
}
private boolean checkPlayServices() {
int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (resultCode != ConnectionResult.SUCCESS) {
if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) {
GooglePlayServicesUtil.getErrorDialog(resultCode, this,
PLAY_SERVICES_RESOLUTION_REQUEST).show();
} else {
Log.i(TAG, "This device is not supported.");
finish();
}
return false;
}
return true;
}
@Override
protected void onResume() {
LocalBroadcastManager.getInstance(this).registerReceiver(mRegistrationBroadcastReceiver,
new IntentFilter(QuickstartPreferences.REGISTRATION_COMPLETE));
super.onResume();
}
@Override
protected void onPause() {
LocalBroadcastManager.getInstance(this).unregisterReceiver(mRegistrationBroadcastReceiver);
super.onPause();
}
提前感谢您。问题似乎出现在您的清单中:
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
</intent-filter>
</receiver>
<!-- [END gcm_receiver] -->
<!-- [START gcm_listener] -->
<service
android:name="com.qaof.mkm.endakkhabar.RegistrationIntentService"
android:exported="false" >
</service>
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
<!-- [END gcm_listener] -->
<!-- [START instanceId_listener] -->
<service
android:name="com.qaof.mkm.endakkhabar.MyInstanceIDListenerService"
android:exported="false" >
<intent-filter>
<action android:name="com.google.android.gms.iid.InstanceID" />
</intent-filter>
</service>
<!-- [END instanceId_listener] -->
<!-- [START gcm_listener] -->
<service
android:name="com.qaof.mkm.endakkhabar.RegistrationIntentService"
android:exported="false" >
</service>
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
<!-- [END gcm_listener] -->
意图过滤器需要位于服务元件内部,即
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
</intent-filter>
</receiver>
<!-- [END gcm_receiver] -->
<!-- [START gcm_listener] -->
<service
android:name="com.qaof.mkm.endakkhabar.RegistrationIntentService"
android:exported="false" >
</service>
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
<!-- [END gcm_listener] -->
<!-- [START instanceId_listener] -->
<service
android:name="com.qaof.mkm.endakkhabar.MyInstanceIDListenerService"
android:exported="false" >
<intent-filter>
<action android:name="com.google.android.gms.iid.InstanceID" />
</intent-filter>
</service>
<!-- [END instanceId_listener] -->
<!-- [START gcm_listener] -->
<service
android:name="com.qaof.mkm.endakkhabar.RegistrationIntentService"
android:exported="false" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
<!-- [END gcm_listener] -->