Skip to content

Commit b979a86

Browse files
committed
Fix crash on iOS 14 when multiple completion methods are called
1 parent f917996 commit b979a86

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

FirebaseMessaging/Sources/FIRMessagingRemoteNotificationsProxy.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,7 @@ - (void)application:(UIApplication *)application
397397
didReceiveRemoteNotification:(NSDictionary *)userInfo
398398
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
399399
[[FIRMessaging messaging] appDidReceiveMessage:userInfo];
400+
completionHandler(UIBackgroundFetchResultNoData);
400401
}
401402

402403
- (void)application:(UIApplication *)application

GoogleUtilities/AppDelegateSwizzler/GULAppDelegateSwizzler.m

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#import "GoogleUtilities/Network/Public/GoogleUtilities/GULMutableDictionary.h"
2323

2424
#import <objc/runtime.h>
25+
#import <dispatch/group.h>
2526

2627
// Implementations need to be typed before calling the implementation directly to cast the
2728
// arguments and the return types correctly. Otherwise, it will crash the app.
@@ -881,18 +882,30 @@ - (void)application:(GULApplication *)application
881882
GULRealDidReceiveRemoteNotificationWithCompletionIMP
882883
didReceiveRemoteNotificationWithCompletionIMP =
883884
[didReceiveRemoteNotificationWithCompletionIMPPointer pointerValue];
884-
885+
886+
dispatch_group_t __block callbackGroup = dispatch_group_create();
887+
UIBackgroundFetchResult __block latestFetchResult = UIBackgroundFetchResultNoData;
888+
dispatch_group_notify(callbackGroup, dispatch_get_main_queue(), ^() {
889+
completionHandler(latestFetchResult);
890+
});
885891
// Notify interceptors.
886892
[GULAppDelegateSwizzler
887893
notifyInterceptorsWithMethodSelector:methodSelector
888894
callback:^(id<GULApplicationDelegate> interceptor) {
895+
896+
dispatch_group_enter(callbackGroup);
897+
void (^localCompletionHandler)(UIBackgroundFetchResult) = ^void (UIBackgroundFetchResult fetchResult){
898+
latestFetchResult = fetchResult;
899+
dispatch_group_leave(callbackGroup);
900+
};
901+
889902
NSInvocation *invocation = [GULAppDelegateSwizzler
890903
appDelegateInvocationForSelector:methodSelector];
891904
[invocation setTarget:interceptor];
892905
[invocation setSelector:methodSelector];
893906
[invocation setArgument:(void *)(&application) atIndex:2];
894907
[invocation setArgument:(void *)(&userInfo) atIndex:3];
895-
[invocation setArgument:(void *)(&completionHandler) atIndex:4];
908+
[invocation setArgument:(void *)(&localCompletionHandler) atIndex:4];
896909
[invocation invoke];
897910
}];
898911
// Call the real implementation if the real App Delegate has any.

0 commit comments

Comments
 (0)