Signed-off-by: Marcel Müller <marcel-mueller@gmx.de>
This commit is contained in:
Marcel Müller 2022-08-08 10:31:27 +02:00
Родитель 203208ee40
Коммит cd6d2ae2d4
3 изменённых файлов: 78 добавлений и 50 удалений

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

@ -175,17 +175,22 @@
- (void)checkForPushNotificationSubscription
{
BOOL isAppActive = [[UIApplication sharedApplication] applicationState] == UIApplicationStateActive;
// Subscribe only if both tokens have been generated and app is active (do not try to subscribe when
// the app is running in background e.g. when the app is launched due to a VoIP push notification)
if (normalPushToken && pushKitToken && isAppActive) {
// Store new Normal Push & PushKit tokens in Keychain
UICKeyChainStore *keychain = [UICKeyChainStore keyChainStoreWithService:bundleIdentifier accessGroup:groupIdentifier];
[NCSettingsController sharedInstance].ncNormalPushToken = normalPushToken;
[keychain setString:normalPushToken forKey:kNCNormalPushTokenKey];
[NCSettingsController sharedInstance].ncPushKitToken = pushKitToken;
[keychain setString:pushKitToken forKey:kNCPushKitTokenKey];
if (!normalPushToken || !pushKitToken) {
return;
}
// Store new Normal Push & PushKit tokens in Keychain
UICKeyChainStore *keychain = [UICKeyChainStore keyChainStoreWithService:bundleIdentifier accessGroup:groupIdentifier];
[NCSettingsController sharedInstance].ncNormalPushToken = normalPushToken;
[keychain setString:normalPushToken forKey:kNCNormalPushTokenKey];
[NCSettingsController sharedInstance].ncPushKitToken = pushKitToken;
[keychain setString:pushKitToken forKey:kNCPushKitTokenKey];
BOOL isAppInBackground = [[UIApplication sharedApplication] applicationState] == UIApplicationStateBackground;
// Subscribe only if both tokens have been generated and app is not running in the background (do not try to subscribe
// when the app is running in background e.g. when the app is launched due to a VoIP push notification)
if (!isAppInBackground) {
// Remove subscribed flag for all accounts
RLMRealm *realm = [RLMRealm defaultRealm];
[realm beginWriteTransaction];
@ -193,7 +198,7 @@
account.pushNotificationSubscribed = NO;
}
[realm commitWriteTransaction];
// Try to subscribe for push notifications in all accounts
for (TalkAccount *account in [[NCDatabaseManager sharedInstance] allAccounts]) {
[[NCSettingsController sharedInstance] subscribeForPushNotificationsForAccountId:account.accountId];

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

@ -85,13 +85,24 @@ NSString * const kNCUserDefaultBrowser = @"ncUserDefaultBrowser";
- (NSString *)pushTokenSHA512
{
return [self createSHA512:[self combinedPushToken]];
NSString *token = [self combinedPushToken];
if (!token) {
return nil;
}
return [self createSHA512:token];
}
- (NSString *)combinedPushToken
{
NSString *normalPushToken = [_keychain stringForKey:kNCNormalPushTokenKey];
NSString *pushKitToken = [_keychain stringForKey:kNCPushKitTokenKey];
if (!normalPushToken || !pushKitToken) {
return nil;
}
return [NSString stringWithFormat:@"%@ %@", normalPushToken, pushKitToken];
}

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

@ -537,44 +537,56 @@ NSString * const kContactSyncEnabled = @"contactSyncEnabled";
{
#if !TARGET_IPHONE_SIMULATOR
NCPushNotificationKeyPair *keyPair = [self generatePushNotificationsKeyPairForAccountId:accountId];
if (keyPair) {
[[NCAPIController sharedInstance] subscribeAccount:[[NCDatabaseManager sharedInstance] talkAccountForAccountId:accountId] withPublicKey:keyPair.publicKey toNextcloudServerWithCompletionBlock:^(NSDictionary *responseDict, NSError *error) {
if (!error) {
NSLog(@"Subscribed to NC server successfully.");
NSString *publicKey = [responseDict objectForKey:@"publicKey"];
NSString *deviceIdentifier = [responseDict objectForKey:@"deviceIdentifier"];
NSString *signature = [responseDict objectForKey:@"signature"];
RLMRealm *realm = [RLMRealm defaultRealm];
[realm beginWriteTransaction];
NSPredicate *query = [NSPredicate predicateWithFormat:@"accountId = %@", accountId];
TalkAccount *managedAccount = [TalkAccount objectsWithPredicate:query].firstObject;
managedAccount.userPublicKey = publicKey;
managedAccount.deviceIdentifier = deviceIdentifier;
managedAccount.deviceSignature = signature;
[realm commitWriteTransaction];
[[NCAPIController sharedInstance] subscribeAccount:[[NCDatabaseManager sharedInstance] talkAccountForAccountId:accountId] toPushServerWithCompletionBlock:^(NSError *error) {
if (!error) {
RLMRealm *realm = [RLMRealm defaultRealm];
[realm beginWriteTransaction];
NSPredicate *query = [NSPredicate predicateWithFormat:@"accountId = %@", accountId];
TalkAccount *managedAccount = [TalkAccount objectsWithPredicate:query].firstObject;
managedAccount.pushNotificationPublicKey = keyPair.publicKey;
managedAccount.pushNotificationSubscribed = YES;
[realm commitWriteTransaction];
[[NCKeyChainController sharedInstance] setPushNotificationPrivateKey:keyPair.privateKey forAccountId:accountId];
NSLog(@"Subscribed to Push Notification server successfully.");
} else {
NSLog(@"Error while subscribing to Push Notification server.");
}
}];
} else {
NSLog(@"Error while subscribing to NC server.");
}
}];
if (!keyPair) {
NSLog(@"Error while subscribing: Unable to generate push notifications key pair.");
return;
}
NSString *pushToken = [[NCKeyChainController sharedInstance] combinedPushToken];
if (!pushToken) {
NSLog(@"Error while subscribing: Push token is not available.");
return;
}
[[NCAPIController sharedInstance] subscribeAccount:[[NCDatabaseManager sharedInstance] talkAccountForAccountId:accountId] withPublicKey:keyPair.publicKey toNextcloudServerWithCompletionBlock:^(NSDictionary *responseDict, NSError *error) {
if (!error) {
NSLog(@"Subscribed to NC server successfully.");
NSString *publicKey = [responseDict objectForKey:@"publicKey"];
NSString *deviceIdentifier = [responseDict objectForKey:@"deviceIdentifier"];
NSString *signature = [responseDict objectForKey:@"signature"];
RLMRealm *realm = [RLMRealm defaultRealm];
[realm beginWriteTransaction];
NSPredicate *query = [NSPredicate predicateWithFormat:@"accountId = %@", accountId];
TalkAccount *managedAccount = [TalkAccount objectsWithPredicate:query].firstObject;
managedAccount.userPublicKey = publicKey;
managedAccount.deviceIdentifier = deviceIdentifier;
managedAccount.deviceSignature = signature;
[realm commitWriteTransaction];
[[NCAPIController sharedInstance] subscribeAccount:[[NCDatabaseManager sharedInstance] talkAccountForAccountId:accountId] toPushServerWithCompletionBlock:^(NSError *error) {
if (!error) {
RLMRealm *realm = [RLMRealm defaultRealm];
[realm beginWriteTransaction];
NSPredicate *query = [NSPredicate predicateWithFormat:@"accountId = %@", accountId];
TalkAccount *managedAccount = [TalkAccount objectsWithPredicate:query].firstObject;
managedAccount.pushNotificationPublicKey = keyPair.publicKey;
managedAccount.pushNotificationSubscribed = YES;
[realm commitWriteTransaction];
[[NCKeyChainController sharedInstance] setPushNotificationPrivateKey:keyPair.privateKey forAccountId:accountId];
NSLog(@"Subscribed to Push Notification server successfully.");
} else {
NSLog(@"Error while subscribing to Push Notification server.");
}
}];
} else {
NSLog(@"Error while subscribing to NC server.");
}
}];
#endif
}