【发布时间】:2016-08-09 11:44:22
【问题描述】:
AndroidManifest.xml:
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<!-- Receiver for GCM Messages-->
<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.REGISTRATION" />
<action android:name="com.google.android.c2dm.intent.GCM_RECEIVED_ACTION"/>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="de.comp.module.client" />
</intent-filter>
</receiver>
<service
android:name="de.comp.module.client.gcm.GCMBroadcastIntentService"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
GCMBroadcastIntentService 类:
public class GCMBroadcastIntentService extends GcmListenerService {
@Override
public void onMessageReceived(String from, Bundle extras) {
if (extras.containsKey("message")) {}
}
}
永远不会调用 onMessageReceived() 方法。服务器工作正常,消息发送到谷歌 GCM 没有任何错误,但它们从未到达设备。我还使用存储在文件夹 /app 中的 google-services.json。在我的应用程序中,用户可以在新安装后登录。在这种情况下,请求一个新的 GCM 令牌并将其发送到服务器。所以它应该是正确的和最新的。
缺少什么?感谢您的帮助。
编辑:
public class GCMRegistrationIntentService extends IntentService {
@Override
protected void onHandleIntent(Intent intent) {
try {
// register server key to GCM
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
String regid = gcm.register(getString(R.string.gcm_defaultSenderId));
// Request new token for this server key
InstanceID instanceID = InstanceID.getInstance(this);
String token = instanceID.getToken(getString(R.string.gcm_defaultSenderId), GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
// Subscribe to topic channels
subscribeTopics(token);
...
}
【问题讨论】:
-
你把服务器密钥注册到Gcm了吗??
标签: android google-cloud-messaging