我的GCM客户端未收到任何类型的通知。我能够在后端注册令牌。我已经仔细检查了我的Sender id&我通过注册google api获得的api密钥。客户端从不接收任何GCM消息。我无法弄清楚背后的核心原因
public class MessageListener extends GcmListenerService {
/**
* 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().
*/
@Override
public void onMessageReceived(String from, Bundle data) {
super.onMessageReceived(from, data);
Log.d("AGCM", "Message from ::" + from + " Bundle ::" + data.toString());
}}
清单:
<application
android:name=".AGCMApplicationClass"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".GCMInitializationService"
android:exported="false" >
</service>
<!-- Meta data required for Google Play Services -->
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<!-- GCM components start -->
<receiver
android:name="com.google.android.gms.gcm.GcmReceiver"
android:exported="true"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</receiver>
<service
android:name="com.example.agcm.MessageListener"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
<service
android:name="com.example.agcm.gcm.InstanceIDChangeListenerService"
android:exported="false" >
<intent-filter>
<action android:name="com.google.android.gms.iid.InstanceID" />
</intent-filter>
</service>
<service
android:name="com.example.agcm.gcm.GCMIntentService"
android:enabled="true" >
</service>
<receiver android:name="com.example.agcm.gcmdownload.GCMAppInstallReceiver"></receiver>
<!-- GCM components end -->
</application>
获取令牌代码段:
if (GCMUtils.checkGooglePlaySevices(getApplicationContext(), mRegistrationCallbacks)) {
InstanceID instanceID = InstanceID.getInstance(context);
registrationToken = instanceID.getToken(senderID,
GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
sendRegistrationIdToBackend(registrationToken, context,urlToHit,appPackage);
}
永远不会调用消息侦听器。