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 56cd787b2..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 @@ -132,7 +132,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"; @@ -152,6 +152,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 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"; @@ -208,6 +209,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; @@ -269,7 +271,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; @@ -652,7 +654,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); } @@ -1419,6 +1421,7 @@ public void onAttachedToActivity(ActivityPluginBinding binding) { mainActivity = binding.getActivity(); Intent mainActivityIntent = mainActivity.getIntent(); + if (!launchedActivityFromHistory(mainActivityIntent)) { if (SELECT_FOREGROUND_NOTIFICATION_ACTION.equals(mainActivityIntent.getAction())) { Map notificationResponse = @@ -1574,6 +1577,9 @@ public void fail(String message) { case STOP_FOREGROUND_SERVICE: stopForegroundService(result); break; + case CONSUME_SELECTED_NOTIFICATION_METHOD: + consumeSelectedNotification(result); + break; default: result.notImplemented(); break; @@ -1686,9 +1692,10 @@ 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); + && !launchedActivityFromHistory(launchIntent) + && !isSelectedNotificationConsumed(); if (notificationLaunchedApp) { notificationAppLaunchDetails.put( "notificationResponse", extractNotificationResponseMap(launchIntent)); @@ -2027,7 +2034,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())) { @@ -2381,6 +2388,34 @@ public boolean onActivityResult(int requestCode, int resultCode, @Nullable Inten return true; } + private void consumeSelectedNotification(Result result) { + 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); + } + + 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 { public final String code; 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 bae3d29fc..347111f3a 100644 --- a/flutter_local_notifications/lib/src/platform_flutter_local_notifications.dart +++ b/flutter_local_notifications/lib/src/platform_flutter_local_notifications.dart @@ -349,6 +349,15 @@ class AndroidFlutterLocalNotificationsPlugin Future stopForegroundService() => _channel.invokeMethod('stopForegroundService'); + /// 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 consumeSelectedNotification() => + _channel.invokeMethod('consumeSelectedNotification'); + @override Future show( int id,