Swift NSUserNotificationCenter didActivateNotification not working
Asked Answered
S

1

7

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.

Speedball answered 11/12, 2014 at 17:12 Comment(0)
S
5

Change your second 'func notify' declaration to 'optional func userNotificationCenter'

Slump answered 11/12, 2014 at 20:45 Comment(3)
Did this actually work? I tried it when my notification clicks weren't firing and it sends the error " 'optional' can only be applied to protocol memebers "Pleo
@ChrisR Maybe that was with Swift 1.x? Have you tried removing the optional qualifier?Lucero
The documentation for the didActivateNotification delegate function is here.Lucero

© 2022 - 2024 — McMap. All rights reserved.