我是 OSX 开发的新手,我正在制作一个应用程序,它会在发生某些事情时触发通知.但是当应用程序是关键应用程序时,它不会显示通知,因为这是默认行为.即使应用程序是关键应用程序,我也想向他们展示.但是我只找到了用objective-c编写的这个问题的解决方案,但现在我正在使用Swift.我想知道如何用 Swift 实现它.
I am new to OSX development and I am making an app which fires a notification when something happens. But it isn't showing the notification when the app is the key app, as it is the default behavior. I want to show them even when the app IS the key app. However I only found solutions to this matter that were written in objective-c but right now I am working with Swift. I was wondering how I could I implement it with Swift.
推荐答案为了确保始终显示通知,您需要为 NSUserNotificationCenter 设置一个委托并实现 userNotificationCenter(center:shouldPresentNotification:) ->布尔.文档 说这条消息是
To ensure the notifications are always shown you'll need to set a delegate for NSUserNotificationCenter and implement userNotificationCenter(center:shouldPresentNotification:) -> Bool. The documentation says this message is
当用户通知中心决定不发送时发送给委托展示您的通知.
Sent to the delegate when the user notification center has decided not to present your notification.
您可以在您选择的任何类中实现委托.下面是一个例子:
You can implement the delegate in any class of your choosing. Here is an example:
class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDelegate { func applicationDidFinishLaunching(aNotification: NSNotification) { NSUserNotificationCenter.defaultUserNotificationCenter().delegate = self } func userNotificationCenter(center: NSUserNotificationCenter, shouldPresentNotification notification: NSUserNotification) -> Bool { return true }更多推荐
应用程序处于活动状态时,Swift NSUserNotification 不显示
发布评论