2

Please have a look at my Pending Intent code.

        notificationIntent.putExtra("is_from_notification", true);            
        notificationIntent.putExtra("push_message_id", push_message_id);
        notificationIntent.putExtra("open_target", open_target);
        notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    PendingIntent contentIntent = PendingIntent.getActivity(context,notifyID, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        Notification noti = new NotificationCompat.Builder(context)
                .setSmallIcon(icon_small)
                .setTicker(message)
                .setLargeIcon(largeIcon)
                .setWhen(System.currentTimeMillis())
                .setContentTitle(title)
                .setContentText(message)
                .setContentIntent(contentIntent)
                .setAutoCancel(true).build();

        NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
       notificationManager.notify(notifyID, noti);

My Problem is that, where and how should I clear this pending intent. It stays as far as app is in recent tasks.

Hemant
  • 23
  • 5

2 Answers2

0

Just call notificationManager.cancel(notifyId);

Or

call notificationManager.cancelAll();

Hope , it helps.

Anish Mittal
  • 1,157
  • 12
  • 29
  • But your code for cancel notification from status bar. Are you sure it clear pending intent also? – Hemant Feb 24 '16 at 12:06
-1

To clear a pendingIntent use:

Edit:

PendingIntent.getActivity(context,notifyID, contentIntent, PendingIntent.FLAG_UPDATE_CURRENT).cancel();

Place this line somewhere in your code, where u want to cancel the pendingintent.

Note: The intent must have the same name and ID as your pendingIntent, in your case: notifyID and contentIntent

Note: A PendingIntent itself is simply a reference to a token maintained by the system describing the original data used to retrieve it. This means that, even if its owning application's process is killed, the PendingIntent itself will remain usable from other processes that have been given it. If the creating application later re-retrieves the same kind of PendingIntent (same operation, same Intent action, data, categories, and components, and same flags), it will receive a PendingIntent representing the same token if that is still valid, and can thus call cancel() to remove it.

cancel() - Cancel a currently active PendingIntent. Only the original application owning a PendingIntent can cancel it.

Should help u out

Community
  • 1
  • 1
Strider
  • 4,452
  • 3
  • 24
  • 35
  • No. it did not help. – Hemant Feb 24 '16 at 11:05
  • @Hemant This is only to push u in the right direction. But I've **editted** my answer for your use. It **should work** now! – Strider Feb 24 '16 at 11:09
  • it's not working, I put PendingIntent.getActivity(getApplicationContext(), notifyID, getIntent(), PendingIntent.FLAG_UPDATE_CURRENT).cancel(); in current activity. current activity is called from pending intent. – Hemant Feb 24 '16 at 11:45
  • That's wrong, **getIntent()** must be **contentIntent** this is very **important**, because this indicates that the `pendingIntent` named `contentIntent` should be canceled! just copy my line to your code. – Strider Feb 24 '16 at 11:47
  • **contentintent** is in GCMBaseIntentService, and i want to cancel pending intent from activity which is started by pending intent. – Hemant Feb 24 '16 at 12:01
  • Well I guess u have to change the context. But this line should cancel your pendingIntent – Strider Feb 24 '16 at 12:24