Hero Roma
Hero Roma

Reputation: 147

How to remove a notification from the status bar

I am working on building a local notification in Android. I have managed to show a local notification in the status bar but I am not able to hide it using setAutoCancel() or anything of the sort. Here's my code:

My use case is : I show a local notification indicating to the user I am doing some processing. When I finish processing I change the text on the notification

NotificationManager manager = NotificationManagerCompat.from( this );

NotificationBuilder builder = new NotificationCompat.Builder( getApplicationContext() );
builder.setOngoing( true )
       .setSmallIcon( R.drawable.X )
       .setContentTitle("Content title")
       .setPriority( NotificationCompat.PRIORITY_MAX )
       .setProgress( 100, 0, false );

Notification notification = builder.build();
notification.flags = Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR | Notification.FLAG_FOREGROUND_SERVICE;

manager.notify( 100, notification ); 

After I finish off some processing I want the notification to be able to be cleared so I do the following :

PendingIntent btPendingIntent = PendingIntent.getActivity( this, 0, new Intent(  ), 0 );

NotificationCompat.Builder mb = new NotificationCompat.Builder(this);
mb.setSmallIcon( R.drawable.ic_action_X )
        .setLargeIcon( bitmap )
        .setContentTitle("Upload complete")
        .setAutoCancel( true );
mb.setContentIntent( btPendingIntent );
notificationManager.notify( 100, mb.build() );

The notification changes the text and icon as I want but when the user taps on the notification nothing happens - the notification does not get cancelled.

Is there a way to have an action on the notification that cancels the notification but does not open any activities or anything else?

Upvotes: 1

Views: 3499

Answers (1)

tachyonflux
tachyonflux

Reputation: 20211

You can remove a notification using NotificationManager.cancel() and passing in the id that was used in the notify call.

notificationManager.cancel(100);

Upvotes: 6

Related Questions