如何解决Android的GCM通知重复消息

2025-03-23 14:32:11
推荐回答(1个)
回答1:

谷歌的通知提供了一个唯一的id参数,只要设置不同的id,就可以避免重复,方法如下:
private void showNotification(String title, Context context) {
int requestCode = (int) System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(android.content.Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_launcher,
"90上门洗车", System.currentTimeMillis());
notification.flags = Notification.FLAG_AUTO_CANCEL;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_LIGHTS;
notification.vibrate = new long[]{0, 100, 200, 300};
Intent intent = null;
if (pushType == 1) {
intent = new Intent(context, Advertisement.class);
} else if (pushType == 2) {
intent = new Intent(context, HomePage.class);
} else if (pushType == 3) {
intent = new Intent(context, OrderList.class);
}

PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
intent, 0);
notification.setLatestEventInfo(context, "90上门洗车", title, contentIntent);
notificationManager.notify(requestCode, notification);
}
在这里,把notify()里面的id参数变成了时间戳,编译,运行。