Home » Swift » Swift NSUserNotificationCenter didActivateNotification not working

Swift NSUserNotificationCenter didActivateNotification not working

Posted by: admin January 9, 2018 Leave a comment

Questions:

I’m working on a swift os x application and I’m having trouble understanding the userNoticiation / didActivateNotification structure. I’ve reviewed the documentation and searched SO but am still stuck. Any guidance on the following code would be much appreciated:

func notify(message: String, callBack: String) {


    println(message)
    println(callBack)
    var notification:NSUserNotification = NSUserNotification()
    notification.title = "New Phone Call"
    notification.informativeText = message
    notification.actionButtonTitle = "Lookup"
    notification.hasActionButton = true
    var center:NSUserNotificationCenter = NSUserNotificationCenter.defaultUserNotificationCenter()


    center.delegate = self
    center.scheduleNotification(notification)



}
func notify (center: NSUserNotificationCenter, didActivateNotification notification: NSUserNotification){
    center.delegate = self
    println("clicked")  //this does not print
}

The notification displays exactly as I’d like it to. Clicking the “Lookup” button that I’ve defined will bring my app to the foreground the first time it is clicked, but the code I’d expect to handle that action does not fire.

Thanks in advance.

Answers:

Change your second ‘func notify’ declaration to ‘optional func userNotificationCenter’