Skip to content

Commit 69e4d72

Browse files
chore: refactor notifications to support categories (#265)
1 parent 9c13c31 commit 69e4d72

2 files changed

Lines changed: 21 additions & 8 deletions

File tree

‎Coder-Desktop/Coder-Desktop/Coder_DesktopApp.swift‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,10 @@ class AppDelegate: NSObject, NSApplicationDelegate {
4949
let state: AppState
5050
let fileSyncDaemon: MutagenDaemon
5151
let urlHandler: URLHandler
52-
let notifDelegate: NotifDelegate
5352
let autoUpdater: UpdaterService
5453

5554
override init() {
56-
notifDelegate = NotifDelegate()
55+
AppDelegate.registerNotificationCategories()
5756
vpn = CoderVPNService()
5857
autoUpdater = UpdaterService()
5958
let state = AppState(onChange: vpn.configureTunnelProviderProtocol)
@@ -79,8 +78,9 @@ class AppDelegate: NSObject, NSApplicationDelegate {
7978
}
8079
self.fileSyncDaemon = fileSyncDaemon
8180
urlHandler = URLHandler(state: state, vpn: vpn)
81+
super.init()
8282
// `delegate` is weak
83-
UNUserNotificationCenter.current().delegate = notifDelegate
83+
UNUserNotificationCenter.current().delegate = self
8484
}
8585

8686
func applicationDidFinishLaunching(_: Notification) {
@@ -164,7 +164,11 @@ class AppDelegate: NSObject, NSApplicationDelegate {
164164
do { try urlHandler.handle(url) } catch let handleError {
165165
Task {
166166
do {
167-
try await sendNotification(title: "Failed to handle link", body: handleError.description)
167+
try await sendNotification(
168+
title: "Failed to handle link",
169+
body: handleError.description,
170+
category: .uriFailure
171+
)
168172
} catch let notifError {
169173
logger.error("Failed to send notification (\(handleError.description)): \(notifError)")
170174
}

‎Coder-Desktop/Coder-Desktop/Notifications.swift‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
import UserNotifications
22

3-
class NotifDelegate: NSObject, UNUserNotificationCenterDelegate {
4-
override init() {
5-
super.init()
3+
extension AppDelegate: UNUserNotificationCenterDelegate {
4+
static func registerNotificationCategories() {
5+
UNUserNotificationCenter.current().setNotificationCategories(
6+
Set(NotificationCategory.allCases.map {
7+
UNNotificationCategory(identifier: $0.rawValue, actions: [], intentIdentifiers: [], options: [])
8+
})
9+
)
610
}
711

812
// This function is required for notifications to appear as banners whilst the app is running.
@@ -15,7 +19,7 @@ class NotifDelegate: NSObject, UNUserNotificationCenterDelegate {
1519
}
1620
}
1721

18-
func sendNotification(title: String, body: String) async throws {
22+
func sendNotification(title: String, body: String, category: NotificationCategory) async throws {
1923
let nc = UNUserNotificationCenter.current()
2024
let granted = try await nc.requestAuthorization(options: [.alert, .badge])
2125
guard granted else {
@@ -24,5 +28,10 @@ func sendNotification(title: String, body: String) async throws {
2428
let content = UNMutableNotificationContent()
2529
content.title = title
2630
content.body = body
31+
content.categoryIdentifier = category.rawValue
2732
try await nc.add(.init(identifier: UUID().uuidString, content: content, trigger: nil))
2833
}
34+
35+
enum NotificationCategory: String, CaseIterable {
36+
case uriFailure = "URI_FAILURE"
37+
}

0 commit comments

Comments
 (0)