Skip to content

feat: send push notifications for invalid coder scheme URIs #146

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: ethan/uri-handler
Choose a base branch
from
Open
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
18 changes: 15 additions & 3 deletions Coder-Desktop/Coder-Desktop/Coder_DesktopApp.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import FluidMenuBarExtra
import NetworkExtension
import os
import SDWebImageSVGCoder
import SDWebImageSwiftUI
import SwiftUI
import UserNotifications
import VPNLib

@main
Expand Down Expand Up @@ -36,13 +38,16 @@ struct DesktopApp: App {

@MainActor
class AppDelegate: NSObject, NSApplicationDelegate {
private var logger = Logger(subsystem: Bundle.main.bundleIdentifier!, category: "app-delegate")
private var menuBar: MenuBarController?
let vpn: CoderVPNService
let state: AppState
let fileSyncDaemon: MutagenDaemon
let urlHandler: URLHandler
let notifDelegate: NotifDelegate

override init() {
notifDelegate = NotifDelegate()
vpn = CoderVPNService()
let state = AppState(onChange: vpn.configureTunnelProviderProtocol)
vpn.onStart = {
Expand All @@ -67,6 +72,8 @@ class AppDelegate: NSObject, NSApplicationDelegate {
}
self.fileSyncDaemon = fileSyncDaemon
urlHandler = URLHandler(state: state, vpn: vpn)
// `delegate` is weak
UNUserNotificationCenter.current().delegate = notifDelegate
}

func applicationDidFinishLaunching(_: Notification) {
Expand Down Expand Up @@ -141,9 +148,14 @@ class AppDelegate: NSObject, NSApplicationDelegate {
// We only accept one at time, for now
return
}
do { try urlHandler.handle(url) } catch {
// TODO: Push notification
print(error.description)
do { try urlHandler.handle(url) } catch let handleError {
Task {
do {
try await sendNotification(title: "Failed to handle link", body: handleError.description)
} catch let notifError {
logger.error("Failed to send notification (\(handleError.description)): \(notifError)")
}
}
}
}

Expand Down
28 changes: 28 additions & 0 deletions Coder-Desktop/Coder-Desktop/Notifications.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import UserNotifications

class NotifDelegate: NSObject, UNUserNotificationCenterDelegate {
override init() {
super.init()
}

// This function is required for notifications to appear as banners whilst the app is running.
// We're effectively forwarding the notification back to the OS
nonisolated func userNotificationCenter(
_: UNUserNotificationCenter,
willPresent _: UNNotification
) async -> UNNotificationPresentationOptions {
[.banner]
}
}

func sendNotification(title: String, body: String) async throws {
let nc = UNUserNotificationCenter.current()
let granted = try await nc.requestAuthorization(options: [.alert, .badge])
guard granted else {
return
}
let content = UNMutableNotificationContent()
content.title = title
content.body = body
try await nc.add(.init(identifier: UUID().uuidString, content: content, trigger: nil))
}
1 change: 1 addition & 0 deletions Coder-Desktop/project.yml
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ targets:
com.apple.developer.system-extension.install: true
com.apple.security.application-groups:
- $(TeamIdentifierPrefix)com.coder.Coder-Desktop
aps-environment: development
settings:
base:
ASSETCATALOG_COMPILER_APPICON_NAME: AppIcon # Sets the app icon to "AppIcon".
Expand Down
Loading