From 52fed870587b3f41f460e93574b778f33ce03eee Mon Sep 17 00:00:00 2001 From: Remco Anker Date: Thu, 18 Jul 2024 10:39:06 +0200 Subject: [PATCH] Properly cancel notification on action --- .../ActionBroadcastReceiver.java | 10 ++++++++-- .../FlutterLocalNotificationsPlugin.java | 3 +++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/flutter_local_notifications/android/src/main/java/com/dexterous/flutterlocalnotifications/ActionBroadcastReceiver.java b/flutter_local_notifications/android/src/main/java/com/dexterous/flutterlocalnotifications/ActionBroadcastReceiver.java index d9c93acd2..6083a63f0 100644 --- a/flutter_local_notifications/android/src/main/java/com/dexterous/flutterlocalnotifications/ActionBroadcastReceiver.java +++ b/flutter_local_notifications/android/src/main/java/com/dexterous/flutterlocalnotifications/ActionBroadcastReceiver.java @@ -53,8 +53,14 @@ public void onReceive(Context context, Intent intent) { FlutterLocalNotificationsPlugin.extractNotificationResponseMap(intent); if (intent.getBooleanExtra(FlutterLocalNotificationsPlugin.CANCEL_NOTIFICATION, false)) { - NotificationManagerCompat.from(context) - .cancel((int) action.get(FlutterLocalNotificationsPlugin.NOTIFICATION_ID)); + int notificationId = (int) action.get(FlutterLocalNotificationsPlugin.NOTIFICATION_ID); + Object tag = action.get(FlutterLocalNotificationsPlugin.NOTIFICATION_TAG); + + if(tag instanceof String) { + NotificationManagerCompat.from(context).cancel((String) tag, notificationId); + } else { + NotificationManagerCompat.from(context).cancel(notificationId); + } } if (actionEventSink == null) { diff --git a/flutter_local_notifications/android/src/main/java/com/dexterous/flutterlocalnotifications/FlutterLocalNotificationsPlugin.java b/flutter_local_notifications/android/src/main/java/com/dexterous/flutterlocalnotifications/FlutterLocalNotificationsPlugin.java index b0968614b..dfc37a00d 100644 --- a/flutter_local_notifications/android/src/main/java/com/dexterous/flutterlocalnotifications/FlutterLocalNotificationsPlugin.java +++ b/flutter_local_notifications/android/src/main/java/com/dexterous/flutterlocalnotifications/FlutterLocalNotificationsPlugin.java @@ -121,6 +121,7 @@ public class FlutterLocalNotificationsPlugin static final String PAYLOAD = "payload"; static final String NOTIFICATION_ID = "notificationId"; + static final String NOTIFICATION_TAG = "notificationTag"; static final String CANCEL_NOTIFICATION = "cancelNotification"; private static final String TAG = "FLTLocalNotifPlugin"; @@ -309,6 +310,7 @@ protected static Notification createNotification( actionIntent .putExtra(NOTIFICATION_ID, notificationDetails.id) + .putExtra(NOTIFICATION_TAG, notificationDetails.tag) .putExtra(ACTION_ID, action.id) .putExtra(CANCEL_NOTIFICATION, action.cancelNotification) .putExtra(PAYLOAD, notificationDetails.payload); @@ -627,6 +629,7 @@ static Map extractNotificationResponseMap(Intent intent) { final int notificationId = intent.getIntExtra(NOTIFICATION_ID, 0); final Map notificationResponseMap = new HashMap<>(); notificationResponseMap.put(NOTIFICATION_ID, notificationId); + notificationResponseMap.put(NOTIFICATION_TAG, intent.getStringExtra(NOTIFICATION_TAG)); notificationResponseMap.put(ACTION_ID, intent.getStringExtra(ACTION_ID)); notificationResponseMap.put( FlutterLocalNotificationsPlugin.PAYLOAD,