From a980e9b960295bc0de8b62901abb960e907ef4dd Mon Sep 17 00:00:00 2001 From: kborodin Date: Fri, 8 Dec 2023 13:40:36 +0200 Subject: [PATCH 01/10] fixed launched from notification flag is always true --- .../FlutterLocalNotificationsPlugin.java | 33 +++++++++++++++---- 1 file changed, 26 insertions(+), 7 deletions(-) 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 a2536cf09..85951dd76 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 @@ -133,6 +133,7 @@ public class FlutterLocalNotificationsPlugin private static final String SELECT_FOREGROUND_NOTIFICATION_ACTION = "SELECT_FOREGROUND_NOTIFICATION"; private static final String SCHEDULED_NOTIFICATIONS = "scheduled_notifications"; + private static final String LAUNCHED_FROM_NOTIFICATION = "LAUNCHED_FROM_NOTIFICATION"; private static final String INITIALIZE_METHOD = "initialize"; private static final String GET_CALLBACK_HANDLE_METHOD = "getCallbackHandle"; private static final String ARE_NOTIFICATIONS_ENABLED_METHOD = "areNotificationsEnabled"; @@ -1578,24 +1579,42 @@ private void show(MethodCall call, Result result) { private void getNotificationAppLaunchDetails(Result result) { Map notificationAppLaunchDetails = new HashMap<>(); - Boolean notificationLaunchedApp = false; + boolean notificationLaunchedApp = false; if (mainActivity != null) { Intent launchIntent = mainActivity.getIntent(); + notificationLaunchedApp = - launchIntent != null - && (SELECT_NOTIFICATION.equals(launchIntent.getAction()) - || SELECT_FOREGROUND_NOTIFICATION_ACTION.equals(launchIntent.getAction())) - && !launchedActivityFromHistory(launchIntent); + isNotificationLaunchedApp(launchIntent); + Log.w("LOCAL_NOTIFICATION", "LAUNCHED_FROM_NOTIFICATION: " + + notificationLaunchedApp); + if (notificationLaunchedApp) { notificationAppLaunchDetails.put( - "notificationResponse", extractNotificationResponseMap(launchIntent)); + "notificationResponse", extractNotificationResponseMap(launchIntent)); + launchIntent.putExtra(LAUNCHED_FROM_NOTIFICATION, true); + mainActivity.setIntent(launchIntent); } - } + } notificationAppLaunchDetails.put(NOTIFICATION_LAUNCHED_APP, notificationLaunchedApp); result.success(notificationAppLaunchDetails); } + private boolean isNotificationLaunchedApp(Intent launchIntent) { + return launchIntent != null + && (SELECT_NOTIFICATION.equals(launchIntent.getAction()) + || SELECT_FOREGROUND_NOTIFICATION_ACTION.equals(launchIntent.getAction())) + && !launchedActivityFromHistory(launchIntent) + && !isAlreadyLaunched(launchIntent); + } + + private Boolean isAlreadyLaunched(Intent intent) { + if(intent != null && intent.hasExtra(LAUNCHED_FROM_NOTIFICATION)) { + return intent.getBooleanExtra(LAUNCHED_FROM_NOTIFICATION, false); + } + return false; + } + private void initialize(MethodCall call, Result result) { Map arguments = call.arguments(); String defaultIcon = (String) arguments.get(DEFAULT_ICON); From f3ed24ff53dabc0ff65ac9cfed6045325476ea78 Mon Sep 17 00:00:00 2001 From: kborodin Date: Fri, 8 Dec 2023 14:38:38 +0200 Subject: [PATCH 02/10] added logs for action intent on attached activity --- .../FlutterLocalNotificationsPlugin.java | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) 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 85951dd76..7935e4a94 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 @@ -133,7 +133,6 @@ public class FlutterLocalNotificationsPlugin private static final String SELECT_FOREGROUND_NOTIFICATION_ACTION = "SELECT_FOREGROUND_NOTIFICATION"; private static final String SCHEDULED_NOTIFICATIONS = "scheduled_notifications"; - private static final String LAUNCHED_FROM_NOTIFICATION = "LAUNCHED_FROM_NOTIFICATION"; private static final String INITIALIZE_METHOD = "initialize"; private static final String GET_CALLBACK_HANDLE_METHOD = "getCallbackHandle"; private static final String ARE_NOTIFICATIONS_ENABLED_METHOD = "areNotificationsEnabled"; @@ -1360,6 +1359,9 @@ public void onAttachedToActivity(ActivityPluginBinding binding) { mainActivity = binding.getActivity(); Intent mainActivityIntent = mainActivity.getIntent(); + Log.w("LOCAL_NOTIFICATION", "onAttachedToActivity()\n" + + "selected action: " + mainActivityIntent.getAction()); + if (!launchedActivityFromHistory(mainActivityIntent)) { if (SELECT_FOREGROUND_NOTIFICATION_ACTION.equals(mainActivityIntent.getAction())) { Map notificationResponse = @@ -1591,7 +1593,6 @@ private void getNotificationAppLaunchDetails(Result result) { if (notificationLaunchedApp) { notificationAppLaunchDetails.put( "notificationResponse", extractNotificationResponseMap(launchIntent)); - launchIntent.putExtra(LAUNCHED_FROM_NOTIFICATION, true); mainActivity.setIntent(launchIntent); } @@ -1604,15 +1605,7 @@ private boolean isNotificationLaunchedApp(Intent launchIntent) { return launchIntent != null && (SELECT_NOTIFICATION.equals(launchIntent.getAction()) || SELECT_FOREGROUND_NOTIFICATION_ACTION.equals(launchIntent.getAction())) - && !launchedActivityFromHistory(launchIntent) - && !isAlreadyLaunched(launchIntent); - } - - private Boolean isAlreadyLaunched(Intent intent) { - if(intent != null && intent.hasExtra(LAUNCHED_FROM_NOTIFICATION)) { - return intent.getBooleanExtra(LAUNCHED_FROM_NOTIFICATION, false); - } - return false; + && !launchedActivityFromHistory(launchIntent); } private void initialize(MethodCall call, Result result) { @@ -2162,6 +2155,10 @@ public boolean onActivityResult(int requestCode, int resultCode, @Nullable Inten return true; } + public void resetSelectedNotification() { + mainActivity.getIntent().setAction(null); + } + private static class PluginException extends RuntimeException { public final String code; From 5b0940bc14e1e44eee9f0440a8348bef95498fee Mon Sep 17 00:00:00 2001 From: kborodin Date: Fri, 8 Dec 2023 14:43:34 +0200 Subject: [PATCH 03/10] moved logs in initialise --- .../FlutterLocalNotificationsPlugin.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) 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 7935e4a94..0ad73d730 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 @@ -1359,8 +1359,6 @@ public void onAttachedToActivity(ActivityPluginBinding binding) { mainActivity = binding.getActivity(); Intent mainActivityIntent = mainActivity.getIntent(); - Log.w("LOCAL_NOTIFICATION", "onAttachedToActivity()\n" + - "selected action: " + mainActivityIntent.getAction()); if (!launchedActivityFromHistory(mainActivityIntent)) { if (SELECT_FOREGROUND_NOTIFICATION_ACTION.equals(mainActivityIntent.getAction())) { @@ -1609,6 +1607,9 @@ private boolean isNotificationLaunchedApp(Intent launchIntent) { } private void initialize(MethodCall call, Result result) { + Log.w("LOCAL_NOTIFICATION", "initialize()\n" + + "selected action: " + mainActivity.getIntent().getAction()); + Map arguments = call.arguments(); String defaultIcon = (String) arguments.get(DEFAULT_ICON); if (!isValidDrawableResource( From cc846debd5126ed4624abd839e6f5244c6c27f50 Mon Sep 17 00:00:00 2001 From: kborodin Date: Fri, 8 Dec 2023 14:59:44 +0200 Subject: [PATCH 04/10] added `resetSelectedNotification` function, in order to control `didNotificationLaunchApp` --- .../FlutterLocalNotificationsPlugin.java | 17 +++++++++++++++-- .../platform_flutter_local_notifications.dart | 8 ++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) 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 0ad73d730..17da103a2 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 @@ -149,6 +149,7 @@ public class FlutterLocalNotificationsPlugin private static final String GET_NOTIFICATION_CHANNELS_METHOD = "getNotificationChannels"; private static final String START_FOREGROUND_SERVICE = "startForegroundService"; private static final String STOP_FOREGROUND_SERVICE = "stopForegroundService"; + private static final String RESET_SELECTED_NOTIFICATION = "resetSelectedNotification"; private static final String PENDING_NOTIFICATION_REQUESTS_METHOD = "pendingNotificationRequests"; private static final String GET_ACTIVE_NOTIFICATIONS_METHOD = "getActiveNotifications"; private static final String SHOW_METHOD = "show"; @@ -1478,6 +1479,9 @@ public void fail(String message) { case STOP_FOREGROUND_SERVICE: stopForegroundService(result); break; + case RESET_SELECTED_NOTIFICATION: + resetSelectedNotification(result); + break; default: result.notImplemented(); break; @@ -2156,8 +2160,17 @@ public boolean onActivityResult(int requestCode, int resultCode, @Nullable Inten return true; } - public void resetSelectedNotification() { - mainActivity.getIntent().setAction(null); + private void resetSelectedNotification(Result result) { + String action = mainActivity.getIntent().getAction(); + if(action == null) { + return; + } + boolean isSelectNotification = action.equals(SELECT_NOTIFICATION); + boolean isSelectForegroundNotification = action.equals(SELECT_FOREGROUND_NOTIFICATION_ACTION); + if(isSelectNotification || isSelectForegroundNotification) { + mainActivity.getIntent().setAction("android.intent.action.MAIN"); + } + result.success(null); } private static class PluginException extends RuntimeException { diff --git a/flutter_local_notifications/lib/src/platform_flutter_local_notifications.dart b/flutter_local_notifications/lib/src/platform_flutter_local_notifications.dart index 2c72cc5d6..5ad783af7 100644 --- a/flutter_local_notifications/lib/src/platform_flutter_local_notifications.dart +++ b/flutter_local_notifications/lib/src/platform_flutter_local_notifications.dart @@ -292,6 +292,14 @@ class AndroidFlutterLocalNotificationsPlugin Future stopForegroundService() => _channel.invokeMethod('stopForegroundService'); + /// Resets a selected notification + /// + /// If no selected notification present, this function does nothing. + /// + /// Could be used in cases when need to control `didNotificationLaunchApp` + Future resetSelectedNotification() => + _channel.invokeMethod('resetSelectedNotification'); + @override Future show( int id, From 210d13ff83c57b6b25eb60bca808fd95a9488b0a Mon Sep 17 00:00:00 2001 From: kborodin Date: Fri, 8 Dec 2023 15:09:19 +0200 Subject: [PATCH 05/10] removed unnecessary logs --- .../FlutterLocalNotificationsPlugin.java | 5 ----- 1 file changed, 5 deletions(-) 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 17da103a2..32e4ed980 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 @@ -1589,8 +1589,6 @@ private void getNotificationAppLaunchDetails(Result result) { notificationLaunchedApp = isNotificationLaunchedApp(launchIntent); - Log.w("LOCAL_NOTIFICATION", "LAUNCHED_FROM_NOTIFICATION: " + - notificationLaunchedApp); if (notificationLaunchedApp) { notificationAppLaunchDetails.put( @@ -1611,9 +1609,6 @@ private boolean isNotificationLaunchedApp(Intent launchIntent) { } private void initialize(MethodCall call, Result result) { - Log.w("LOCAL_NOTIFICATION", "initialize()\n" + - "selected action: " + mainActivity.getIntent().getAction()); - Map arguments = call.arguments(); String defaultIcon = (String) arguments.get(DEFAULT_ICON); if (!isValidDrawableResource( From c64bf7454496da39f9a4a98521a8334c373fe704 Mon Sep 17 00:00:00 2001 From: kborodin Date: Fri, 8 Dec 2023 15:19:24 +0200 Subject: [PATCH 06/10] removed unnecessary changed --- .../FlutterLocalNotificationsPlugin.java | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) 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 32e4ed980..4af44320c 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 @@ -1583,31 +1583,24 @@ private void show(MethodCall call, Result result) { private void getNotificationAppLaunchDetails(Result result) { Map notificationAppLaunchDetails = new HashMap<>(); - boolean notificationLaunchedApp = false; + Boolean notificationLaunchedApp = false; if (mainActivity != null) { Intent launchIntent = mainActivity.getIntent(); - notificationLaunchedApp = - isNotificationLaunchedApp(launchIntent); - + launchIntent != null + && (SELECT_NOTIFICATION.equals(launchIntent.getAction()) + || SELECT_FOREGROUND_NOTIFICATION_ACTION.equals(launchIntent.getAction())) + && !launchedActivityFromHistory(launchIntent); if (notificationLaunchedApp) { notificationAppLaunchDetails.put( "notificationResponse", extractNotificationResponseMap(launchIntent)); - mainActivity.setIntent(launchIntent); } - } + notificationAppLaunchDetails.put(NOTIFICATION_LAUNCHED_APP, notificationLaunchedApp); result.success(notificationAppLaunchDetails); } - private boolean isNotificationLaunchedApp(Intent launchIntent) { - return launchIntent != null - && (SELECT_NOTIFICATION.equals(launchIntent.getAction()) - || SELECT_FOREGROUND_NOTIFICATION_ACTION.equals(launchIntent.getAction())) - && !launchedActivityFromHistory(launchIntent); - } - private void initialize(MethodCall call, Result result) { Map arguments = call.arguments(); String defaultIcon = (String) arguments.get(DEFAULT_ICON); From 4fc03722a0d8e9bb61bb346a13cf0288f79ad272 Mon Sep 17 00:00:00 2001 From: kborodin Date: Tue, 9 Jan 2024 11:20:11 +0200 Subject: [PATCH 07/10] renamed consts --- .../FlutterLocalNotificationsPlugin.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) 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 4af44320c..5e18e91e2 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 @@ -129,7 +129,7 @@ public class FlutterLocalNotificationsPlugin private static final String CALLBACK_HANDLE = "callback_handle"; private static final String DRAWABLE = "drawable"; private static final String DEFAULT_ICON = "defaultIcon"; - private static final String SELECT_NOTIFICATION = "SELECT_NOTIFICATION"; + private static final String SELECT_NOTIFICATION_ACTION = "SELECT_NOTIFICATION"; private static final String SELECT_FOREGROUND_NOTIFICATION_ACTION = "SELECT_FOREGROUND_NOTIFICATION"; private static final String SCHEDULED_NOTIFICATIONS = "scheduled_notifications"; @@ -149,7 +149,7 @@ public class FlutterLocalNotificationsPlugin private static final String GET_NOTIFICATION_CHANNELS_METHOD = "getNotificationChannels"; private static final String START_FOREGROUND_SERVICE = "startForegroundService"; private static final String STOP_FOREGROUND_SERVICE = "stopForegroundService"; - private static final String RESET_SELECTED_NOTIFICATION = "resetSelectedNotification"; + private static final String RESET_SELECTED_NOTIFICATION_CHANNEL_METHOD = "resetSelectedNotification"; private static final String PENDING_NOTIFICATION_REQUESTS_METHOD = "pendingNotificationRequests"; private static final String GET_ACTIVE_NOTIFICATIONS_METHOD = "getActiveNotifications"; private static final String SHOW_METHOD = "show"; @@ -252,7 +252,7 @@ protected static Notification createNotification( setupNotificationChannel(context, notificationChannelDetails); } Intent intent = getLaunchIntent(context); - intent.setAction(SELECT_NOTIFICATION); + intent.setAction(SELECT_NOTIFICATION_ACTION); intent.putExtra(NOTIFICATION_ID, notificationDetails.id); intent.putExtra(PAYLOAD, notificationDetails.payload); int flags = PendingIntent.FLAG_UPDATE_CURRENT; @@ -625,7 +625,7 @@ static Map extractNotificationResponseMap(Intent intent) { notificationResponseMap.put(INPUT, remoteInput.getString(INPUT_RESULT)); } - if (SELECT_NOTIFICATION.equals(intent.getAction())) { + if (SELECT_NOTIFICATION_ACTION.equals(intent.getAction())) { notificationResponseMap.put(NOTIFICATION_RESPONSE_TYPE, 0); } @@ -1479,7 +1479,7 @@ public void fail(String message) { case STOP_FOREGROUND_SERVICE: stopForegroundService(result); break; - case RESET_SELECTED_NOTIFICATION: + case RESET_SELECTED_NOTIFICATION_CHANNEL_METHOD: resetSelectedNotification(result); break; default: @@ -1588,7 +1588,7 @@ private void getNotificationAppLaunchDetails(Result result) { Intent launchIntent = mainActivity.getIntent(); notificationLaunchedApp = launchIntent != null - && (SELECT_NOTIFICATION.equals(launchIntent.getAction()) + && (SELECT_NOTIFICATION_ACTION.equals(launchIntent.getAction()) || SELECT_FOREGROUND_NOTIFICATION_ACTION.equals(launchIntent.getAction())) && !launchedActivityFromHistory(launchIntent); if (notificationLaunchedApp) { @@ -1837,7 +1837,7 @@ public boolean onNewIntent(Intent intent) { } private Boolean sendNotificationPayloadMessage(Intent intent) { - if (SELECT_NOTIFICATION.equals(intent.getAction()) + if (SELECT_NOTIFICATION_ACTION.equals(intent.getAction()) || SELECT_FOREGROUND_NOTIFICATION_ACTION.equals(intent.getAction())) { Map notificationResponse = extractNotificationResponseMap(intent); if (SELECT_FOREGROUND_NOTIFICATION_ACTION.equals(intent.getAction())) { @@ -2153,7 +2153,7 @@ private void resetSelectedNotification(Result result) { if(action == null) { return; } - boolean isSelectNotification = action.equals(SELECT_NOTIFICATION); + boolean isSelectNotification = action.equals(SELECT_NOTIFICATION_ACTION); boolean isSelectForegroundNotification = action.equals(SELECT_FOREGROUND_NOTIFICATION_ACTION); if(isSelectNotification || isSelectForegroundNotification) { mainActivity.getIntent().setAction("android.intent.action.MAIN"); From 5b40a144e1cd52142f4710efff970e03830c2503 Mon Sep 17 00:00:00 2001 From: kborodin Date: Mon, 15 Jan 2024 14:53:20 +0200 Subject: [PATCH 08/10] added intent extra, renamed channel method --- .../FlutterLocalNotificationsPlugin.java | 40 +++++++++++++------ 1 file changed, 28 insertions(+), 12 deletions(-) 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 5e18e91e2..1d8900495 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 @@ -149,7 +149,7 @@ public class FlutterLocalNotificationsPlugin private static final String GET_NOTIFICATION_CHANNELS_METHOD = "getNotificationChannels"; private static final String START_FOREGROUND_SERVICE = "startForegroundService"; private static final String STOP_FOREGROUND_SERVICE = "stopForegroundService"; - private static final String RESET_SELECTED_NOTIFICATION_CHANNEL_METHOD = "resetSelectedNotification"; + private static final String CONSUME_SELECTED_NOTIFICATION_METHOD = "consumeSelectedNotification"; private static final String PENDING_NOTIFICATION_REQUESTS_METHOD = "pendingNotificationRequests"; private static final String GET_ACTIVE_NOTIFICATIONS_METHOD = "getActiveNotifications"; private static final String SHOW_METHOD = "show"; @@ -196,6 +196,7 @@ public class FlutterLocalNotificationsPlugin private static final String INPUT_RESULT = "FlutterLocalNotificationsPluginInputResult"; private static final String INPUT = "input"; private static final String NOTIFICATION_RESPONSE_TYPE = "notificationResponseType"; + private static final String EXTRA_IS_SELECTED_NOTIFICATION_CONSUMED_KEY = "notificationConsumed"; static String NOTIFICATION_DETAILS = "notificationDetails"; static Gson gson; private MethodChannel channel; @@ -1479,8 +1480,8 @@ public void fail(String message) { case STOP_FOREGROUND_SERVICE: stopForegroundService(result); break; - case RESET_SELECTED_NOTIFICATION_CHANNEL_METHOD: - resetSelectedNotification(result); + case CONSUME_SELECTED_NOTIFICATION_METHOD: + consumeSelectedNotification(result); break; default: result.notImplemented(); @@ -1590,7 +1591,8 @@ private void getNotificationAppLaunchDetails(Result result) { launchIntent != null && (SELECT_NOTIFICATION_ACTION.equals(launchIntent.getAction()) || SELECT_FOREGROUND_NOTIFICATION_ACTION.equals(launchIntent.getAction())) - && !launchedActivityFromHistory(launchIntent); + && !launchedActivityFromHistory(launchIntent) + && !isSelectedNotificationConsumed(); if (notificationLaunchedApp) { notificationAppLaunchDetails.put( "notificationResponse", extractNotificationResponseMap(launchIntent)); @@ -2148,17 +2150,31 @@ public boolean onActivityResult(int requestCode, int resultCode, @Nullable Inten return true; } - private void resetSelectedNotification(Result result) { - String action = mainActivity.getIntent().getAction(); + private void consumeSelectedNotification(Result result) { + if(mainActivity == null) { + return; + } + + Intent mainActivityIntent = mainActivity.getIntent(); + String action = mainActivityIntent.getAction(); if(action == null) { - return; + return; } - boolean isSelectNotification = action.equals(SELECT_NOTIFICATION_ACTION); - boolean isSelectForegroundNotification = action.equals(SELECT_FOREGROUND_NOTIFICATION_ACTION); - if(isSelectNotification || isSelectForegroundNotification) { - mainActivity.getIntent().setAction("android.intent.action.MAIN"); + boolean isSelectNotificationAction = action.equals(SELECT_NOTIFICATION_ACTION); + boolean isSelectForegroundNotificationAction = + action.equals(SELECT_FOREGROUND_NOTIFICATION_ACTION); + if(isSelectNotificationAction || isSelectForegroundNotificationAction) { + mainActivityIntent.putExtra(EXTRA_IS_SELECTED_NOTIFICATION_CONSUMED_KEY, true); } - result.success(null); + result.success(null); + } + + private boolean isSelectedNotificationConsumed() { + if(mainActivity == null) { + return false; + } + return mainActivity.getIntent().getBooleanExtra(EXTRA_IS_SELECTED_NOTIFICATION_CONSUMED_KEY, + false); } private static class PluginException extends RuntimeException { From f0b7b52274c6eca5589e92cb3409fa1e531feb1b Mon Sep 17 00:00:00 2001 From: kborodin Date: Mon, 15 Jan 2024 14:56:41 +0200 Subject: [PATCH 09/10] renamed `resetSelectedNotification` to `consumeSelectedNotification` --- .../lib/src/platform_flutter_local_notifications.dart | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/flutter_local_notifications/lib/src/platform_flutter_local_notifications.dart b/flutter_local_notifications/lib/src/platform_flutter_local_notifications.dart index 5ad783af7..ee91c1f85 100644 --- a/flutter_local_notifications/lib/src/platform_flutter_local_notifications.dart +++ b/flutter_local_notifications/lib/src/platform_flutter_local_notifications.dart @@ -292,13 +292,14 @@ class AndroidFlutterLocalNotificationsPlugin Future stopForegroundService() => _channel.invokeMethod('stopForegroundService'); - /// Resets a selected notification + /// Sets an extra to an Android intent which indicates that selected + /// notification has been consumed. /// /// If no selected notification present, this function does nothing. /// /// Could be used in cases when need to control `didNotificationLaunchApp` - Future resetSelectedNotification() => - _channel.invokeMethod('resetSelectedNotification'); + Future consumeSelectedNotification() => + _channel.invokeMethod('consumeSelectedNotification'); @override Future show( From 915e75bc7acee00b17799346a805b06f5b0cf5d8 Mon Sep 17 00:00:00 2001 From: github-actions <> Date: Tue, 5 Aug 2025 11:33:41 +0000 Subject: [PATCH 10/10] Google Java Format --- .../FlutterLocalNotificationsPlugin.java | 53 ++++++++++--------- 1 file changed, 27 insertions(+), 26 deletions(-) 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 c452f6901..d0ee65bac 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 @@ -1691,14 +1691,14 @@ private void getNotificationAppLaunchDetails(Result result) { if (mainActivity != null) { Intent launchIntent = mainActivity.getIntent(); notificationLaunchedApp = - launchIntent != null - && (SELECT_NOTIFICATION_ACTION.equals(launchIntent.getAction()) - || SELECT_FOREGROUND_NOTIFICATION_ACTION.equals(launchIntent.getAction())) - && !launchedActivityFromHistory(launchIntent) - && !isSelectedNotificationConsumed(); + launchIntent != null + && (SELECT_NOTIFICATION_ACTION.equals(launchIntent.getAction()) + || SELECT_FOREGROUND_NOTIFICATION_ACTION.equals(launchIntent.getAction())) + && !launchedActivityFromHistory(launchIntent) + && !isSelectedNotificationConsumed(); if (notificationLaunchedApp) { notificationAppLaunchDetails.put( - "notificationResponse", extractNotificationResponseMap(launchIntent)); + "notificationResponse", extractNotificationResponseMap(launchIntent)); } } @@ -2389,30 +2389,31 @@ public boolean onActivityResult(int requestCode, int resultCode, @Nullable Inten } private void consumeSelectedNotification(Result result) { - if(mainActivity == null) { - return; - } + if (mainActivity == null) { + return; + } - Intent mainActivityIntent = mainActivity.getIntent(); - String action = mainActivityIntent.getAction(); - if(action == null) { - return; - } - boolean isSelectNotificationAction = action.equals(SELECT_NOTIFICATION_ACTION); - boolean isSelectForegroundNotificationAction = - action.equals(SELECT_FOREGROUND_NOTIFICATION_ACTION); - if(isSelectNotificationAction || isSelectForegroundNotificationAction) { - mainActivityIntent.putExtra(EXTRA_IS_SELECTED_NOTIFICATION_CONSUMED_KEY, true); - } - result.success(null); + Intent mainActivityIntent = mainActivity.getIntent(); + String action = mainActivityIntent.getAction(); + if (action == null) { + return; + } + boolean isSelectNotificationAction = action.equals(SELECT_NOTIFICATION_ACTION); + boolean isSelectForegroundNotificationAction = + action.equals(SELECT_FOREGROUND_NOTIFICATION_ACTION); + if (isSelectNotificationAction || isSelectForegroundNotificationAction) { + mainActivityIntent.putExtra(EXTRA_IS_SELECTED_NOTIFICATION_CONSUMED_KEY, true); + } + result.success(null); } private boolean isSelectedNotificationConsumed() { - if(mainActivity == null) { - return false; - } - return mainActivity.getIntent().getBooleanExtra(EXTRA_IS_SELECTED_NOTIFICATION_CONSUMED_KEY, - false); + if (mainActivity == null) { + return false; + } + return mainActivity + .getIntent() + .getBooleanExtra(EXTRA_IS_SELECTED_NOTIFICATION_CONSUMED_KEY, false); } private static class PluginException extends RuntimeException {