本文介绍了在Mac上使用Swift显示通知横幅的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图在使用Swift的Mac OS上显示一些通知横幅.但我只在通知中心看到它们,而不是横幅.
I tried to show up some notification banners on Mac OS with Swift. But i get them only in the notification center, not as banner.
您有个主意吗?这是我的简单代码:
Do you have an idea? Here my simple code:
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"
}
推荐答案
如果您的应用程序位于前台,则不会显示标题.
You won't get a banner if your app is in the foreground.
尝试使用…
notification.deliveryDate = Date(timeIntervalSinceNow: 5)
NSUserNotificationCenter.default.scheduleNotification(notification)
然后切换到另一个应用
这篇关于在Mac上使用Swift显示通知横幅的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!