Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

catch on swipe to dismiss event

I'm using an android notification to alert the user once a service is finished (success or failure), and I want to delete local files once the process is done.

My problem is that in the event of failure - I want to let the user a "retry" option. and if he chooses not to retry and to dismiss the notification I want to delete local files saved for the process purposes (images...).

Is there a way to catch the notification's swipe-to-dismiss event?

like image 972
Dror Fichman Avatar asked Feb 03 '13 10:02

Dror Fichman


People also ask

How do I track notification clicks on Android?

Scroll down and long-press the “Settings” widget, then place it on your home screen. You'll get a list of features that the Settings shortcut can access. Tap “Notification Log.” Tap the widget and scroll through your past notifications.

How do I swipe away notifications on Android?

First, press-and-hold on the persistent notification you want to remove. Another option is to swipe the notification left or right, and then tap on the cogwheel icon shown next to it. Next, tap on the switch next to Permanent to disable it, and then press Save.

How do I turn on notification listener service?

To use NotificationListenerService, we need to create a java file which extends NotificationListenerService and implement two callback methods. Both methods have a parameter named “sbn”, which is an object of StatusBarNotification class. StatusBarNotification provides necessary information about Notifications.


2 Answers

DeleteIntent: DeleteIntent is a PendingIntent object that can be associated with a notification and gets fired when the notification gets deleted, ether by :

  • User specific action
  • User Delete all the notifications.

You can set the Pending Intent to a broadcast Receiver and then perform any action you want.

  Intent intent = new Intent(this, MyBroadcastReceiver.class);
  PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 0, intent, 0);
  Builder builder = new Notification.Builder(this):
 ..... code for your notification
  builder.setDeleteIntent(pendingIntent);

MyBroadcastReceiver

public class MyBroadcastReceiver extends BroadcastReceiver {
      @Override
      public void onReceive(Context context, Intent intent) {
             .... code to handle cancel
         }

  }
like image 168
Mr.Me Avatar answered Oct 20 '22 21:10

Mr.Me


A fully flushed out answer (with thanks to Mr. Me for the answer):

1) Create a receiver to handle the swipe-to-dismiss event:

public class NotificationDismissedReceiver extends BroadcastReceiver {
  @Override
  public void onReceive(Context context, Intent intent) {
      int notificationId = intent.getExtras().getInt("com.my.app.notificationId");
      /* Your code to handle the event here */
  }
}

2) Add an entry to your manifest:

<receiver
    android:name="com.my.app.receiver.NotificationDismissedReceiver"
    android:exported="false" >
</receiver>

3) Create the pending intent using a unique id for the pending intent (the notification id is used here) as without this the same extras will be reused for each dismissal event:

private PendingIntent createOnDismissedIntent(Context context, int notificationId) {
    Intent intent = new Intent(context, NotificationDismissedReceiver.class);
    intent.putExtra("com.my.app.notificationId", notificationId);

    PendingIntent pendingIntent =
           PendingIntent.getBroadcast(context.getApplicationContext(), 
                                      notificationId, intent, 0);
    return pendingIntent;
}

4) Build your notification:

Notification notification = new NotificationCompat.Builder(context)
              .setContentTitle("My App")
              .setContentText("hello world")
              .setWhen(notificationTime)
              .setDeleteIntent(createOnDismissedIntent(context, notificationId))
              .build();

NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(notificationId, notification);
like image 90
Chris Knight Avatar answered Oct 20 '22 19:10

Chris Knight