即使success:1
,我的Android客户端也无法接收来自GCM服务器的推送通知。
multicast_id":8703499666074543637,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1439080356936445%9bd2607ff9fd7ecd"
我遵循了 tutorial 。它遵循使用GCM的新实现,其中:
- 在应用清单中,将" com.google.android.gms.gcm.GcmReceiver"替换为GcmBroadcastReceiver,并将当前扩展IntentService的服务声明替换为>新GcmListenerService
- 从客户端代码中删除BroadcastReceiver实现
- 重构当前的IntentService服务实现以使用GcmListenerService
醇>
我使用此 link 测试我的应用以获取GCM推送通知。
这是我对BroadcastReciever和IntentService的清单的一部分:
<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" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.usc.jay.ifff" />
</intent-filter>
</receiver>
<service android:name="com.usc.jay.ifff.GCMIntentService"
android:exported="false" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
我的IntentService:
public class GCMIntentService extends GcmListenerService{
public static final int MESSAGE_NOTIFICATION_ID = 435345;
@Override
public void onMessageReceived(String from, Bundle data) {
String message = data.getString("message");
sendNotification(message);
}
private void sendNotification(String message) {
int icon = R.mipmap.ic_launcher;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
String title = this.getString(R.string.app_name);
Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.putExtra("message", message);
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(this, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
// Play default notification sound
notification.defaults |= Notification.DEFAULT_SOUND;
// Vibrate if vibrate is enabled
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(0, notification);
}