From 7f847cfff26e79bbd3672505a4a5a074a0775c23 Mon Sep 17 00:00:00 2001 From: Raul Mircea Gigea <636215+pletoss@users.noreply.github.com> Date: Fri, 14 Feb 2025 20:33:02 +0100 Subject: [PATCH] Avoid double callback of fetchCompletionHandler in didReceiveRemoteNotification Use a callbackGroup to wait for all fetchCompletionHandler callbacks and aggregate result, then call original completion handler one single time --- Assets/Plugins/iOS/AppboyAppDelegate.mm | 54 +++++++++++++++++++++++-- 1 file changed, 50 insertions(+), 4 deletions(-) diff --git a/Assets/Plugins/iOS/AppboyAppDelegate.mm b/Assets/Plugins/iOS/AppboyAppDelegate.mm index 2839b702..f1a6a9ba 100644 --- a/Assets/Plugins/iOS/AppboyAppDelegate.mm +++ b/Assets/Plugins/iOS/AppboyAppDelegate.mm @@ -59,19 +59,65 @@ - (void)application:(UIApplication *)application didRegisterForRemoteNotificatio - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler { - if ([UnityAppController instancesRespondToSelector:@selector(application:didReceiveRemoteNotification:fetchCompletionHandler:)]) { - [super application:application didReceiveRemoteNotification:userInfo fetchCompletionHandler:completionHandler]; + + dispatch_group_t __block callbackGroup = dispatch_group_create(); + NSMutableArray *__block fetchResults = [NSMutableArray array]; + void (^localCompletionHandler)(UIBackgroundFetchResult) = + ^void(UIBackgroundFetchResult fetchResult) { + [fetchResults addObject:[NSNumber numberWithInt:(int)fetchResult]]; + dispatch_group_leave(callbackGroup); + }; + + + if ([UnityAppController instancesRespondToSelector:@selector(application:didReceiveRemoteNotification:fetchCompletionHandler:)]) { + dispatch_group_enter(callbackGroup); + [super application:application didReceiveRemoteNotification:userInfo fetchCompletionHandler:localCompletionHandler]; } NSLog(@"AppboyAppDelegate called from application:didReceiveRemoteNotification:fetchCompletionHandler:. UIApplicationState is %ld", (long)[[UIApplication sharedApplication] applicationState]); // Pass notification to Braze if ([self.brazeUnityPlist[BRZUnityAutomaticPushIntegrationKey] boolValue]) { - [[AppboyUnityManager sharedInstance] registerApplication:application + dispatch_group_enter(callbackGroup); + [[AppboyUnityManager sharedInstance] registerApplication:application didReceiveRemoteNotification:userInfo - fetchCompletionHandler:completionHandler]; + fetchCompletionHandler:localCompletionHandler]; } else { NSLog(@"Automatic push integration disabled. Not forwarding notification."); } + + dispatch_group_notify(callbackGroup, dispatch_get_main_queue(), ^() { + BOOL allFetchesFailed = YES; + BOOL anyFetchHasNewData = NO; + + for (NSNumber *oneResult in fetchResults) { + UIBackgroundFetchResult result = (UIBackgroundFetchResult)oneResult.intValue; + + switch (result) { + case UIBackgroundFetchResultNoData: + allFetchesFailed = NO; + break; + case UIBackgroundFetchResultNewData: + allFetchesFailed = NO; + anyFetchHasNewData = YES; + break; + case UIBackgroundFetchResultFailed: + + break; + } + } + + UIBackgroundFetchResult finalFetchResult = UIBackgroundFetchResultNoData; + + if (allFetchesFailed) { + finalFetchResult = UIBackgroundFetchResultFailed; + } else if (anyFetchHasNewData) { + finalFetchResult = UIBackgroundFetchResultNewData; + } else { + finalFetchResult = UIBackgroundFetchResultNoData; + } + + completionHandler(finalFetchResult); + }); } #pragma mark - Helpers