![](/img/trans.png)
[英]Display banner for NSUserNotification while app is frontmost
[英]Display NSUserNotification when app is active
我當前正在制作一個XIB菜單欄應用程序,該應用程序使用以下代碼顯示通知:
func showNotification(message:String, title:String = "App Name") -> Void {
let notification = NSUserNotification()
notification.title = title
notification.informativeText = message
NSUserNotificationCenter.defaultUserNotificationCenter().deliverNotification(notification)
}
並這樣稱呼它:
showNotification("\(song)\n\(artist)",title: "Now Playing")
隱藏菜單欄應用程序(未顯示)時,該通知有效,但是當用戶顯示菜單欄應用程序時,該通知不顯示。
在查看應用程序時,是否可以顯示通知?
默認情況下,當您的應用程序處於活動狀態時,不會顯示您的應用程序傳遞的通知。 為了獲得預期的行為,您必須使用如下所示的用戶通知中心委托:
extension AppController: NSUserNotificationCenterDelegate {
private func setupUserNotificationCenter() {
let nc = NSUserNotificationCenter.defaultUserNotificationCenter()
nc.delegate = self
}
public func userNotificationCenter(center: NSUserNotificationCenter, shouldPresentNotification notification: NSUserNotification) -> Bool {
return true
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.