Skip to content

Commit d83727e

Browse files
remcoankerMaikuB
andcommitted
[flutter_local_notifications_android] Fix cancel notification with tag in action receiver (#2378)
* Cancel notification with tag in action * Google Java Format --------- Co-authored-by: Michael Bui <[email protected]> Co-authored-by: github-actions <>
1 parent ad7f5c3 commit d83727e

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

Diff for: flutter_local_notifications/android/src/main/java/com/dexterous/flutterlocalnotifications/ActionBroadcastReceiver.java

+8-2
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,14 @@ public void onReceive(Context context, Intent intent) {
5353
FlutterLocalNotificationsPlugin.extractNotificationResponseMap(intent);
5454

5555
if (intent.getBooleanExtra(FlutterLocalNotificationsPlugin.CANCEL_NOTIFICATION, false)) {
56-
NotificationManagerCompat.from(context)
57-
.cancel((int) action.get(FlutterLocalNotificationsPlugin.NOTIFICATION_ID));
56+
int notificationId = (int) action.get(FlutterLocalNotificationsPlugin.NOTIFICATION_ID);
57+
Object tag = action.get(FlutterLocalNotificationsPlugin.NOTIFICATION_TAG);
58+
59+
if (tag instanceof String) {
60+
NotificationManagerCompat.from(context).cancel((String) tag, notificationId);
61+
} else {
62+
NotificationManagerCompat.from(context).cancel(notificationId);
63+
}
5864
}
5965

6066
if (actionEventSink == null) {

Diff for: flutter_local_notifications/android/src/main/java/com/dexterous/flutterlocalnotifications/FlutterLocalNotificationsPlugin.java

+3
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ public class FlutterLocalNotificationsPlugin
121121

122122
static final String PAYLOAD = "payload";
123123
static final String NOTIFICATION_ID = "notificationId";
124+
static final String NOTIFICATION_TAG = "notificationTag";
124125
static final String CANCEL_NOTIFICATION = "cancelNotification";
125126

126127
private static final String TAG = "FLTLocalNotifPlugin";
@@ -309,6 +310,7 @@ protected static Notification createNotification(
309310

310311
actionIntent
311312
.putExtra(NOTIFICATION_ID, notificationDetails.id)
313+
.putExtra(NOTIFICATION_TAG, notificationDetails.tag)
312314
.putExtra(ACTION_ID, action.id)
313315
.putExtra(CANCEL_NOTIFICATION, action.cancelNotification)
314316
.putExtra(PAYLOAD, notificationDetails.payload);
@@ -623,6 +625,7 @@ static Map<String, Object> extractNotificationResponseMap(Intent intent) {
623625
final int notificationId = intent.getIntExtra(NOTIFICATION_ID, 0);
624626
final Map<String, Object> notificationResponseMap = new HashMap<>();
625627
notificationResponseMap.put(NOTIFICATION_ID, notificationId);
628+
notificationResponseMap.put(NOTIFICATION_TAG, intent.getStringExtra(NOTIFICATION_TAG));
626629
notificationResponseMap.put(ACTION_ID, intent.getStringExtra(ACTION_ID));
627630
notificationResponseMap.put(
628631
FlutterLocalNotificationsPlugin.PAYLOAD,

0 commit comments

Comments
 (0)