Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android remove notification(created by mNotifyBuilder) on click

i found many answer to this, but none help :( I have this code:

private static void generateNotification(Context context, String message) {
    int icon = R.drawable.icon;
    NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    int notifyID = 96;
    NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(context)
        .setContentTitle("MyApp")
        .setContentText(message)
        .setDefaults(Notification.DEFAULT_ALL)
        .setAutoCancel(true)
        .setSmallIcon(icon);

    Notification notification = mNotifyBuilder.build();
    notification.flags |= Notification.FLAG_AUTO_CANCEL;

    mNotificationManager.notify(notifyID, notification);
}

But if i click on notification nothing happen and it is still there. In documentation is, that i need to use:

.setAutoCancel(true)

Some one have similar problem and somebody tells him to use:

notification.flags |= Notification.FLAG_AUTO_CANCEL;

I use both, but no result :( Thank you very much for answers. :)

like image 586
user1696947 Avatar asked Apr 16 '13 08:04

user1696947


2 Answers

The user can dismiss all notification or if you set your notification to auto-cancel it is also removed once the user selects it.

You can also call the cancel() for a specific notification ID on the NotificationManager. The cancelAll() method call removes all of the notifications you previously issued.

Example:

mNotificationManager.cancel(notifyId);
like image 27
SBotirov Avatar answered Oct 17 '22 05:10

SBotirov


I think that if you don't use an Intent with your notification, the only way to dismiss its to swipe it.

Otherwise you can use an Intent to open your activity and that will actually clear the notification:

Intent showIntent = new Intent(this, YourActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, showIntent, 0);

And to add it to the notification:

NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(context)
    .setContentTitle("MyApp")
    .setContentText(message)
    .setDefaults(Notification.DEFAULT_ALL)
    .setAutoCancel(true)
    .setContentIntent(contentIntent)
    .setSmallIcon(icon);

Hope this helps!

like image 131
Aballano Avatar answered Oct 17 '22 06:10

Aballano