Created
November 8, 2021 02:18
-
-
Save HereOrCode/cf184702b298181238aeb3fd781db203 to your computer and use it in GitHub Desktop.
Deliver an OSX notification with Swift
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
// Remember: import UserNotifications | |
func sendNotification(title: String, body: String = "") { | |
let content = UNMutableNotificationContent() | |
content.title = title | |
if body.count > 0 { | |
content.body = body | |
} | |
// you can alse add a subtitle | |
content.subtitle = "subtitle here... " | |
let uuidString = UUID().uuidString | |
let request = UNNotificationRequest( | |
identifier: uuidString, | |
content: content, trigger: nil) | |
let notificationCenter = UNUserNotificationCenter.current() | |
notificationCenter.requestAuthorization(options: [.alert, .sound]) { _, _ in } | |
notificationCenter.add(request) | |
} | |
/* | |
* How to use | |
* sendNotification(title: "Hello", body: "I am a programmer") | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Line 19 of UNUserNotificationCenter gives an error like this one =>
Any idea as to how I should solve this, I am trying to use your code from C++ using a bridging header and call it with different titles and subtitles and stuff but I don't know how to do it with this error.