From 50c67b3b6c0faa4dc70e3320b1eb647e8c52d1c2 Mon Sep 17 00:00:00 2001 From: David Langley Date: Fri, 26 Nov 2021 10:32:50 +0000 Subject: [PATCH 1/5] Don't allow concurrent initializations of MXBackgroundSyncService --- RiotNSE/NotificationService.swift | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/RiotNSE/NotificationService.swift b/RiotNSE/NotificationService.swift index 99a9bba12..214579e7e 100644 --- a/RiotNSE/NotificationService.swift +++ b/RiotNSE/NotificationService.swift @@ -67,6 +67,7 @@ class NotificationService: UNNotificationServiceExtension { private var pushNotificationStore: PushNotificationStore = PushNotificationStore() private let localAuthenticationService = LocalAuthenticationService(pinCodePreferences: .shared) + private let backgroundServiceInitQueue = DispatchQueue(label: "backgroundServiceInitQueue") // MARK: - Method Overrides override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) { @@ -169,14 +170,16 @@ class NotificationService: UNNotificationServiceExtension { MXKAccountManager.shared()?.forceReloadAccounts() self.userAccount = MXKAccountManager.shared()?.activeAccounts.first if let userAccount = userAccount { - if NotificationService.backgroundSyncService?.credentials != userAccount.mxCredentials { - MXLog.debug("[NotificationService] setup: MXBackgroundSyncService init: BEFORE") - self.logMemory() - NotificationService.backgroundSyncService = MXBackgroundSyncService(withCredentials: userAccount.mxCredentials) - MXLog.debug("[NotificationService] setup: MXBackgroundSyncService init: AFTER") - self.logMemory() + backgroundServiceInitQueue.sync { + if NotificationService.backgroundSyncService?.credentials != userAccount.mxCredentials { + MXLog.debug("[NotificationService] setup: MXBackgroundSyncService init: BEFORE") + self.logMemory() + NotificationService.backgroundSyncService = MXBackgroundSyncService(withCredentials: userAccount.mxCredentials) + MXLog.debug("[NotificationService] setup: MXBackgroundSyncService init: AFTER") + self.logMemory() + } + completion() } - completion() } else { MXLog.debug("[NotificationService] setup: No active accounts") fallbackToBestAttemptContent(forEventId: eventId) From 56086788900c47248e7bbf6e38fb5dae4c52e54e Mon Sep 17 00:00:00 2001 From: David Langley Date: Fri, 26 Nov 2021 12:47:53 +0000 Subject: [PATCH 2/5] Make backgroundServiceInitQueue static and follow convention for queue naming. --- RiotNSE/NotificationService.swift | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/RiotNSE/NotificationService.swift b/RiotNSE/NotificationService.swift index 214579e7e..f6125baca 100644 --- a/RiotNSE/NotificationService.swift +++ b/RiotNSE/NotificationService.swift @@ -66,8 +66,7 @@ class NotificationService: UNNotificationServiceExtension { }() private var pushNotificationStore: PushNotificationStore = PushNotificationStore() private let localAuthenticationService = LocalAuthenticationService(pinCodePreferences: .shared) - - private let backgroundServiceInitQueue = DispatchQueue(label: "backgroundServiceInitQueue") + private static let backgroundServiceInitQueue = DispatchQueue(label: "io.element.NotificationService.backgroundServiceInitQueue") // MARK: - Method Overrides override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) { @@ -170,7 +169,7 @@ class NotificationService: UNNotificationServiceExtension { MXKAccountManager.shared()?.forceReloadAccounts() self.userAccount = MXKAccountManager.shared()?.activeAccounts.first if let userAccount = userAccount { - backgroundServiceInitQueue.sync { + Self.backgroundServiceInitQueue.sync { if NotificationService.backgroundSyncService?.credentials != userAccount.mxCredentials { MXLog.debug("[NotificationService] setup: MXBackgroundSyncService init: BEFORE") self.logMemory() From f3938739fab8d45ba4ad1635f606d81797654e87 Mon Sep 17 00:00:00 2001 From: David Langley Date: Fri, 26 Nov 2021 12:52:40 +0000 Subject: [PATCH 3/5] Restore the pushkit token after login/logout when configuring PushKit --- Riot/Managers/PushNotification/PushNotificationService.m | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Riot/Managers/PushNotification/PushNotificationService.m b/Riot/Managers/PushNotification/PushNotificationService.m index fc6eab47d..4e22554af 100644 --- a/Riot/Managers/PushNotification/PushNotificationService.m +++ b/Riot/Managers/PushNotification/PushNotificationService.m @@ -266,6 +266,15 @@ Matrix session observer used to detect new opened sessions. - (void)configurePushKit { MXLogDebug(@"[PushNotificationService] configurePushKit") + NSData* token = [_pushRegistry pushTokenForType:PKPushTypeVoIP]; + if (token) { + // If the token is available, store it. This can happen if you sign out and back in. + // i.e We are registered, but we have cleared it form the the store on logout and the + // _pushRegistry lives through signin/signout as PushNotificationService is a singleton + // on app delegate. + _pushNotificationStore.pushKitToken = token; + MXLogDebug(@"[PushNotificationService] configurePushKit: Restored pushKit token") + } _pushRegistry.delegate = self; _pushRegistry.desiredPushTypes = [NSSet setWithObject:PKPushTypeVoIP]; From 45e4492bf951f2c19708bba43920f664d8c90daa Mon Sep 17 00:00:00 2001 From: David Langley Date: Fri, 26 Nov 2021 14:38:28 +0000 Subject: [PATCH 4/5] Create 5199.bugfix --- changelog.d/5199.bugfix | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/5199.bugfix diff --git a/changelog.d/5199.bugfix b/changelog.d/5199.bugfix new file mode 100644 index 000000000..d363f1449 --- /dev/null +++ b/changelog.d/5199.bugfix @@ -0,0 +1 @@ +Fix bug where VoIP calls would not connect reliably after signout/signin. From 67c22b9c5e3317e8f52954ea79e8474d867c4d48 Mon Sep 17 00:00:00 2001 From: David Langley Date: Fri, 26 Nov 2021 14:57:58 +0000 Subject: [PATCH 5/5] Update Riot/Managers/PushNotification/PushNotificationService.m Co-authored-by: ismailgulek --- Riot/Managers/PushNotification/PushNotificationService.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Riot/Managers/PushNotification/PushNotificationService.m b/Riot/Managers/PushNotification/PushNotificationService.m index 4e22554af..13db25148 100644 --- a/Riot/Managers/PushNotification/PushNotificationService.m +++ b/Riot/Managers/PushNotification/PushNotificationService.m @@ -269,7 +269,7 @@ Matrix session observer used to detect new opened sessions. NSData* token = [_pushRegistry pushTokenForType:PKPushTypeVoIP]; if (token) { // If the token is available, store it. This can happen if you sign out and back in. - // i.e We are registered, but we have cleared it form the the store on logout and the + // i.e We are registered, but we have cleared it from the the store on logout and the // _pushRegistry lives through signin/signout as PushNotificationService is a singleton // on app delegate. _pushNotificationStore.pushKitToken = token;