我想在应用程序位于最前面时显示用户通知。我找到了下面的代码,但我不确定如何使用委托:它似乎只返回一个布尔值。
class MyNotificationDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDelegate {
func applicationDidFinishLaunching(aNotification: NSNotification) {
NSUserNotificationCenter.defaultUserNotificationCenter().delegate = self
}
func userNotificationCenter(center: NSUserNotificationCenter, shouldPresentNotification notification: NSUserNotification) -> Bool {
return true
} }
我试过一些句子,如:
var delegate : MyNotificationDelegate = MyNotificationDelegate()
var notification:NSUserNotification = NSUserNotification()
var notificationcenter:NSUserNotificationCenter = NSUserNotificationCenter.defaultUserNotificationCenter()
delegate.userNotificationCenter(notificationcenter, shouldPresentNotification: notification)
但它不会显示横幅。我知道对于NSUserNotificationCenter
,deliverNotification:
方法是显示横幅的方式。但我不确定NSUserNotificationCenterDelegate
协议。
如何始终显示通知横幅?