Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect left-swipe in notification tray?

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?

like image 654
Rohan Avatar asked Feb 08 '23 05:02

Rohan


1 Answers

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);
like image 103
44kksharma Avatar answered Feb 10 '23 23:02

44kksharma