Add background push proxy subscription

Signed-off-by: Marcel Müller <marcel-mueller@gmx.de>
This commit is contained in:
Marcel Müller 2022-10-13 11:10:11 +02:00
Родитель e6f90b28f8
Коммит 950355b29d
2 изменённых файлов: 43 добавлений и 4 удалений

Просмотреть файл

@ -372,6 +372,8 @@
- (void)performBackgroundFetchWithCompletionHandler:(void (^)(BOOL errorOccurred))completionHandler
{
dispatch_group_t notificationsGroup = dispatch_group_create();
__block BOOL errorOccurred = NO;
__block BOOL expired = NO;
BGTaskHelper *bgTask = [BGTaskHelper startBackgroundTaskWithName:@"NCBackgroundFetch" expirationHandler:^(BGTaskHelper *task) {
@ -387,15 +389,48 @@
[NCUtils log:@"Start performBackgroundFetchWithCompletionHandler"];
dispatch_group_enter(notificationsGroup);
[[NCRoomsManager sharedInstance] updateRoomsAndChatsUpdatingUserStatus:NO withCompletionBlock:^(NSError *error) {
[NCUtils log:@"CompletionHandler performBackgroundFetchWithCompletionHandler"];
[NCUtils log:@"CompletionHandler updateRoomsAndChatsUpdatingUserStatus"];
if (!expired) {
completionHandler(error != nil);
if (error) {
errorOccurred = YES;
}
[bgTask stopBackgroundTask];
dispatch_group_leave(notificationsGroup);
}];
NSDateComponents *dayComponent = [[NSDateComponents alloc] init];
dayComponent.day = -1;
NSDate *thresholdDate = [[NSCalendar currentCalendar] dateByAddingComponents:dayComponent toDate:[NSDate date] options:0];
NSInteger thresholdTimestamp = [thresholdDate timeIntervalSince1970];
// Push proxy should be subscrided atleast every 24h
// Check if we reached the threshold and start the subscription process
for (TalkAccount *account in [[NCDatabaseManager sharedInstance] allAccounts]) {
if (account.lastPushSubscription < thresholdTimestamp) {
dispatch_group_enter(notificationsGroup);
[[NCSettingsController sharedInstance] subscribeForPushNotificationsForAccountId:account.accountId withCompletionBlock:^(BOOL success) {
if (!success) {
errorOccurred = YES;
}
dispatch_group_leave(notificationsGroup);
}];
}
}
dispatch_group_notify(notificationsGroup, dispatch_get_main_queue(), ^{
[NCUtils log:@"CompletionHandler performBackgroundFetchWithCompletionHandler dispatch_group_notify"];
if (!expired) {
completionHandler(errorOccurred);
}
[bgTask stopBackgroundTask];
});
}

Просмотреть файл

@ -570,6 +570,10 @@ NSString * const kContactSyncEnabled = @"contactSyncEnabled";
[bgTask stopBackgroundTask];
}
}];
#else
if (block) {
block(YES);
}
#endif
}