![](/img/trans.png)
[英]Swift: Cocoa, local notification not showing in mac OS Catalina
[英]Showing notification banner on Mac with 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"
}
如果您的應用程序在前台,您將不會收到橫幅。
嘗試使用…
notification.deliveryDate = Date(timeIntervalSinceNow: 5)
NSUserNotificationCenter.default.scheduleNotification(notification)
然后切換到另一個應用程序
如果在發送通知時,發送通知的應用程序處於焦點狀態,則通知不會顯示為橫幅。 如果應用程序在前台未處於活動狀態,則它只能發送橫幅通知。
當您的應用不是主要焦點時,您的代碼運行良好,我剛剛對其進行了測試。
因此,因為您在單擊按鈕時發送通知,所以在發送通知時應用程序會聚焦:通知只會轉到通知中心,不會顯示為橫幅。
這是蘋果故意制定的規則。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.