Skip to content

Commit

Permalink
fix(ios): refactor & update fallback handler notId
Browse files Browse the repository at this point in the history
- refactored didReceiveRemoteNotification
- refactored didReceiveNotificationResponse
- updated fallback handler notId to "pushplugin-handler-<time>" to be unique.

Creating a unique fallback notId ensures each notification stores and executes its own
completionHandler, preventing newer notifications from overwriting existing ones.
  • Loading branch information
erisu committed Nov 17, 2024
1 parent fcf6ca1 commit d92a516
Showing 1 changed file with 18 additions and 24 deletions.
42 changes: 18 additions & 24 deletions src/ios/PushPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -279,17 +279,16 @@ - (void)didReceiveRemoteNotification:(NSNotification *)notification {
completionHandler(result);
});
};

if (self.handlerObj == nil) {
self.handlerObj = [NSMutableDictionary dictionaryWithCapacity:2];
}

id notId = [userInfo objectForKey:@"notId"];
if (notId != nil) {
NSLog(@"[PushPlugin] notId %@", notId);
[self.handlerObj setObject:safeHandler forKey:notId];
} else {
NSLog(@"[PushPlugin] notId handler");
[self.handlerObj setObject:safeHandler forKey:@"handler"];
}
NSString *fallbackNotIdKey = [NSString stringWithFormat:@"pushplugin-handler-%f", [NSDate timeIntervalSinceReferenceDate]];
NSString *notIdKey = (notId != nil) ? notId : fallbackNotIdKey;
[self.handlerObj setObject:safeHandler forKey:notIdKey];

self.notificationMessage = userInfo;
self.isInline = NO;
[self notificationReceived];
Expand Down Expand Up @@ -382,11 +381,10 @@ - (void)didReceiveNotificationResponse:(NSNotification *)notification {
switch (applicationState) {
case UIApplicationStateActive:
{
NSLog(@"[PushPlugin] App is active. Notification message set with: %@", modifiedUserInfo);

self.isInline = NO;
self.notificationMessage = modifiedUserInfo;

NSLog(@"[PushPlugin] App is active. Notification message set with: %@", self.notificationMessage);

[self notificationReceived];
if (completionHandler) {
completionHandler();
Expand All @@ -395,18 +393,19 @@ - (void)didReceiveNotificationResponse:(NSNotification *)notification {
}
case UIApplicationStateInactive:
{
NSLog(@"[PushPlugin] App is inactive. Storing notification message for later launch with: %@", modifiedUserInfo);

self.coldstart = YES;
self.launchNotification = modifiedUserInfo;

NSLog(@"[PushPlugin] App is inactive. Storing notification message for later launch with: %@", self.launchNotification);

if (completionHandler) {
completionHandler();
}
break;
}
case UIApplicationStateBackground:
{
NSLog(@"[PushPlugin] App is in the background. Notification message set with: %@", modifiedUserInfo);

void (^safeHandler)(void) = ^{
dispatch_async(dispatch_get_main_queue(), ^{
if (completionHandler) {
Expand All @@ -415,24 +414,19 @@ - (void)didReceiveNotificationResponse:(NSNotification *)notification {
});
};

self.isInline = NO;

if (self.handlerObj == nil) {
self.handlerObj = [NSMutableDictionary dictionaryWithCapacity:2];
}

id notId = modifiedUserInfo[@"notId"];
if (notId != nil) {
NSLog(@"[PushPlugin] notId %@", notId);
[self.handlerObj setObject:safeHandler forKey:notId];
} else {
NSLog(@"[PushPlugin] notId handler");
[self.handlerObj setObject:safeHandler forKey:@"handler"];
}
NSString *fallbackNotIdKey = [NSString stringWithFormat:@"pushplugin-handler-%f", [NSDate timeIntervalSinceReferenceDate]];
NSString *notIdKey = (notId != nil) ? notId : fallbackNotIdKey;
[self.handlerObj setObject:safeHandler forKey:notIdKey];

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

NSLog(@"[PushPlugin] App is in the background. Notification message set with: %@", self.notificationMessage);
self.isInline = NO;
self.notificationMessage = modifiedUserInfo;

[self performSelectorOnMainThread:@selector(notificationReceived) withObject:self waitUntilDone:NO];
break;
Expand Down

0 comments on commit d92a516

Please sign in to comment.