Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions Coder-Desktop/Coder-Desktop/Coder_DesktopApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,10 @@ class AppDelegate: NSObject, NSApplicationDelegate {
let state: AppState
let fileSyncDaemon: MutagenDaemon
let urlHandler: URLHandler
let notifDelegate: NotifDelegate
let autoUpdater: UpdaterService

override init() {
notifDelegate = NotifDelegate()
AppDelegate.registerNotificationCategories()
vpn = CoderVPNService()
autoUpdater = UpdaterService()
let state = AppState(onChange: vpn.configureTunnelProviderProtocol)
Expand All @@ -79,8 +78,9 @@ class AppDelegate: NSObject, NSApplicationDelegate {
}
self.fileSyncDaemon = fileSyncDaemon
urlHandler = URLHandler(state: state, vpn: vpn)
super.init()
// `delegate` is weak
UNUserNotificationCenter.current().delegate = notifDelegate
UNUserNotificationCenter.current().delegate = self
}

func applicationDidFinishLaunching(_: Notification) {
Expand Down Expand Up @@ -164,7 +164,11 @@ class AppDelegate: NSObject, NSApplicationDelegate {
do { try urlHandler.handle(url) } catch let handleError {
Task {
do {
try await sendNotification(title: "Failed to handle link", body: handleError.description)
try await sendNotification(
title: "Failed to handle link",
body: handleError.description,
category: .uriFailure
)
} catch let notifError {
logger.error("Failed to send notification (\(handleError.description)): \(notifError)")
}
Expand Down
17 changes: 13 additions & 4 deletions Coder-Desktop/Coder-Desktop/Notifications.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import UserNotifications

class NotifDelegate: NSObject, UNUserNotificationCenterDelegate {
override init() {
super.init()
extension AppDelegate: UNUserNotificationCenterDelegate {
static func registerNotificationCategories() {
UNUserNotificationCenter.current().setNotificationCategories(
Set(NotificationCategory.allCases.map {
UNNotificationCategory(identifier: $0.rawValue, actions: [], intentIdentifiers: [], options: [])
})
)
}

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

func sendNotification(title: String, body: String) async throws {
func sendNotification(title: String, body: String, category: NotificationCategory) async throws {
let nc = UNUserNotificationCenter.current()
let granted = try await nc.requestAuthorization(options: [.alert, .badge])
guard granted else {
Expand All @@ -24,5 +28,10 @@ func sendNotification(title: String, body: String) async throws {
let content = UNMutableNotificationContent()
content.title = title
content.body = body
content.categoryIdentifier = category.rawValue
try await nc.add(.init(identifier: UUID().uuidString, content: content, trigger: nil))
}

enum NotificationCategory: String, CaseIterable {
case uriFailure = "URI_FAILURE"
}
Loading