Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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";
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -652,7 +654,7 @@ static Map<String, Object> 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);
}

Expand Down Expand Up @@ -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<String, Object> notificationResponse =
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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<String, Object> notificationResponse = extractNotificationResponseMap(intent);
if (SELECT_FOREGROUND_NOTIFICATION_ACTION.equals(intent.getAction())) {
Expand Down Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,15 @@ class AndroidFlutterLocalNotificationsPlugin
Future<void> 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<void> consumeSelectedNotification() =>
_channel.invokeMethod('consumeSelectedNotification');

@override
Future<void> show(
int id,
Expand Down