Skip to content

Commit 30c6cc3

Browse files
committed
Allow didReceiveNotificationResponse to work for non local notifications too
1 parent 759f4fa commit 30c6cc3

File tree

1 file changed

+41
-10
lines changed

1 file changed

+41
-10
lines changed

flutter_local_notifications/ios/flutter_local_notifications/Sources/flutter_local_notifications/FlutterLocalNotificationsPlugin.m

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -811,7 +811,6 @@ - (UNCalendarNotificationTrigger *)buildUserNotificationCalendarTrigger:
811811

812812
- (UNTimeIntervalNotificationTrigger *)buildUserNotificationTimeIntervalTrigger:
813813
(id)arguments API_AVAILABLE(ios(10.0)) {
814-
815814
if ([self containsKey:REPEAT_INTERVAL_MILLISECODNS forDictionary:arguments]) {
816815
NSInteger repeatIntervalMilliseconds =
817816
[arguments[REPEAT_INTERVAL_MILLISECODNS] integerValue];
@@ -902,6 +901,39 @@ - (BOOL)containsKey:(NSString *)key forDictionary:(NSDictionary *)dictionary {
902901
return dictionary[key] != [NSNull null] && dictionary[key] != nil;
903902
}
904903

904+
- (NSString *)extractPayloadFromUserInfo:(NSDictionary *)userInfo {
905+
BOOL isFlutterNotification = [self isAFlutterLocalNotification:userInfo];
906+
NSString *payload;
907+
908+
if (isFlutterNotification) {
909+
payload = (NSString *)userInfo[PAYLOAD];
910+
} else {
911+
// For non-Flutter notifications, use the entire userInfo as payload
912+
if (userInfo != nil) {
913+
// Filter out FCM-specific keys
914+
NSMutableDictionary *filteredUserInfo = [userInfo mutableCopy];
915+
NSArray *keysToRemove = @[@"message_id", @"message_type", @"collapse_key", @"from", @"to", @"fcm_options"];
916+
917+
for (NSString *key in keysToRemove) {
918+
[filteredUserInfo removeObjectForKey:key];
919+
}
920+
921+
// Remove keys starting with "google." or "gsm."
922+
for (NSString *key in [filteredUserInfo allKeys]) {
923+
if ([key hasPrefix:@"google."] || [key hasPrefix:@"gsm."]) {
924+
[filteredUserInfo removeObjectForKey:key];
925+
}
926+
}
927+
928+
payload = [[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:filteredUserInfo options:0 error:nil] encoding:NSUTF8StringEncoding];
929+
} else {
930+
payload = nil;
931+
}
932+
}
933+
934+
return payload;
935+
}
936+
905937
#pragma mark - UNUserNotificationCenterDelegate
906938
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
907939
willPresentNotification:(UNNotification *)notification
@@ -955,8 +987,9 @@ - (NSMutableDictionary *)extractNotificationResponseDict:
955987
[[NSMutableDictionary alloc] init];
956988
NSInteger notificationId =
957989
[response.notification.request.identifier integerValue];
958-
NSString *payload =
959-
(NSString *)response.notification.request.content.userInfo[PAYLOAD];
990+
NSDictionary *userInfo = response.notification.request.content.userInfo;
991+
NSString *payload = [self extractPayloadFromUserInfo:userInfo];
992+
960993
NSNumber *notificationIdNumber = [NSNumber numberWithInteger:notificationId];
961994
notitificationResponseDict[@"notificationId"] = notificationIdNumber;
962995
notitificationResponseDict[PAYLOAD] = payload;
@@ -983,15 +1016,10 @@ - (void)userNotificationCenter:(UNUserNotificationCenter *)center
9831016
didReceiveNotificationResponse:(UNNotificationResponse *)response
9841017
withCompletionHandler:(void (^)(void))completionHandler
9851018
API_AVAILABLE(ios(10.0)) {
986-
if (![self isAFlutterLocalNotification:response.notification.request.content
987-
.userInfo]) {
988-
return;
989-
}
990-
1019+
NSDictionary *userInfo = response.notification.request.content.userInfo;
9911020
NSInteger notificationId =
9921021
[response.notification.request.identifier integerValue];
993-
NSString *payload =
994-
(NSString *)response.notification.request.content.userInfo[PAYLOAD];
1022+
NSString *payload = [self extractPayloadFromUserInfo:userInfo];
9951023

9961024
if ([response.actionIdentifier
9971025
isEqualToString:UNNotificationDefaultActionIdentifier]) {
@@ -1009,6 +1037,7 @@ - (void)userNotificationCenter:(UNUserNotificationCenter *)center
10091037
NSArray<NSString *> *foregroundActionIdentifiers =
10101038
[[NSUserDefaults standardUserDefaults]
10111039
stringArrayForKey:FOREGROUND_ACTION_IDENTIFIERS];
1040+
10121041
if ([foregroundActionIdentifiers indexOfObject:response.actionIdentifier] !=
10131042
NSNotFound) {
10141043
if (_initialized) {
@@ -1028,6 +1057,8 @@ - (void)userNotificationCenter:(UNUserNotificationCenter *)center
10281057
registerPlugins:registerPlugins];
10291058
}
10301059

1060+
completionHandler();
1061+
} else {
10311062
completionHandler();
10321063
}
10331064
}

0 commit comments

Comments
 (0)