【问题标题】:Hide NSUserNotification after certain time一段时间后隐藏 NSUserNotification
【发布时间】:2015-12-22 04:16:19
【问题描述】:

目前,当我使用警报样式创建 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)

【问题讨论】:

    标签: cocoa notifications swift2


    【解决方案1】:

    实际上很简单,使用 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))
    

    【讨论】:

    • 如果你想把它留在通知中心怎么办? (只需删除弹出窗口)有没有办法做到这一点?
    【解决方案2】:

    您可以将removeDeliveredNotification: 或removeAllDeliveredNotifications 与计时器一起使用

    // 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;
    

    OS X(10.8 及更高版本)

    【讨论】:

    • 嗨,帕拉格,谢谢。所以我尝试使用它,但不幸的是它不起作用。这是我在之前的代码之后添加的代码。 let seconds = 15.0 let delay = seconds * Double(NSEC_PER_SEC) // 每秒纳秒 let dispatchTime = dispatch_time(DISPATCH_TIME_NOW, Int64(delay)) dispatch_after(dispatchTime, dispatch_get_main_queue(), { var test = NSUserNotificationCenter.removeAllDeliveredNotifications(notificationcenter) print(测试)})
    【解决方案3】:

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

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

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

    1. 横幅
    2. 提醒

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

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

    更新 1: Cocoa 提供NSWindow & NSPanel(HUD 和普通面板)。您可以根据需要自定义窗口或面板。检查有多个选项可以帮助您根据您的要求形成。

    如果你不能得到,说你想要一个圆角,那么你需要自定义窗口/视图等。

    【讨论】:

    • 嗨 Anoop,感谢您的回复。您建议使用自定义警报窗口?你能建议我应该看什么api吗?我正在研究新的 MS Outlook 2016 应用程序,它实现了某种警报,看起来与默认警报完全相同,但不是默认警报。它也会自动隐藏。所以甚至可能使用类似的方法
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-14
    • 2021-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多