I am creating notification from a intent service using startForeground(id,notification).
Random r=new Random();
int id=r.nextInt(9999);
Builder notice2=new Notification.Builder(getApplicationContext())
.setContentTitle(call.getName())
.setAutoCancel(true)
.setContentIntent(intent)
.setContentText("content")
.setSmallIcon(com.project.calltracker.R.drawable.ic_alert)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), com.project.calltracker.R.drawable.ic_logo));
startForeground(id, notice2.getNotification());
Here I have set AutoCancel(true)
But when I click on the notification it does not disappear??
I am really confused. I have tried everything for last couple hours but still no luck!
Please help!
Thanks!
Users are now allowed to swipe away all notifications, including ongoing background or persistent notifications, but those messages from apps with ongoing processes will be moved into a new, minimized shade. The new shade is called "Apps active in background" and is delineated by a solid, thin white line.
Here are some of the ways to fix it when notifications are not showing up on your Android device. Check That Do Not Disturb is not enabled. It may seem obvious, but forgetting Do Not Disturb is enabled is one of the most common causes for not receiving notifications. If this setting is on (enabled), turn it off, and they will start working again.
This feature has been removed or disabled as of Android 11 DP4 and Beta 1, and ongoing and persistent notifications can no longer be dismissed/minimized by swiping them away. Our original coverage of the change is below.
A notification cannot be snoozed nor can one access its associated app's notification settings while it is minimized. This new paradigm can help users keep track of their apps, but limit clutter in doing so.
I found the answer from a answer to my other post. Basically there can be only one foreground service. Using startForeground() to generate the notification means that as long as this service is running the notification cannot be removed.
Instead using NotificationManager.notify() simply generates the notification. Setting AutoCancel(true) for this notification makes it disappear on swipe/click.
Thanks!
You can modify your code like this, so that the Notification
will be canceled when clicked :
Random r=new Random();
int id=r.nextInt(9999);
Builder notice2=new Notification.Builder(getApplicationContext())
.setContentTitle(call.getName())
.setAutoCancel(true)
.setContentIntent((PendingIntent.getActivity(getApplicationContext(), id, intent, PendingIntent.FLAG_CANCEL_CURRENT))
.setContentText("content")
.setSmallIcon(com.project.calltracker.R.drawable.ic_alert)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), com.project.calltracker.R.drawable.ic_logo));
startForeground(id, notice2.getNotification());
Instead of using simple plain Intent
, i have used PendingIntent
with appropriate Flag setup for canceling the current Notification
.
Here are some informative links regarding PendingIntent
:
I hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With