Skip to content

Commit

Permalink
[flutter_local_notifications] fixed issue to cancel notification on a…
Browse files Browse the repository at this point in the history
…ndroid 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 <>
  • Loading branch information
MaikuB committed May 18, 2023
1 parent ed344d1 commit ee0c200
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 7 deletions.
4 changes: 4 additions & 0 deletions flutter_local_notifications/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1323,6 +1323,14 @@ 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<String, Object> notificationResponse =
extractNotificationResponseMap(mainActivityIntent);
processForegroundNotificationAction(mainActivityIntent, notificationResponse);
}
}
}

@Override
Expand Down Expand Up @@ -1754,6 +1762,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);
Expand All @@ -1766,11 +1775,7 @@ private Boolean sendNotificationPayloadMessage(Intent intent) {
|| SELECT_FOREGROUND_NOTIFICATION_ACTION.equals(intent.getAction())) {
Map<String, Object> 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;
Expand All @@ -1779,6 +1784,14 @@ private Boolean sendNotificationPayloadMessage(Intent intent) {
return false;
}

private void processForegroundNotificationAction(
Intent intent, Map<String, Object> 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<String, Object> arguments = call.arguments();
Expand Down
2 changes: 1 addition & 1 deletion flutter_local_notifications/example/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ subprojects {
project.evaluationDependsOn(':app')
}

task clean(type: Delete) {
tasks.register("clean", Delete) {
delete rootProject.buildDir
}
2 changes: 1 addition & 1 deletion flutter_local_notifications/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit ee0c200

Please sign in to comment.