Skip to content

Commit

Permalink
feat(ios)!: match Android's behavior for actionCallback
Browse files Browse the repository at this point in the history
  • Loading branch information
erisu committed Nov 17, 2024
1 parent f82e9f4 commit fcf6ca1
Showing 1 changed file with 23 additions and 11 deletions.
34 changes: 23 additions & 11 deletions src/ios/PushPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -336,13 +336,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];

Expand All @@ -368,11 +373,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;
Expand All @@ -389,12 +396,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);

Expand Down Expand Up @@ -428,7 +430,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);

Expand All @@ -446,6 +448,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"];
Expand Down

0 comments on commit fcf6ca1

Please sign in to comment.