forked from p0deje/Maccy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Post notifications on new copy and selection
This might have problems pre-Sonoma, but at least there we can post notifications with custom sounds. Fixes p0deje#211
- Loading branch information
Showing
11 changed files
with
63 additions
and
29 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,39 @@ | ||
import UserNotifications | ||
|
||
class Notifier { | ||
private static var center: UNUserNotificationCenter { UNUserNotificationCenter.current() } | ||
|
||
static func authorize() { | ||
center.requestAuthorization(options: [.alert, .sound]) { granted, error in | ||
if error != nil { | ||
NSLog("Failed to authorize notifications: \(String(describing: error))") | ||
} | ||
} | ||
} | ||
|
||
static func notify(body: String?, sound: UNNotificationSound) { | ||
guard let body else { return } | ||
|
||
authorize() | ||
|
||
center.getNotificationSettings { settings in | ||
guard (settings.authorizationStatus == .authorized) || | ||
(settings.authorizationStatus == .provisional) else { return } | ||
|
||
let content = UNMutableNotificationContent() | ||
if settings.alertSetting == .enabled { | ||
content.body = body | ||
} | ||
if settings.soundSetting == .enabled { | ||
content.sound = sound | ||
} | ||
|
||
let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: nil) | ||
center.add(request) { error in | ||
if error != nil { | ||
NSLog("Failed to deliver notification: \(String(describing: error))") | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains 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
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains 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,6 @@ | ||
import UserNotifications | ||
|
||
extension UNNotificationSound { | ||
static let knock = UNNotificationSound(named: UNNotificationSoundName("knock.caf")) | ||
static let write = UNNotificationSound(named: UNNotificationSoundName("write.caf")) | ||
} |
This file contains 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