From 75bbf5476b2a523b74b700faddc4ed90c6c8ad3d Mon Sep 17 00:00:00 2001 From: Michael Bui <25263378+MaikuB@users.noreply.github.com> Date: Tue, 16 May 2023 19:50:58 +1000 Subject: [PATCH] fixed issue to cancel notification on android when user taps on an action that shows a UI whilst app is terminated --- flutter_local_notifications/CHANGELOG.md | 4 ++++ .../FlutterLocalNotificationsPlugin.java | 22 ++++++++++++++----- .../example/android/build.gradle | 2 +- flutter_local_notifications/pubspec.yaml | 2 +- 4 files changed, 23 insertions(+), 7 deletions(-) diff --git a/flutter_local_notifications/CHANGELOG.md b/flutter_local_notifications/CHANGELOG.md index 423a28505..60f9aab90 100644 --- a/flutter_local_notifications/CHANGELOG.md +++ b/flutter_local_notifications/CHANGELOG.md @@ -1,3 +1,7 @@ +# [14.0.1] + +* [Android] fixed issue [1991](https://github.com/MaikuB/flutter_local_notifications/issues/1991) where tapping on a notification action with `showUserInterface` set to true whilst app is terminated wouldn't dismiss/cancel notification + # [14.0.0+2] * Bumped maximum Dart SDK constraint 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 cb59175ec..5cf2c1755 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 @@ -1323,6 +1323,13 @@ public void onAttachedToActivity(ActivityPluginBinding binding) { binding.addOnNewIntentListener(this); binding.addRequestPermissionsResultListener(this); mainActivity = binding.getActivity(); + Intent mainActivityIntent = mainActivity.getIntent(); + if (!launchedActivityFromHistory(mainActivityIntent)) { + if (SELECT_FOREGROUND_NOTIFICATION_ACTION.equals(mainActivityIntent.getAction())) { + Map notificationResponse = extractNotificationResponseMap(mainActivityIntent); + processForegroundNotificationAction(mainActivityIntent, notificationResponse); + } + } } @Override @@ -1754,6 +1761,7 @@ public boolean onRequestPermissionsResult( @Override public boolean onNewIntent(Intent intent) { + System.out.println("mbui: onNewIntent"); boolean res = sendNotificationPayloadMessage(intent); if (res && mainActivity != null) { mainActivity.setIntent(intent); @@ -1766,11 +1774,7 @@ private Boolean sendNotificationPayloadMessage(Intent intent) { || SELECT_FOREGROUND_NOTIFICATION_ACTION.equals(intent.getAction())) { Map notificationResponse = extractNotificationResponseMap(intent); if (SELECT_FOREGROUND_NOTIFICATION_ACTION.equals(intent.getAction())) { - if (intent.getBooleanExtra(FlutterLocalNotificationsPlugin.CANCEL_NOTIFICATION, false)) { - NotificationManagerCompat.from(applicationContext) - .cancel( - (int) notificationResponse.get(FlutterLocalNotificationsPlugin.NOTIFICATION_ID)); - } + processForegroundNotificationAction(intent, notificationResponse); } channel.invokeMethod("didReceiveNotificationResponse", notificationResponse); return true; @@ -1779,6 +1783,14 @@ private Boolean sendNotificationPayloadMessage(Intent intent) { return false; } + private void processForegroundNotificationAction(Intent intent, Map notificationResponse) { + if (intent.getBooleanExtra(FlutterLocalNotificationsPlugin.CANCEL_NOTIFICATION, false)) { + NotificationManagerCompat.from(applicationContext) + .cancel( + (int) notificationResponse.get(FlutterLocalNotificationsPlugin.NOTIFICATION_ID)); + } + } + private void createNotificationChannelGroup(MethodCall call, Result result) { if (VERSION.SDK_INT >= VERSION_CODES.O) { Map arguments = call.arguments(); diff --git a/flutter_local_notifications/example/android/build.gradle b/flutter_local_notifications/example/android/build.gradle index 090ffc6a8..0ca70b834 100644 --- a/flutter_local_notifications/example/android/build.gradle +++ b/flutter_local_notifications/example/android/build.gradle @@ -26,6 +26,6 @@ subprojects { project.evaluationDependsOn(':app') } -task clean(type: Delete) { +tasks.register("clean", Delete) { delete rootProject.buildDir } diff --git a/flutter_local_notifications/pubspec.yaml b/flutter_local_notifications/pubspec.yaml index fdb54dead..b0d2696b3 100644 --- a/flutter_local_notifications/pubspec.yaml +++ b/flutter_local_notifications/pubspec.yaml @@ -2,7 +2,7 @@ name: flutter_local_notifications description: A cross platform plugin for displaying and scheduling local notifications for Flutter applications with the ability to customise for each platform. -version: 14.0.0+2 +version: 14.0.1 homepage: https://github.com/MaikuB/flutter_local_notifications/tree/master/flutter_local_notifications issue_tracker: https://github.com/MaikuB/flutter_local_notifications/issues