Send notification to Mountain lion notification center

Viewed 22657

Can someone give an example of sending a test notification from a Cocoa app to Notifications Center? eg. when I click on an NSButton

2 Answers

@alexjohnj's answer updated for Swift 5.2

In AppDelegate

func applicationDidFinishLaunching(_ aNotification: Notification) {

  // Set delegate
  NSUserNotificationCenter.default.delegate = self
}

And then confirm to NSUserNotificationCenterDelegate as

extension AppDelegate: NSUserNotificationCenterDelegate {
 func userNotificationCenter(_ center: NSUserNotificationCenter, shouldPresent notification: NSUserNotification) -> Bool {
    true
}}
Related