首页 > 解决方案 > 使用 Swift 在 Mac 上显示通知横幅

问题描述

我尝试使用 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"
}

标签: swiftmacosnotifications

解决方案


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

尝试使用...</p>

notification.deliveryDate = Date(timeIntervalSinceNow: 5)
NSUserNotificationCenter.default.scheduleNotification(notification)

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


推荐阅读