Skip to content

Commit 305dd12

Browse files
committed
Update backend when the APNS token changes. Bump version.
1 parent 9041130 commit 305dd12

File tree

3 files changed

+57
-10
lines changed

3 files changed

+57
-10
lines changed

SubVT.xcodeproj/project.pbxproj

+4-4
Original file line numberDiff line numberDiff line change
@@ -2208,7 +2208,7 @@
22082208
ASSETCATALOG_COMPILER_INCLUDE_ALL_APPICON_ASSETS = NO;
22092209
CODE_SIGN_ENTITLEMENTS = SubVT/Resources/Config/SubVT.entitlements;
22102210
CODE_SIGN_STYLE = Automatic;
2211-
CURRENT_PROJECT_VERSION = 15;
2211+
CURRENT_PROJECT_VERSION = 16;
22122212
DEVELOPMENT_ASSET_PATHS = "\"SubVT/Preview Content\"";
22132213
DEVELOPMENT_TEAM = TM389H3UFR;
22142214
ENABLE_PREVIEWS = YES;
@@ -2225,7 +2225,7 @@
22252225
"$(inherited)",
22262226
"@executable_path/Frameworks",
22272227
);
2228-
MARKETING_VERSION = 0.1.5;
2228+
MARKETING_VERSION = 0.1.6;
22292229
OTHER_LDFLAGS = (
22302230
"-Xlinker",
22312231
"-interposable",
@@ -2248,7 +2248,7 @@
22482248
ASSETCATALOG_COMPILER_INCLUDE_ALL_APPICON_ASSETS = NO;
22492249
CODE_SIGN_ENTITLEMENTS = SubVT/Resources/Config/SubVT.entitlements;
22502250
CODE_SIGN_STYLE = Automatic;
2251-
CURRENT_PROJECT_VERSION = 15;
2251+
CURRENT_PROJECT_VERSION = 16;
22522252
DEVELOPMENT_ASSET_PATHS = "\"SubVT/Preview Content\"";
22532253
DEVELOPMENT_TEAM = TM389H3UFR;
22542254
ENABLE_PREVIEWS = YES;
@@ -2265,7 +2265,7 @@
22652265
"$(inherited)",
22662266
"@executable_path/Frameworks",
22672267
);
2268-
MARKETING_VERSION = 0.1.5;
2268+
MARKETING_VERSION = 0.1.6;
22692269
PRODUCT_BUNDLE_IDENTIFIER = io.helikon.subvt;
22702270
PRODUCT_NAME = "$(TARGET_NAME)";
22712271
SUPPORTS_MACCATALYST = NO;

SubVT/Classes/App/SubVTApp.swift

+27-6
Original file line numberDiff line numberDiff line change
@@ -66,17 +66,38 @@ class AppDelegate: NSObject, UIApplicationDelegate {
6666
_ application: UIApplication,
6767
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data
6868
) {
69-
guard self.apnsToken.isEmpty else { return }
7069
let token = deviceToken.map { String(format: "%02.2hhx", $0) }.joined()
71-
self.apnsToken = token
70+
if !self.apnsToken.isEmpty
71+
&& self.apnsToken != token
72+
&& self.notificationChannelId > 0 {
73+
NotificationUtil.deleteUserNotificationChannel(channelId: UInt64(self.notificationChannelId)) {
74+
self.apnsToken = ""
75+
self.notificationChannelId = 0
76+
self.createNotificationChannel(token: token)
77+
} onError: { error in
78+
log.error("Error while deleting existing notification channel: \(error)")
79+
}
80+
return
81+
} else {
82+
self.createNotificationChannel(token: token)
83+
}
84+
}
85+
86+
private func createNotificationChannel(token: String) {
87+
guard self.apnsToken.isEmpty else {
88+
return
89+
}
7290
self.apnsSetupHasFailed = false
7391
NotificationUtil.createAPNSNotificationChannel(token: token) { channelId in
92+
self.apnsToken = token
7493
self.hasCompletedAPNSRegistration = true
7594
self.notificationChannelId = Int(channelId)
76-
NotificationUtil.createDefaultUserNotificationRules(channelId: channelId) {
77-
self.hasCreatedDefaultNotificationRules = true
78-
} onError: { error in
79-
self.apnsSetupHasFailed = true
95+
if !self.hasCreatedDefaultNotificationRules {
96+
NotificationUtil.createDefaultUserNotificationRules(channelId: channelId) {
97+
self.hasCreatedDefaultNotificationRules = true
98+
} onError: { error in
99+
self.apnsSetupHasFailed = true
100+
}
80101
}
81102
} onError: { error in
82103
self.apnsSetupHasFailed = true

SubVT/Classes/Utility/NotificationUtil.swift

+26
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,32 @@ struct NotificationUtil {
107107
.store(in: &cancellables)
108108
}
109109

110+
static func deleteUserNotificationChannel(
111+
channelId: UInt64,
112+
onSuccess: @escaping () -> (),
113+
onError: @escaping (APIError) -> ()
114+
) {
115+
NotificationUtil.appService.deleteUserNotificationChannel(
116+
id: channelId
117+
)
118+
.sink { (response) in
119+
if let error = response.error {
120+
log.error("Error while deleting user notification channel: \(error)")
121+
onError(error)
122+
} else {
123+
switch response.result {
124+
case .success:
125+
log.info("Successfully deleted user notification channel.")
126+
onSuccess()
127+
case .failure(let error):
128+
log.error("Error while deleting user notification channel: \(error)")
129+
onError(error)
130+
}
131+
}
132+
}
133+
.store(in: &cancellables)
134+
}
135+
110136
static func updateAppNotificationBadge(context: NSManagedObjectContext) {
111137
do {
112138
let fetchRequest : NSFetchRequest<Notification> = Notification.fetchRequest()

0 commit comments

Comments
 (0)