Cocoa 在特定时间后隐藏通知

Cocoa 在特定时间后隐藏通知,cocoa,notifications,swift2,Cocoa,Notifications,Swift2,当前,当我使用警报样式创建NSUserNotification时,它不会隐藏,除非我手动关闭它 有没有办法在2秒后自动关闭/隐藏它 NSUserNotification代码仅供参考: let notification:NSUserNotification = NSUserNotification() notification.title = "Title" notification.subtitle = "Subtitle" notification.informativeText = "In

当前,当我使用警报样式创建NSUserNotification时,它不会隐藏,除非我手动关闭它

有没有办法在2秒后自动关闭/隐藏它

NSUserNotification代码仅供参考:

let notification:NSUserNotification = NSUserNotification()
notification.title = "Title"
notification.subtitle = "Subtitle"
notification.informativeText = "Informative text"

notification.soundName = NSUserNotificationDefaultSoundName

notification.deliveryDate = NSDate(timeIntervalSinceNow: 10)
notification.hasActionButton = false
let notificationcenter:NSUserNotificationCenter = NSUserNotificationCenter.defaultUserNotificationCenter()
notificationcenter.scheduleNotification(notification)
Blockquote有没有办法在2秒后自动关闭/隐藏它

不,在OSX 10.11之前,您没有任何此类选项,苹果可能会在未来提供

用户可以通过三种方式自定义NSUserNotification(也称为咆哮通知):

  • 没有
  • 横幅
  • 警觉的
  • 作为开发人员,您无法控制系统设置。这取决于用户启用或禁用并选择他喜欢的通知类型

    如果您希望向用户显示任何警报,您可以创建自己的警报窗口并将其显示在该角。您可以将计时器设置为关闭,或者在需要时提供操作按钮将其关闭

    更新1: Cocoa提供
    NSWindow
    NSPanel
    (抬头显示器和普通面板)。您可以根据需要自定义窗口或面板。检查是否有多个选项可以帮助您根据您的要求进行成型


    如果您无法获取,比如您想要一个圆角,那么您需要自定义窗口/视图等。

    您可以使用带有计时器的
    removeDeliveredNotification
    :或
    RemoveAllDeliveredNotification

    // Clear a delivered notification from the notification center. If the notification is not in the delivered list, nothing happens.
    - (void)removeDeliveredNotification:(NSUserNotification *)notification;
    
    // Clear all delivered notifications for this application from the notification center.
    - (void)removeAllDeliveredNotifications;
    

    OSX(10.8及更高版本)

    使用NSObject的
    performSelector:withObject:afterDelay:
    方法

    由于您要在某个时间间隔后安排通知传递,因此需要在传递之前的初始延迟之外再添加解除之前的额外延迟。在这里,我把它们写成了交付前10秒和解雇前2秒的常数:

    let delayBeforeDelivering: NSTimeInterval = 10
    let delayBeforeDismissing: NSTimeInterval = 2
    
    let notification = NSUserNotification()
    notification.title = "Title"
    notification.deliveryDate = NSDate(timeIntervalSinceNow: delayBeforeDelivering)
    
    let notificationcenter = NSUserNotificationCenter.defaultUserNotificationCenter()
    
    notificationcenter.scheduleNotification(notification)
    
    notificationcenter.performSelector("removeDeliveredNotification:",
        withObject: notification,
        afterDelay: (delayBeforeDelivering + delayBeforeDismissing))
    
    对于Swift 5,您可以使用以下各项:

    let delayBeforeDelivering: TimeInterval = 10
    let delayBeforeDismissing: TimeInterval = 2
    
    let notification = NSUserNotification()
    notification.title = "Title"
    notification.deliveryDate = Date(timeIntervalSinceNow: delayBeforeDelivering)
    
    let notificationcenter = NSUserNotificationCenter.default
    
    notificationcenter.scheduleNotification(notification)
    
    notificationcenter.perform(#selector(NSUserNotificationCenter.removeDeliveredNotification(_:)),
                    with: notification,
                    afterDelay: (delayBeforeDelivering + delayBeforeDismissing))
    

    你好,谢谢你的回复。您建议使用自定义警报窗口吗?你能建议我应该看什么api吗?我正在研究新的MS outlook 2016应用程序,它实现了某种警报,看起来与默认警报完全相同,但不是默认警报。它也会自动隐藏。因此,它甚至可能使用类似的方法,谢谢你。所以我试着用这个,但不幸的是它不起作用。这是我在之前的代码之后添加的代码。let seconds=15.0 let delay=seconds*Double(NSEC\u PER\u seconds)//纳秒/seconds let dispatchTime=dispatch\u time(dispatch\u time\u NOW,Int64(delay))dispatch\u after(dispatch\u get\u main\u queue(),{var test NSUserNotificationCenter.removeAllDeliveredNotifications(notificationcenter)print(test)})如果你想把它留在通知中心怎么办?(只需删除弹出窗口)有办法吗?