Ich bin neu in Swift (aber nicht zur Programmierung). Ich habe eine einfache App, die basierend auf bestimmten Bedingungen eine Warnung bereitstellt. Ich möchte eine Funktion ausführen (oder auch nur eine Variable setzen), wenn eine der Tasten gedrückt wird. Im Idealfall brauche ich nur einen Knopf, aber wenn aus irgendeinem Grund nur der notification.actionButtonTitle
einen Handler haben kann, ist das in Ordnung für mich.macOS Swift 3 - Handhabung der Benachrichtigung Alarm Aktion Taste
Mein Benachrichtigungscode befindet sich derzeit in einer Swift-Datei als Hilfsprogramm.
import Foundation
class NotificationHelper {
static func sampleNotification(notification: NSUserNotification) {
let notificationCenter = NSUserNotificationCenter.default
notification.identifier = "unique-id-123"
notification.hasActionButton = true
notification.otherButtonTitle = "Close"
notification.actionButtonTitle = "Show"
notification.title = "Hello"
notification.subtitle = "How are you?"
notification.informativeText = "This is a test"
notificationCenter.deliver(notification)
}
}
Derzeit in AppDelegate
wird diese definiert:
let notification = NSUserNotification()
... und ich rufe die Meldung wie folgt aus:
NotificationHelper.sampleNotification(notification: notification)
Die resultierende Benachrichtigung funktioniert, wie Sie im Screenshot sehen unten. Ich kann jedoch nicht auf die Tastenaktion hören. Ich habe das Hinzufügen dieser versucht, die AppDelegate
sowie die NotificationHelper
Datei, aber ich hatte keinen Erfolg damit:
func userNotificationCenter(center: NSUserNotificationCenter, didActivateNotification notification: NSUserNotification) {
print("checking notification response")
}
Jede Idee von dem, was ich fehle?
Danke!