Cancel Notification When App Is Removed From Recents(memory) List
I am using a service which shows an ongoing notification. when the app gets closed normally, notification disappears. But, if the user removed it from recent list app is closing an
Solution 1:
Use the following code to cancel a Notification:
NotificationManagernotificationManager= (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(NOTIFICATION_ID);
In this code there is always the same id used for notifications. If you have different notifications that need to be cancelled and you have to save the ids that you used to create the Notification.
Solution 2:
Finally, I got solution.
I missed starting foreground service while building notification.
added these two lines and notification removed automatically when app removed from recent list / killed.
onBuildNotification():
startForeground(NOTIFICATION_ID, notification);
onRemovenotification():
stopForeground(true);
Post a Comment for "Cancel Notification When App Is Removed From Recents(memory) List"