Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use delete intent to perform some action on clear notification?

Tags:

android

I want to reset a variable of my service when user clears my notification: that's all!

Looking around I see that everyone suggest to add a delete intent on my notification, but intent is used to start an activity, a service o whatever while I just need a thing like this:

void onClearPressed(){
   aVariable = 0;
}

how to obtain this result?

like image 541
Phate Avatar asked Oct 23 '12 10:10

Phate


People also ask

How do you dismiss a notification?

To dismiss a notification, touch it and swipe left or right. Tap the dismiss icon to dismiss all notifications. On newer versions of Android, you can manage some notifications from the lock screen. Double-tap a notification to open the app or swipe left or right to dismiss the notification.

What is pending intent in Android notification?

In other words, PendingIntent lets us pass a future Intent to another application and allow that application to execute that Intent as if it had the same permissions as our application, whether or not our application is still around when the Intent is eventually invoked.

How do I manage multiple notifications on Android?

Set the unique id to let Notification Manager knows this is a another notification instead of same notification. If you use the same unique Id for each notification, the Notification Manager will assume that is same notification and would replace the previous notification.


2 Answers

Notifications are not managed by your app and all things like showing notifications and clearing them are actually happening in another process. You can't make another app directly execute a piece of code just because of security reasons.

The only possibility in your case is to provide a PendingIntent which just wraps around a regular Intent and will be started on behalf of your app when notification is cleared. You need to use PendingIntent for sending broadcast or starting a service and then doing what you want in the broadcast receiver or in the service. What exactly to use depends on from which application component you are showing notifications.

In case of broadcast receiver you can just create an anonymous inner class for broadcast receiver and register it dynamically before showing notification. It will look something like that:

public class NotificationHelper {
    private static final String NOTIFICATION_DELETED_ACTION = "NOTIFICATION_DELETED";

    private final BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            aVariable = 0; // Do what you want here
            unregisterReceiver(this);
        }
    };

    public void showNotification(Context ctx, String text) {
        Intent intent = new Intent(NOTIFICATION_DELETED_ACTION);
        PendingIntent pendintIntent = PendingIntent.getBroadcast(ctx, 0, intent, 0);
        registerReceiver(receiver, new IntentFilter(NOTIFICATION_DELETED_ACTION));
        Notification n = new Notification.Builder(mContext).
          setContentText(text).
          setDeleteIntent(pendintIntent).
          build();
        NotificationManager.notify(0, n);
    }
}
like image 108
Andrei Mankevich Avatar answered Oct 15 '22 02:10

Andrei Mankevich


Andrei is correct.
If you want multiple messages back such as:

  • you want to know if the message was clicked
  • you attached an action with an icon that you want to catch
  • AND you want to know if the message was canceled

you must register each of those response filters:

public void showNotification(Context ctx, String text) ()
{
    /… create intents and pending intents same format as Andrie did../
    /… you could also set up the style of your message box etc. …/

    //need to register each response filter
    registerReceiver(receiver, new IntentFilter(CLICK_ACTION));
    registerReceiver(receiver, new IntentFilter(USER_RESPONSE_ACTION));
    registerReceiver(receiver, new IntentFilter(NOTIFICATION_DELETED_ACTION));

    Notification n = new Notification.Builder(mContext)
      .setContentText(text)
      .setContentIntent(pendingIntent)                          //Click action
      .setDeleteIntent(pendingCancelIntent)                     //Cancel/Deleted action
      .addAction(R.drawable.icon, "Title", pendingActionIntent) //Response action
      .build();

    NotificationManager.notify(0, n);

}

Then you can catch the different responses with if, else statements (as Andrei did), or with a switch statement.

Note: I make this response primarily because I could not find this anywhere, and I had to figure it out on my own. (perhaps I will remember it better for that :-) Have Fun!

like image 38
Chuck Avatar answered Oct 15 '22 01:10

Chuck