Skip to content

Commit

Permalink
Update backend when the APNS token changes. Bump version.
Browse files Browse the repository at this point in the history
  • Loading branch information
kukabi committed Apr 28, 2024
1 parent 9041130 commit 305dd12
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 10 deletions.
8 changes: 4 additions & 4 deletions SubVT.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -2225,7 +2225,7 @@
"$(inherited)",
"@executable_path/Frameworks",
);
MARKETING_VERSION = 0.1.5;
MARKETING_VERSION = 0.1.6;
OTHER_LDFLAGS = (
"-Xlinker",
"-interposable",
Expand All @@ -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;
Expand All @@ -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;
Expand Down
33 changes: 27 additions & 6 deletions SubVT/Classes/App/SubVTApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 26 additions & 0 deletions SubVT/Classes/Utility/NotificationUtil.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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> = Notification.fetchRequest()
Expand Down

0 comments on commit 305dd12

Please sign in to comment.