推送通知在选中时标记为已读
Push Notifications mark as read when checked
我正在使用 GCM 推送通知,如果用户在系统托盘上查找通知并单击通知以进行阅读,我希望此通知在系统托盘上标记为已读而不是关闭它,我该如何实现?任何帮助将不胜感激。
这是我的通知代码
private static void generateNotification(Context context, String message) {
int icon = R.drawable.ic_launcher_logo;
long when = System.currentTimeMillis();
pref = new PrefsHelper(context);
updateNotification = pref.getNotificationID() + 1;
pref.setNotificationID(updateNotification);
String title = context.getString(R.string.app_name);
int diff = (int)System.currentTimeMillis();
Intent notificationIntent = new Intent(context, NotificationActivity.class);
notificationIntent.setAction(message);
NotificationManagerCompat notificationManager = NotificationManagerCompat
.from(context);
PendingIntent intent = PendingIntent.getActivity(context, 100, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification.Builder bilder = new Notification.Builder(context);
bilder.setSmallIcon(R.drawable.ic_launcher_logo);
bilder.setSubText("");
bilder.setTicker(message);
bilder.setContentIntent(intent);
bilder.setContentTitle(title);
bilder.setContentText(message);
/*bilder.setOngoing(true);*/
bilder.setAutoCancel(false);
// set intent so it does not start a new activity
//notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
// notification. (context, title, message, intent);
// Play default notification sound
/*notification.defaults |= Notification.DEFAULT_SOUND;
// notification.sound = Uri.parse("android.resource://" +
// context.getPackageName() + "your_sound_file_name.mp3");
// Vibrate if vibrate is enabled
notification.defaults |= Notification.DEFAULT_VIBRATE;*/
notificationManager.notify(diff, bilder.build());
}
您可以使用 notifyID
到 update the current notification 当它被点击时 "update" 它的内容和 "mark it as read":
((NotificationManager) notificationManager).notify(
notifyID,
notifyBuilder.build()
);
这个ID是一个"unique ID",所以当你用同一个ID发送多个通知时,每个通知都会覆盖前一个。
我正在使用 GCM 推送通知,如果用户在系统托盘上查找通知并单击通知以进行阅读,我希望此通知在系统托盘上标记为已读而不是关闭它,我该如何实现?任何帮助将不胜感激。 这是我的通知代码
private static void generateNotification(Context context, String message) {
int icon = R.drawable.ic_launcher_logo;
long when = System.currentTimeMillis();
pref = new PrefsHelper(context);
updateNotification = pref.getNotificationID() + 1;
pref.setNotificationID(updateNotification);
String title = context.getString(R.string.app_name);
int diff = (int)System.currentTimeMillis();
Intent notificationIntent = new Intent(context, NotificationActivity.class);
notificationIntent.setAction(message);
NotificationManagerCompat notificationManager = NotificationManagerCompat
.from(context);
PendingIntent intent = PendingIntent.getActivity(context, 100, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification.Builder bilder = new Notification.Builder(context);
bilder.setSmallIcon(R.drawable.ic_launcher_logo);
bilder.setSubText("");
bilder.setTicker(message);
bilder.setContentIntent(intent);
bilder.setContentTitle(title);
bilder.setContentText(message);
/*bilder.setOngoing(true);*/
bilder.setAutoCancel(false);
// set intent so it does not start a new activity
//notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
// notification. (context, title, message, intent);
// Play default notification sound
/*notification.defaults |= Notification.DEFAULT_SOUND;
// notification.sound = Uri.parse("android.resource://" +
// context.getPackageName() + "your_sound_file_name.mp3");
// Vibrate if vibrate is enabled
notification.defaults |= Notification.DEFAULT_VIBRATE;*/
notificationManager.notify(diff, bilder.build());
}
您可以使用 notifyID
到 update the current notification 当它被点击时 "update" 它的内容和 "mark it as read":
((NotificationManager) notificationManager).notify(
notifyID,
notifyBuilder.build()
);
这个ID是一个"unique ID",所以当你用同一个ID发送多个通知时,每个通知都会覆盖前一个。