Created
October 10, 2014 21:01
-
-
Save ericdke/fec20e6db9e0aa25e8ea 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
func showNotification() -> Void { | |
var notification = NSUserNotification() | |
notification.title = "Test from Swift" | |
notification.informativeText = "The body of this Swift notification" | |
notification.soundName = NSUserNotificationDefaultSoundName | |
NSUserNotificationCenter.defaultUserNotificationCenter().deliverNotification(notification) | |
} | |
//showNotification() |
Thanks @Valerio69 that worked for me, I was missing NSUserNotificationCenter.default.delegate = self
Thanks @Valerio69. Thank you so much, I could have an app that pops up notification upper-right of the screen.
https://gist.github.com/code4you2021/cf184702b298181238aeb3fd781db203
UNUserNotificationCenter | Apple Developer Documentation
https://developer.apple.com/documentation/usernotifications/unusernotificationcenter
Availability
iOS 10.0+
iPadOS 10.0+
macOS 10.14+
Mac Catalyst 13.0+
tvOS 10.0+
watchOS 3.0+
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@ixeau Without NSUserNotificationCenterDelegate your notification will be shown only into the Notification center. If you want to have the popup and probably interact with your Notification you need NSUserNotificationCenterDelegate.
Example:
This will show the top-right popup.