31

I'm trying to show a notification that is removed when the user taps on it. I'm using the NotificationCompat class to build my notification and I call setAutoCancel(true) on my builder. This is the piece of code:

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.ic_launcher)
        .setContentTitle("title")
        .setAutoCancel(true)
        .setContentText("content");
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, mBuilder.build());

The notification is correctly added but when I tap on it nothing happens! What am I doing wrong?

stkent
  • 19,772
  • 14
  • 85
  • 111
TheModularMind
  • 2,024
  • 2
  • 22
  • 36
  • The notification just stays there? What happens when you swipe it to dismiss it? What device and API version are you running? – Joe Malin Feb 22 '13 at 21:46
  • 1
    I'm running on Galaxy Nexsus, API 4.2.2. The notification disappear if i swipe it, but if I simply tap it nothing happens. – TheModularMind Feb 22 '13 at 22:41

2 Answers2

59

Using setContentIntent should solve your problem:

.setContentIntent(PendingIntent.getActivity(this, 0, new Intent(), 0));

In your example:

NotificationCompat.Builder mBuilder= new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.notification_icon)
        .setContentTitle("title")
        .setAutoCancel(true)
        .setContentText("content")
        .setContentIntent(PendingIntent.getActivity(this, 0, new Intent(), 0));
NotificationManager notificationManager= (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, mBuilder.build());

Often you might want to direct the user to the relevant content and so might replace 'new Intent()' with something else.

I uploaded a demo to github.

snodnipper
  • 2,580
  • 1
  • 29
  • 19
  • Thanks. Though the user can still clear the notification away by swiping it. – gatopeich Nov 08 '13 at 18:45
  • I've found out that this works, but it can lead to a particular unexpected result: I also open the contentIntent seperately from the notification. This can auto cancel the sound of the notification (On moto g3 it does, on Google Pixel it doesn't). – jobbert Feb 20 '17 at 12:09
  • This also closes the notification list. Would be even better if there was a way that doesn't close it. – Benur21 May 16 '21 at 17:56
31

I know an answer has already been accepted, but I had the same problem with a different solution so I will share it here.

For me, I was using the same NotificationCompat.Builder object to create a notification which called setOngoing(true). This was for an upload progress notification which should not be removed while working.

Anyways, after the task was complete, I called setAutoCancel(true) but the notification was still not swiping away. What I had to do also was call setOngoing(false).

It seems pretty obvious now, but it might save someone else some time in the future.

Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
JMRboosties
  • 15,500
  • 20
  • 76
  • 116