I am generating a notification from a class that extends FireBaseMessagingService. I need to close the notification after a prescribed time.
Asked
Active
Viewed 286 times
0

AL.
- 36,815
- 10
- 142
- 281

selva kumar
- 25
- 6
-
check out postdelayed: http://stackoverflow.com/questions/10845172/android-running-a-method-periodically-using-postdelayed-call – Jaydeep Devda Mar 15 '17 at 13:42
-
you can just create a service that runs in the background that'll timeout after five minutes and delete your notification – Tapan Kumar Patro Mar 15 '17 at 14:00
-
If you want to do that even when app is killed you can achieve it by using AlarmManager by setting alarm after required hours of triggering notification. – Mayank Pandya Mar 15 '17 at 14:05
2 Answers
0
If you stuck in trigger the close action after a prescribed time, there's enough suggestions in the comment above.
If you stuck in how to close an notification.
check this:Close android Notification
0
You should use:
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
manager.cancel(notificationId);
to clear the notification (by the notificationId
). If you want to timing this operation you can use a AlarmManager like:
AlarmManager alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, CustomAlarmReceiver.class);
alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
alarmMgr.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime() +
60 * 1000, alarmIntent);
put your clear method inside CustomAlarmReceiver class.

mcatta
- 481
- 5
- 12