5

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!

Vlad Schnakovszki
  • 8,434
  • 6
  • 80
  • 114
user1122549
  • 664
  • 1
  • 13
  • 27
  • 2
    Does this answer your question? [How to cancel a foreground service from using the notification (swipe dismiss) or clear all notifications?](https://stackoverflow.com/questions/12872204/how-to-cancel-a-foreground-service-from-using-the-notification-swipe-dismiss-o) – Stypox Sep 03 '20 at 19:23

3 Answers3

12

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!

user1122549
  • 664
  • 1
  • 13
  • 27
4

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.

Community
  • 1
  • 1
Salman Khakwani
  • 6,684
  • 7
  • 33
  • 58
  • Thanks for your suggestion! Actually I am already using a Pending Intent. Sorry it was not included in the above code. But still it did not work. I found out the real issue was bcoz I was using startForeground to generate the notification instead of NotificationManager.notify(). – user1122549 Feb 03 '14 at 16:11
2

AutoCancel does not work when service is still on foreground. To allow dismiss by swipe, the stopForeground() must be called:

startForeground(id, notice2.getNotification());
stopForeground(false); //false - do not remove generated notification
Radek
  • 165
  • 10