From e37596f0f7cf6bafc97d59078400f0d6ff368deb Mon Sep 17 00:00:00 2001 From: Artal Druk Date: Thu, 13 Feb 2020 12:20:10 +0200 Subject: [PATCH] fix issue with UNUserNotificationCenterDelegate and the multicast delegate. the system completion handler needs to be called after all tasks are completed, otherwise they might be terminated. --- lib/ios/RNNotificationCenterMulticast.m | 49 +++++++++++++++++++++---- 1 file changed, 41 insertions(+), 8 deletions(-) diff --git a/lib/ios/RNNotificationCenterMulticast.m b/lib/ios/RNNotificationCenterMulticast.m index c1c6add..1cf2fcb 100644 --- a/lib/ios/RNNotificationCenterMulticast.m +++ b/lib/ios/RNNotificationCenterMulticast.m @@ -22,20 +22,53 @@ - (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler { - for (id delegate in delegates) { - if ([delegate respondsToSelector:@selector(userNotificationCenter:willPresentNotification:withCompletionHandler:)]) { - [delegate userNotificationCenter:center willPresentNotification:notification withCompletionHandler:completionHandler]; + dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ + dispatch_group_t completionGroup = dispatch_group_create(); + + __block UNNotificationPresentationOptions allOptions = UNNotificationPresentationOptionNone; + void (^myCompletion)(UNNotificationPresentationOptions); + myCompletion = ^(UNNotificationPresentationOptions options){ + allOptions |= options; + dispatch_group_leave(completionGroup); + }; + + for (id delegate in delegates) { + if ([delegate respondsToSelector:@selector(userNotificationCenter:willPresentNotification:withCompletionHandler:)]) { + dispatch_group_enter(completionGroup); + [delegate userNotificationCenter:center willPresentNotification:notification withCompletionHandler:myCompletion]; + } } - } + + dispatch_group_wait(completionGroup, dispatch_time(DISPATCH_TIME_NOW, 30 * NSEC_PER_SEC)); + + dispatch_async(dispatch_get_main_queue(), ^{ + completionHandler(allOptions); + }); + }); } - (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)(void))completionHandler { - for (id delegate in delegates) { - if ([delegate respondsToSelector:@selector(userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:)]) { - [delegate userNotificationCenter:center didReceiveNotificationResponse:response withCompletionHandler:completionHandler]; + dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ + dispatch_group_t completionGroup = dispatch_group_create(); + + void (^myCompletion)(void) = ^{ + dispatch_group_leave(completionGroup); + }; + + for (id delegate in delegates) { + if ([delegate respondsToSelector:@selector(userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:)]) { + dispatch_group_enter(completionGroup); + [delegate userNotificationCenter:center didReceiveNotificationResponse:response withCompletionHandler:myCompletion]; + } } - } + + dispatch_group_wait(completionGroup, dispatch_time(DISPATCH_TIME_NOW, 30 * NSEC_PER_SEC)); + + dispatch_async(dispatch_get_main_queue(), ^{ + completionHandler(); + }); + }); } - (void)userNotificationCenter:(UNUserNotificationCenter *)center openSettingsForNotification:(nullable UNNotification *)notification