Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Notification not getting dismissed on click/swipe

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!

like image 894
user1122549 Avatar asked Feb 02 '14 13:02

user1122549


People also ask

Can I swipe away all notifications on Android?

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.

Why are notifications not showing up on my Android device?

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.

Is it possible to delete persistent notifications on Android 11?

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.

Can a notification be snoozed?

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.


2 Answers

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!

like image 69
user1122549 Avatar answered Sep 30 '22 17:09

user1122549


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 :

  1. http://developer.android.com/reference/android/app/PendingIntent.html
  2. What is an Android PendingIntent?

I hope this helps.

like image 27
Salman Khakwani Avatar answered Sep 30 '22 18:09

Salman Khakwani