使用 Swift 在 Mac 上显示通知横幅

Hei*_*amp 7 macos notifications swift

我尝试使用 Swift 在 Mac OS 上显示一些通知横幅。但我只在通知中心得到它们,而不是作为横幅。

你有想法吗?这是我的简单代码:

func showNotification() -> Void {
    let notification = NSUserNotification()
    notification.title = "Title of notification"
    notification.subtitle = "Subtitle of notification"
    notification.soundName = NSUserNotificationDefaultSoundName
    NSUserNotificationCenter.default.deliver(notification) 
}

@IBAction func btnPressed(_ sender: NSButton) {
    showNotification()
    testLbl.stringValue = "Button was pressed"
}
Run Code Online (Sandbox Code Playgroud)

Ash*_*lls 7

如果您的应用程序在前台,您将不会收到横幅。

尝试使用…

notification.deliveryDate = Date(timeIntervalSinceNow: 5)
NSUserNotificationCenter.default.scheduleNotification(notification)
Run Code Online (Sandbox Code Playgroud)

然后切换到另一个应用程序