From eeac77afd283816df929e3218c44cd6b5e346627 Mon Sep 17 00:00:00 2001 From: Erisu Date: Wed, 13 Nov 2024 01:30:26 +0900 Subject: [PATCH] feat(ios)!: match Android's behavior for actionCallback --- src/ios/PushPlugin.m | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/src/ios/PushPlugin.m b/src/ios/PushPlugin.m index 2b809903d..2d6917cad 100644 --- a/src/ios/PushPlugin.m +++ b/src/ios/PushPlugin.m @@ -335,13 +335,18 @@ - (void)pushPluginOnApplicationDidBecomeActive:(NSNotification *)notification { - (void)willPresentNotification:(NSNotification *)notification { NSLog(@"[PushPlugin] Notification was received while the app was in the foreground. (willPresentNotification)"); + UIApplicationState applicationState = [UIApplication sharedApplication].applicationState; + NSNumber *applicationStateNumber = @((int)applicationState); + // The original notification that comes from the CDVAppDelegate's willPresentNotification. UNNotification *originalNotification = notification.userInfo[@"notification"]; - NSDictionary *userInfo = originalNotification.request.content.userInfo; + NSDictionary *originalUserInfo = originalNotification.request.content.userInfo; + NSMutableDictionary *modifiedUserInfo = [originalUserInfo mutableCopy]; + [modifiedUserInfo setObject:applicationStateNumber forKey:@"applicationState"]; void (^completionHandler)(UNNotificationPresentationOptions) = notification.userInfo[@"completionHandler"]; - self.notificationMessage = userInfo; + self.notificationMessage = modifiedUserInfo; self.isInline = YES; [self notificationReceived]; @@ -367,11 +372,13 @@ - (void)didReceiveNotificationResponse:(NSNotification *)notification { void (^completionHandler)(void) = notification.userInfo[@"completionHandler"]; + UIApplicationState applicationState = [UIApplication sharedApplication].applicationState; + NSNumber *applicationStateNumber = @((int)applicationState); NSDictionary *originalUserInfo = response.notification.request.content.userInfo; NSMutableDictionary *modifiedUserInfo = [originalUserInfo mutableCopy]; - [modifiedUserInfo setObject:response.actionIdentifier forKey:@"actionCallback"]; + [modifiedUserInfo setObject:applicationStateNumber forKey:@"applicationState"]; - switch ([UIApplication sharedApplication].applicationState) { + switch (applicationState) { case UIApplicationStateActive: { self.isInline = NO; @@ -388,12 +395,7 @@ - (void)didReceiveNotificationResponse:(NSNotification *)notification { case UIApplicationStateInactive: { self.coldstart = YES; - - if ([notification.userInfo[@"actionIdentifier"] rangeOfString:@"UNNotificationDefaultActionIdentifier"].location == NSNotFound) { - self.launchNotification = modifiedUserInfo; - } else { - self.launchNotification = originalUserInfo; - } + self.launchNotification = modifiedUserInfo; NSLog(@"[PushPlugin] App is inactive. Storing notification message for later launch with: %@", self.launchNotification); @@ -427,7 +429,7 @@ - (void)didReceiveNotificationResponse:(NSNotification *)notification { [self.handlerObj setObject:safeHandler forKey:@"handler"]; } - self.notificationMessage = originalUserInfo; + self.notificationMessage = modifiedUserInfo; NSLog(@"[PushPlugin] App is in the background. Notification message set with: %@", self.notificationMessage); @@ -445,6 +447,16 @@ - (void)notificationReceived { NSMutableDictionary* message = [NSMutableDictionary dictionaryWithCapacity:4]; NSMutableDictionary* additionalData = [NSMutableDictionary dictionaryWithCapacity:4]; + // Remove "actionCallback" when application state is not foreground. Only applied to foreground. + NSNumber *applicationStateNumber = self.notificationMessage[@"applicationState"]; + UIApplicationState applicationState = (UIApplicationState)[applicationStateNumber intValue]; + if (applicationState != UIApplicationStateActive) { + [(NSMutableDictionary *) self.notificationMessage removeObjectForKey:@"actionCallback"]; + } + // @todo do not sent applicationState data to front for now. Figure out if we can add + // similar data to the other platforms. + [(NSMutableDictionary *) self.notificationMessage removeObjectForKey:@"applicationState"]; + for (id key in self.notificationMessage) { if ([key isEqualToString:@"aps"]) { id aps = [self.notificationMessage objectForKey:@"aps"];