Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ios): make sure notificationMessage is immutable #317

Merged
merged 1 commit into from
Dec 2, 2024
Merged
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
25 changes: 15 additions & 10 deletions src/ios/PushPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -266,12 +266,14 @@ - (void)didReceiveRemoteNotification:(NSNotification *)notification {
// do some convoluted logic to find out if this should be a silent push.
long silent = 0;
id aps = [userInfo objectForKey:@"aps"];

id contentAvailable = [aps objectForKey:@"content-available"];
if ([contentAvailable isKindOfClass:[NSString class]] && [contentAvailable isEqualToString:@"1"]) {
silent = 1;
} else if ([contentAvailable isKindOfClass:[NSNumber class]]) {
silent = [contentAvailable integerValue];
}

if (silent == 1) {
NSLog(@"[PushPlugin] this should be a silent push");
void (^safeHandler)(UIBackgroundFetchResult) = ^(UIBackgroundFetchResult result){
Expand All @@ -285,22 +287,23 @@ - (void)didReceiveRemoteNotification:(NSNotification *)notification {
}

// Get the notId
id notId = [userInfo objectForKey:@"notId"];
NSMutableDictionary *mutableUserInfo = [userInfo mutableCopy];
id notId = [mutableUserInfo objectForKey:@"notId"];
NSString *notIdKey = notId != nil ? [NSString stringWithFormat:@"%@", notId] : nil;

if (notIdKey == nil) {
// Create a unique notId
notIdKey = [NSString stringWithFormat:@"pushplugin-handler-%f", [NSDate timeIntervalSinceReferenceDate]];
// Add the unique notId to the userInfo. Passes to front-end payload.
[userInfo setValue:notIdKey forKey:@"notId"];
[mutableUserInfo setValue:notIdKey forKey:@"notId"];
// Store the handler for the uniquly created notId.
}

[self.handlerObj setObject:safeHandler forKey:notIdKey];

NSLog(@"[PushPlugin] Stored the completion handler for the background processing of notId %@", notIdKey);

self.notificationMessage = userInfo;
self.notificationMessage = [mutableUserInfo copy];
self.isInline = NO;
[self notificationReceived];
} else {
Expand Down Expand Up @@ -381,7 +384,7 @@ - (void)willPresentNotification:(NSNotification *)notification {
}
}

self.notificationMessage = modifiedUserInfo;
self.notificationMessage = [modifiedUserInfo copy];
self.isInline = YES;
[self notificationReceived];

Expand Down Expand Up @@ -419,7 +422,7 @@ - (void)didReceiveNotificationResponse:(NSNotification *)notification {
NSLog(@"[PushPlugin] App is active. Notification message set with: %@", modifiedUserInfo);

self.isInline = NO;
self.notificationMessage = modifiedUserInfo;
self.notificationMessage = [modifiedUserInfo copy];
[self notificationReceived];
if (completionHandler) {
completionHandler();
Expand All @@ -431,7 +434,7 @@ - (void)didReceiveNotificationResponse:(NSNotification *)notification {
NSLog(@"[PushPlugin] App is inactive. Storing notification message for later launch with: %@", modifiedUserInfo);

self.coldstart = YES;
self.launchNotification = modifiedUserInfo;
self.launchNotification = [modifiedUserInfo copy];
if (completionHandler) {
completionHandler();
}
Expand Down Expand Up @@ -470,7 +473,7 @@ - (void)didReceiveNotificationResponse:(NSNotification *)notification {
NSLog(@"[PushPlugin] Stored the completion handler for the background processing of notId %@", notIdKey);

self.isInline = NO;
self.notificationMessage = modifiedUserInfo;
self.notificationMessage = [modifiedUserInfo copy];

[self performSelectorOnMainThread:@selector(notificationReceived) withObject:self waitUntilDone:NO];
break;
Expand All @@ -483,18 +486,20 @@ - (void)notificationReceived {

if (self.notificationMessage && self.callbackId != nil)
{
NSMutableDictionary* mutableNotificationMessage = [self.notificationMessage mutableCopy];
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"];
NSNumber *applicationStateNumber = mutableNotificationMessage[@"applicationState"];
UIApplicationState applicationState = (UIApplicationState)[applicationStateNumber intValue];
if (applicationState != UIApplicationStateActive) {
[(NSMutableDictionary *) self.notificationMessage removeObjectForKey:@"actionCallback"];
[mutableNotificationMessage 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"];
[mutableNotificationMessage removeObjectForKey:@"applicationState"];
self.notificationMessage = [mutableNotificationMessage copy];

for (id key in self.notificationMessage) {
if ([key isEqualToString:@"aps"]) {
Expand Down