Skip to content

Commit 89ea841

Browse files
committed
Android intent: Consume selected notification
Sets an extra to Android intent which indicates that selected notification has been consumed. TAKEN FROM MaikuB#2212
1 parent 4e7698f commit 89ea841

File tree

2 files changed

+291
-215
lines changed

2 files changed

+291
-215
lines changed

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

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// UPDATES TAKEN FROM https://github.com/MaikuB/flutter_local_notifications/pull/2212
2+
13
package com.dexterous.flutterlocalnotifications;
24

35
import static android.provider.Settings.ACTION_MANAGE_APP_USE_FULL_SCREEN_INTENT;
@@ -132,7 +134,7 @@ public class FlutterLocalNotificationsPlugin
132134
private static final String CALLBACK_HANDLE = "callback_handle";
133135
private static final String DRAWABLE = "drawable";
134136
private static final String DEFAULT_ICON = "defaultIcon";
135-
private static final String SELECT_NOTIFICATION = "SELECT_NOTIFICATION";
137+
private static final String SELECT_NOTIFICATION_ACTION = "SELECT_NOTIFICATION";
136138
private static final String SELECT_FOREGROUND_NOTIFICATION_ACTION =
137139
"SELECT_FOREGROUND_NOTIFICATION";
138140
private static final String SCHEDULED_NOTIFICATIONS = "scheduled_notifications";
@@ -152,6 +154,7 @@ public class FlutterLocalNotificationsPlugin
152154
private static final String GET_NOTIFICATION_CHANNELS_METHOD = "getNotificationChannels";
153155
private static final String START_FOREGROUND_SERVICE = "startForegroundService";
154156
private static final String STOP_FOREGROUND_SERVICE = "stopForegroundService";
157+
private static final String CONSUME_SELECTED_NOTIFICATION_METHOD = "consumeSelectedNotification";
155158
private static final String PENDING_NOTIFICATION_REQUESTS_METHOD = "pendingNotificationRequests";
156159
private static final String GET_ACTIVE_NOTIFICATIONS_METHOD = "getActiveNotifications";
157160
private static final String SHOW_METHOD = "show";
@@ -208,6 +211,7 @@ public class FlutterLocalNotificationsPlugin
208211
private static final String INPUT_RESULT = "FlutterLocalNotificationsPluginInputResult";
209212
private static final String INPUT = "input";
210213
private static final String NOTIFICATION_RESPONSE_TYPE = "notificationResponseType";
214+
private static final String EXTRA_IS_SELECTED_NOTIFICATION_CONSUMED_KEY = "notificationConsumed";
211215
static String NOTIFICATION_DETAILS = "notificationDetails";
212216
static Gson gson;
213217
private MethodChannel channel;
@@ -269,7 +273,7 @@ protected static Notification createNotification(
269273
setupNotificationChannel(context, notificationChannelDetails);
270274
}
271275
Intent intent = getLaunchIntent(context);
272-
intent.setAction(SELECT_NOTIFICATION);
276+
intent.setAction(SELECT_NOTIFICATION_ACTION);
273277
intent.putExtra(NOTIFICATION_ID, notificationDetails.id);
274278
intent.putExtra(PAYLOAD, notificationDetails.payload);
275279
int flags = PendingIntent.FLAG_UPDATE_CURRENT;
@@ -644,7 +648,7 @@ static Map<String, Object> extractNotificationResponseMap(Intent intent) {
644648
notificationResponseMap.put(INPUT, remoteInput.getString(INPUT_RESULT));
645649
}
646650

647-
if (SELECT_NOTIFICATION.equals(intent.getAction())) {
651+
if (SELECT_NOTIFICATION_ACTION.equals(intent.getAction())) {
648652
notificationResponseMap.put(NOTIFICATION_RESPONSE_TYPE, 0);
649653
}
650654

@@ -1566,6 +1570,9 @@ public void fail(String message) {
15661570
case STOP_FOREGROUND_SERVICE:
15671571
stopForegroundService(result);
15681572
break;
1573+
case CONSUME_SELECTED_NOTIFICATION_METHOD:
1574+
consumeSelectedNotification(result);
1575+
break;
15691576
default:
15701577
result.notImplemented();
15711578
break;
@@ -1677,9 +1684,10 @@ private void getNotificationAppLaunchDetails(Result result) {
16771684
Intent launchIntent = mainActivity.getIntent();
16781685
notificationLaunchedApp =
16791686
launchIntent != null
1680-
&& (SELECT_NOTIFICATION.equals(launchIntent.getAction())
1681-
|| SELECT_FOREGROUND_NOTIFICATION_ACTION.equals(launchIntent.getAction()))
1682-
&& !launchedActivityFromHistory(launchIntent);
1687+
&& (SELECT_NOTIFICATION_ACTION.equals(launchIntent.getAction())
1688+
|| SELECT_FOREGROUND_NOTIFICATION_ACTION.equals(launchIntent.getAction()))
1689+
&& !launchedActivityFromHistory(launchIntent)
1690+
&& !isSelectedNotificationConsumed();
16831691
if (notificationLaunchedApp) {
16841692
notificationAppLaunchDetails.put(
16851693
"notificationResponse", extractNotificationResponseMap(launchIntent));
@@ -2018,7 +2026,7 @@ public boolean onNewIntent(Intent intent) {
20182026
}
20192027

20202028
private Boolean sendNotificationPayloadMessage(Intent intent) {
2021-
if (SELECT_NOTIFICATION.equals(intent.getAction())
2029+
if (SELECT_NOTIFICATION_ACTION.equals(intent.getAction())
20222030
|| SELECT_FOREGROUND_NOTIFICATION_ACTION.equals(intent.getAction())) {
20232031
Map<String, Object> notificationResponse = extractNotificationResponseMap(intent);
20242032
if (SELECT_FOREGROUND_NOTIFICATION_ACTION.equals(intent.getAction())) {
@@ -2366,6 +2374,33 @@ public boolean onActivityResult(int requestCode, int resultCode, @Nullable Inten
23662374
return true;
23672375
}
23682376

2377+
private void consumeSelectedNotification(Result result) {
2378+
if(mainActivity == null) {
2379+
return;
2380+
}
2381+
2382+
Intent mainActivityIntent = mainActivity.getIntent();
2383+
String action = mainActivityIntent.getAction();
2384+
if(action == null) {
2385+
return;
2386+
}
2387+
boolean isSelectNotificationAction = action.equals(SELECT_NOTIFICATION_ACTION);
2388+
boolean isSelectForegroundNotificationAction =
2389+
action.equals(SELECT_FOREGROUND_NOTIFICATION_ACTION);
2390+
if(isSelectNotificationAction || isSelectForegroundNotificationAction) {
2391+
mainActivityIntent.putExtra(EXTRA_IS_SELECTED_NOTIFICATION_CONSUMED_KEY, true);
2392+
}
2393+
result.success(null);
2394+
}
2395+
2396+
private boolean isSelectedNotificationConsumed() {
2397+
if(mainActivity == null) {
2398+
return false;
2399+
}
2400+
return mainActivity.getIntent().getBooleanExtra(EXTRA_IS_SELECTED_NOTIFICATION_CONSUMED_KEY,
2401+
false);
2402+
}
2403+
23692404
private static class PluginException extends RuntimeException {
23702405
public final String code;
23712406

0 commit comments

Comments
 (0)