android怎么保证notification不重复
1个回答
2016-09-13
展开全部
谷歌的通知提供了一个唯一的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参数变成了时间戳,编译,运行
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参数变成了时间戳,编译,运行
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询