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

Avoid double callback of fetchCompletionHandler in didReceiveRemoteNotification #108

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
54 changes: 50 additions & 4 deletions Assets/Plugins/iOS/AppboyAppDelegate.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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<NSNumber *> *__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
Expand Down