Swift和NSUserNotification - 没有横幅或警报,但会静默添加到通知列表中

Isa*_*iah 11 notifications silent nsnotificationcenter nsusernotification swift

我正在学习Swift,并希望将简单的用户通知显示为测试.
我的构建成功,但没有显示横幅,而是将通知静默添加到通知列表中.我已经验证了"请勿打扰"已关闭,我在AppleScript中尝试了同样的功能,并且在使用NSUserNotificationAlertStyle我的应用程序的info.plist中的代码时,我尝试了一次成功的非静音警报(不是横幅) ).
但是,这只发生过一次.

我用Swift文件和没有故事板创建了一个新项目.我的AppDelegate.swift包含

import Cocoa

class AppDelegate: NSObject, NSApplicationDelegate {

    @IBOutlet weak var window: NSWindow!


    func applicationDidFinishLaunching(aNotification: NSNotification?) {
        var notification:NSUserNotification = NSUserNotification()
        notification.title = "Title"
        notification.subtitle = "Subtitle"
        notification.informativeText = "Informative text"

        notification.soundName = NSUserNotificationDefaultSoundName

        notification.deliveryDate = NSDate(timeIntervalSinceNow: 5)
        var notificationcenter:NSUserNotificationCenter = NSUserNotificationCenter.defaultUserNotificationCenter()
        if let notificationCenter = NSUserNotificationCenter.defaultUserNotificationCenter() {
            notificationcenter.scheduleNotification(notification)
        }
    }

    func applicationWillTerminate(aNotification: NSNotification?) {
        // Insert code here to tear down your application
    }


}
Run Code Online (Sandbox Code Playgroud)

而且我的info.plist不包含NSUserNotificationAlertStyle前面提到的内容,因为对于横幅来说这不是必需的,我相信.

最后,是的,应用程序"测试"(因为我已经命名它)被设置为显示横幅,因此. 通知系统面板的屏幕截图
总结:我的应用程序成功发出通知,这些通知将添加到通知列表中,但不会显示为横幅.(我使用的是OS X Yosemite 10.10.我希望这个问题与测试版无关)

Jes*_*per 21

如果应用程序是通知发送时的前台/活动应用程序,则不会显示某些通知.见NSUserNotification的presented财产获取更多信息.

例如,当您在iTunes中并且新歌开始播放时,您已经看到正在播放的歌曲,并且不需要显示横幅.

请注意,presented通过提供委托来NSUserNotificationCenter传递(和检查)通知时,您仍然可以收到通知,包括通过实施覆盖表示行为userNotificationCenter:shouldPresentNotification:.