Detect left-swipe in notification tray?
Asked Answered
V

1

5

I want to detect when a user swipes left on a notification -- it can be on any notification because I will detect which notification was recently dismissed using a notification listener.

Is there a "global" gesture swipe I can listen for and only trigger my app-specific event when I detect my notification as dismissed?

Velours answered 26/12, 2015 at 21:7 Comment(2)
AFAIK, this is not possible, except perhaps via a custom ROM.Jarrad
Impossible, but, you can use notification buttonsDixil
Z
11

Try following

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);
Zwinglian answered 16/1, 2017 at 14:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.