Skip to content

Commit ee0c200

Browse files
authored
[flutter_local_notifications] fixed issue to cancel notification on android when user taps on an action that shows a UI whilst app is terminated (#2000)
* fixed issue to cancel notification on android when user taps on an action that shows a UI whilst app is terminated * Google Java Format --------- Co-authored-by: github-actions <>
1 parent ed344d1 commit ee0c200

File tree

4 files changed

+24
-7
lines changed

4 files changed

+24
-7
lines changed

flutter_local_notifications/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# [14.0.1]
2+
3+
* [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
4+
15
# [14.0.0+2]
26

37
* Bumped maximum Dart SDK constraint

flutter_local_notifications/android/src/main/java/com/dexterous/flutterlocalnotifications/FlutterLocalNotificationsPlugin.java

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1323,6 +1323,14 @@ public void onAttachedToActivity(ActivityPluginBinding binding) {
13231323
binding.addOnNewIntentListener(this);
13241324
binding.addRequestPermissionsResultListener(this);
13251325
mainActivity = binding.getActivity();
1326+
Intent mainActivityIntent = mainActivity.getIntent();
1327+
if (!launchedActivityFromHistory(mainActivityIntent)) {
1328+
if (SELECT_FOREGROUND_NOTIFICATION_ACTION.equals(mainActivityIntent.getAction())) {
1329+
Map<String, Object> notificationResponse =
1330+
extractNotificationResponseMap(mainActivityIntent);
1331+
processForegroundNotificationAction(mainActivityIntent, notificationResponse);
1332+
}
1333+
}
13261334
}
13271335

13281336
@Override
@@ -1754,6 +1762,7 @@ public boolean onRequestPermissionsResult(
17541762

17551763
@Override
17561764
public boolean onNewIntent(Intent intent) {
1765+
System.out.println("mbui: onNewIntent");
17571766
boolean res = sendNotificationPayloadMessage(intent);
17581767
if (res && mainActivity != null) {
17591768
mainActivity.setIntent(intent);
@@ -1766,11 +1775,7 @@ private Boolean sendNotificationPayloadMessage(Intent intent) {
17661775
|| SELECT_FOREGROUND_NOTIFICATION_ACTION.equals(intent.getAction())) {
17671776
Map<String, Object> notificationResponse = extractNotificationResponseMap(intent);
17681777
if (SELECT_FOREGROUND_NOTIFICATION_ACTION.equals(intent.getAction())) {
1769-
if (intent.getBooleanExtra(FlutterLocalNotificationsPlugin.CANCEL_NOTIFICATION, false)) {
1770-
NotificationManagerCompat.from(applicationContext)
1771-
.cancel(
1772-
(int) notificationResponse.get(FlutterLocalNotificationsPlugin.NOTIFICATION_ID));
1773-
}
1778+
processForegroundNotificationAction(intent, notificationResponse);
17741779
}
17751780
channel.invokeMethod("didReceiveNotificationResponse", notificationResponse);
17761781
return true;
@@ -1779,6 +1784,14 @@ private Boolean sendNotificationPayloadMessage(Intent intent) {
17791784
return false;
17801785
}
17811786

1787+
private void processForegroundNotificationAction(
1788+
Intent intent, Map<String, Object> notificationResponse) {
1789+
if (intent.getBooleanExtra(FlutterLocalNotificationsPlugin.CANCEL_NOTIFICATION, false)) {
1790+
NotificationManagerCompat.from(applicationContext)
1791+
.cancel((int) notificationResponse.get(FlutterLocalNotificationsPlugin.NOTIFICATION_ID));
1792+
}
1793+
}
1794+
17821795
private void createNotificationChannelGroup(MethodCall call, Result result) {
17831796
if (VERSION.SDK_INT >= VERSION_CODES.O) {
17841797
Map<String, Object> arguments = call.arguments();

flutter_local_notifications/example/android/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,6 @@ subprojects {
2626
project.evaluationDependsOn(':app')
2727
}
2828

29-
task clean(type: Delete) {
29+
tasks.register("clean", Delete) {
3030
delete rootProject.buildDir
3131
}

flutter_local_notifications/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: flutter_local_notifications
22
description: A cross platform plugin for displaying and scheduling local
33
notifications for Flutter applications with the ability to customise for each
44
platform.
5-
version: 14.0.0+2
5+
version: 14.0.1
66
homepage: https://github.com/MaikuB/flutter_local_notifications/tree/master/flutter_local_notifications
77
issue_tracker: https://github.com/MaikuB/flutter_local_notifications/issues
88

0 commit comments

Comments
 (0)