Skip to content

Instantly share code, notes, and snippets.

@rawnly
Created September 27, 2018 10:44
Show Gist options
  • Save rawnly/95de9b596c25390d3ca71e11b0d30ad6 to your computer and use it in GitHub Desktop.
Save rawnly/95de9b596c25390d3ca71e11b0d30ad6 to your computer and use it in GitHub Desktop.
Notification in swift 4.2
// Notification in swift 4.2
// Tested on MacOS 10.14
func showNotification(title: String, body: String, icon: NSImage = nil) -> Void {
let notification = NSUserNotification()
notification.title = title
notification.subtitle = body
if ( icon !== nil ) {
notification.contentImage = icon
}
notification.soundName = NSUserNotificationDefaultSoundName
NSUserNotificationCenter.default.delegate = self
NSUserNotificationCenter.default.deliver(notification)
}
extension ViewController: NSNotificationCenterDelegate {
func userNotificationCenter(_ center: NSUserNotificationCenter, shouldPresent notification: NSUserNotification) -> Bool {
return true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment