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.

This commit is contained in:
Artal Druk
2020-02-13 12:20:10 +02:00
parent 3fe5412641
commit e37596f0f7

View File

@@ -22,20 +22,53 @@
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
{
for (id<UNUserNotificationCenterDelegate> 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<UNUserNotificationCenterDelegate> 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<UNUserNotificationCenterDelegate> 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<UNUserNotificationCenterDelegate> 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