From 305dd126717fd6ba870a1399d6a8627828213024 Mon Sep 17 00:00:00 2001 From: kukabi Date: Sun, 28 Apr 2024 17:33:42 +0300 Subject: [PATCH] Update backend when the APNS token changes. Bump version. --- SubVT.xcodeproj/project.pbxproj | 8 ++--- SubVT/Classes/App/SubVTApp.swift | 33 ++++++++++++++++---- SubVT/Classes/Utility/NotificationUtil.swift | 26 +++++++++++++++ 3 files changed, 57 insertions(+), 10 deletions(-) diff --git a/SubVT.xcodeproj/project.pbxproj b/SubVT.xcodeproj/project.pbxproj index 8089201..eb1ee05 100644 --- a/SubVT.xcodeproj/project.pbxproj +++ b/SubVT.xcodeproj/project.pbxproj @@ -2208,7 +2208,7 @@ ASSETCATALOG_COMPILER_INCLUDE_ALL_APPICON_ASSETS = NO; CODE_SIGN_ENTITLEMENTS = SubVT/Resources/Config/SubVT.entitlements; CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 15; + CURRENT_PROJECT_VERSION = 16; DEVELOPMENT_ASSET_PATHS = "\"SubVT/Preview Content\""; DEVELOPMENT_TEAM = TM389H3UFR; ENABLE_PREVIEWS = YES; @@ -2225,7 +2225,7 @@ "$(inherited)", "@executable_path/Frameworks", ); - MARKETING_VERSION = 0.1.5; + MARKETING_VERSION = 0.1.6; OTHER_LDFLAGS = ( "-Xlinker", "-interposable", @@ -2248,7 +2248,7 @@ ASSETCATALOG_COMPILER_INCLUDE_ALL_APPICON_ASSETS = NO; CODE_SIGN_ENTITLEMENTS = SubVT/Resources/Config/SubVT.entitlements; CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 15; + CURRENT_PROJECT_VERSION = 16; DEVELOPMENT_ASSET_PATHS = "\"SubVT/Preview Content\""; DEVELOPMENT_TEAM = TM389H3UFR; ENABLE_PREVIEWS = YES; @@ -2265,7 +2265,7 @@ "$(inherited)", "@executable_path/Frameworks", ); - MARKETING_VERSION = 0.1.5; + MARKETING_VERSION = 0.1.6; PRODUCT_BUNDLE_IDENTIFIER = io.helikon.subvt; PRODUCT_NAME = "$(TARGET_NAME)"; SUPPORTS_MACCATALYST = NO; diff --git a/SubVT/Classes/App/SubVTApp.swift b/SubVT/Classes/App/SubVTApp.swift index 0724e4d..a1b2129 100644 --- a/SubVT/Classes/App/SubVTApp.swift +++ b/SubVT/Classes/App/SubVTApp.swift @@ -66,17 +66,38 @@ class AppDelegate: NSObject, UIApplicationDelegate { _ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data ) { - guard self.apnsToken.isEmpty else { return } let token = deviceToken.map { String(format: "%02.2hhx", $0) }.joined() - self.apnsToken = token + if !self.apnsToken.isEmpty + && self.apnsToken != token + && self.notificationChannelId > 0 { + NotificationUtil.deleteUserNotificationChannel(channelId: UInt64(self.notificationChannelId)) { + self.apnsToken = "" + self.notificationChannelId = 0 + self.createNotificationChannel(token: token) + } onError: { error in + log.error("Error while deleting existing notification channel: \(error)") + } + return + } else { + self.createNotificationChannel(token: token) + } + } + + private func createNotificationChannel(token: String) { + guard self.apnsToken.isEmpty else { + return + } self.apnsSetupHasFailed = false NotificationUtil.createAPNSNotificationChannel(token: token) { channelId in + self.apnsToken = token self.hasCompletedAPNSRegistration = true self.notificationChannelId = Int(channelId) - NotificationUtil.createDefaultUserNotificationRules(channelId: channelId) { - self.hasCreatedDefaultNotificationRules = true - } onError: { error in - self.apnsSetupHasFailed = true + if !self.hasCreatedDefaultNotificationRules { + NotificationUtil.createDefaultUserNotificationRules(channelId: channelId) { + self.hasCreatedDefaultNotificationRules = true + } onError: { error in + self.apnsSetupHasFailed = true + } } } onError: { error in self.apnsSetupHasFailed = true diff --git a/SubVT/Classes/Utility/NotificationUtil.swift b/SubVT/Classes/Utility/NotificationUtil.swift index 13e01d2..d49f002 100644 --- a/SubVT/Classes/Utility/NotificationUtil.swift +++ b/SubVT/Classes/Utility/NotificationUtil.swift @@ -107,6 +107,32 @@ struct NotificationUtil { .store(in: &cancellables) } + static func deleteUserNotificationChannel( + channelId: UInt64, + onSuccess: @escaping () -> (), + onError: @escaping (APIError) -> () + ) { + NotificationUtil.appService.deleteUserNotificationChannel( + id: channelId + ) + .sink { (response) in + if let error = response.error { + log.error("Error while deleting user notification channel: \(error)") + onError(error) + } else { + switch response.result { + case .success: + log.info("Successfully deleted user notification channel.") + onSuccess() + case .failure(let error): + log.error("Error while deleting user notification channel: \(error)") + onError(error) + } + } + } + .store(in: &cancellables) + } + static func updateAppNotificationBadge(context: NSManagedObjectContext) { do { let fetchRequest : NSFetchRequest = Notification.fetchRequest()