gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 05:29:34 26 4
gpt4 key购买 nike

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

我创建了一个新项目,其中包含 Swift 文件但没有 Storyboard。我的 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
}


}

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

最后,是的,应用程序“test”(我就是这样命名的)被设置为显示横幅。 screenshot of Notifications System Panel
总结:我的应用程序成功发出通知,这些通知将添加到通知列表中,但不会显示为横幅。(我使用的是 OS X Yosemite 10.10,希望这个问题与 beta 无关)

最佳答案

如果应用程序在发送通知时是前台/事件应用程序,则不会显示某些通知。参见 NSUserNotification's presented property了解更多信息。

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

请注意,当 providing a delegate to NSUserNotificationCenter 发送通知(并检查 presented)时,您仍然可以收到通知,包括通过实现 userNotificationCenter:shouldPresentNotification: 覆盖呈现行为。

关于notifications - Swift 和 NSUserNotification - 没有横幅或警报,但会静默添加到通知列表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25303187/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com