Naruto
Naruto

Reputation: 9634

Cancel notification in android

i have a reminder application, it triggers notification for few of the tasks, suppose if i delete or opens a notified task i want to clear that particular notification

i tried this solution

NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(NOTIFICATION_ID);

Here, always while opening the task i need to call this method with notification ID, so does it cause problem to my application?

If so, is there a way to check if the notification for a ID is exist & then clear it?

Upvotes: 0

Views: 2000

Answers (2)

Bhargav Ghodasara
Bhargav Ghodasara

Reputation: 378

try this

        NotificationManager notif = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notify = new Notification(R.drawable.ic_launcher,
                "tittle", System.currentTimeMillis());
        PendingIntent pending = PendingIntent.getActivity(
                getApplicationContext(), 0, new Intent(), 0);
        notify.setLatestEventInfo(getApplicationContext(), "subject",
                "body", pending);
        notify.flags |= Notification.FLAG_AUTO_CANCEL;
        notif.notify(0, notify);

Upvotes: 0

lakshay
lakshay

Reputation: 1028

You can clear notification by setting notificationId (used while generating the notification) Or alternatively set it as autoCancel so that when user performs the notified task , it will disappear automatically from the notification tray. You can do the same by :

NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext());
builder.setAutoCancel(true);

Upvotes: 1

Related Questions