-
Notifications
You must be signed in to change notification settings - Fork 0
Add user notifications for Kanata status #1
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
Merged
malpern
merged 1 commit into
master
from
codex/add-usernotificationservice-for-notifications
Aug 30, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| import Foundation | ||
| import UserNotifications | ||
|
|
||
| /// Manages local user notifications for KeyPath | ||
| @MainActor | ||
| final class UserNotificationService: NSObject, UNUserNotificationCenterDelegate { | ||
| static let shared = UserNotificationService() | ||
|
|
||
| private let center = UNUserNotificationCenter.current() | ||
| private let preferences: PreferencesService | ||
| private let authorizationRequestedKey = "KeyPath.NotificationAuthorizationRequested" | ||
|
|
||
| private override init(preferences: PreferencesService = .shared) { | ||
| self.preferences = preferences | ||
| super.init() | ||
| center.delegate = self | ||
| } | ||
|
|
||
| /// Request authorization if it hasn't been requested before | ||
| func requestAuthorizationIfNeeded() { | ||
| let requested = UserDefaults.standard.bool(forKey: authorizationRequestedKey) | ||
| guard !requested else { return } | ||
|
|
||
| center.requestAuthorization(options: [.alert, .sound]) { granted, _ in | ||
| UserDefaults.standard.set(true, forKey: self.authorizationRequestedKey) | ||
| Task { @MainActor in | ||
| self.preferences.notificationsEnabled = granted | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Send a generic notification if the user has enabled notifications | ||
| private func sendNotification(title: String, body: String) { | ||
| guard preferences.notificationsEnabled else { return } | ||
|
|
||
| let content = UNMutableNotificationContent() | ||
| content.title = title | ||
| content.body = body | ||
|
|
||
| let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: nil) | ||
| center.add(request) | ||
| } | ||
|
|
||
| /// Notify user of a launch failure | ||
| func notifyLaunchFailure(_ status: LaunchFailureStatus) { | ||
| sendNotification(title: "Kanata Launch Failed", body: status.shortMessage) | ||
| } | ||
|
|
||
| /// Notify user of a status change | ||
| func notifyStatusChange(_ state: SimpleKanataState) { | ||
| sendNotification(title: "Kanata Status", body: "Kanata is now \(state.displayName)") | ||
| } | ||
| } | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[P0] Call main-actor notification API from non-isolated KanataManager
UserNotificationService is declared
@MainActor, so invokingUserNotificationService.shared.notifyStatusChange(...)from KanataManager’s property observer (which is not main-actor isolated) will not compile: the compiler reports “call to main actor-isolated instance method … in a synchronous nonisolated context.” Wrap these calls inTask { @MainActor in … }or mark KanataManager as@MainActorbefore accessing the notification service. The same issue exists in thelaunchFailureStatusobserver below.Useful? React with 👍 / 👎.