Skip to content

Commit

Permalink
Properly cancel notification on action
Browse files Browse the repository at this point in the history
  • Loading branch information
remcoanker committed Jul 18, 2024
1 parent 8d749be commit 52fed87
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -627,6 +629,7 @@ static Map<String, Object> extractNotificationResponseMap(Intent intent) {
final int notificationId = intent.getIntExtra(NOTIFICATION_ID, 0);
final Map<String, Object> 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,
Expand Down

0 comments on commit 52fed87

Please sign in to comment.