Ответ 1
Мое решение - называть его onResume()
.
@Override
protected void onResume() {
super.onResume();
// Clear all notification
NotificationManager nMgr = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nMgr.cancelAll();
}
Я посылаю несколько уведомлений на панели уведомлений, поэтому я хотел очистить все это, когда щелкнули одно из уведомлений. На данный момент я очищаю один за другим с помощью Flag. Я знаю, что notificationManager.cancelAll()
может очистить все уведомления, но где я должен поставить так, чтобы я мог запускать один раз после нажатия одного из уведомлений.
private static void generateNotification(Context context, String message) {
int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
String title = context.getString(R.string.app_name);
Intent notificationIntent = new Intent(context, MainActivity.class);
// 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(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(msgid, notification);
//notificationManager.cancelAll(); //i wan to clear all when the notification is clicked, where should i put this line?
}
Мое решение - называть его onResume()
.
@Override
protected void onResume() {
super.onResume();
// Clear all notification
NotificationManager nMgr = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nMgr.cancelAll();
}
Вы должны использовать ожидающее намерение, которое отправляет широковещательную передачу, а затем вводит приемник вещания, который отменяет все ваши уведомления. Лучше всего запомнить все идентификаторы уведомлений и удалить их один за другим.