I am currently making a XIB Menu Bar application that displays a notification using this code:
func showNotification(message:String, title:String = "App Name") -> Void {
let notification = NSUserNotification()
notification.title = title
notification.informativeText = message
NSUserNotificationCenter.defaultUserNotificationCenter().deliverNotification(notification)
}
And calling it like this:
showNotification("\(song)\n\(artist)",title: "Now Playing")
The notification works when the Menu Bar application is hidden away (not shown), however when the user has it shown, the notification does not show.
Is there a way to show the notification while the application is in view?
By default when your application is active, notifications delivered by your app are not shown. To get the expected behaviour, you have to use the user notification center delegate like below :
extension AppController: NSUserNotificationCenterDelegate {
private func setupUserNotificationCenter() {
let nc = NSUserNotificationCenter.defaultUserNotificationCenter()
nc.delegate = self
}
public func userNotificationCenter(center: NSUserNotificationCenter, shouldPresentNotification notification: NSUserNotification) -> Bool {
return true
}
}