一定时间后隐藏 NSUserNotification
Hide NSUserNotification after certain time
目前,当我使用警报样式创建 NSUserNotification 时,除非我手动关闭它,否则它不会隐藏。
有没有办法让我在 2 秒后自动 close/hide 它说?
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 Is there a way i can auto close/hide it say after 2 sec?
不,在 OSX 10.11 之前你没有任何这样的选择,可能在未来 Apple 可能会提供。
用户可以通过三种方式自定义 NSUserNotification
也称为 Growl 通知:
- None
- 横幅
- 警报
您作为开发人员无法控制系统设置。这取决于用户启用或禁用以及选择他喜欢的通知类型。
如果您希望向用户显示任何警报,您可以创建自己的警报 window 并将其显示在那个角落。您可以设置一个计时器来关闭,或提供操作按钮以在需要时关闭它。
更新 1:
Cocoa 提供 NSWindow
& NSPanel
(HUD 和普通面板)。您可以根据需要自定义 Window 或面板。检查是否有多个选项可以帮助您根据您的要求形成。
如果你不能得到,说你想要一个圆角然后你需要自定义 window/view 等
您可以使用 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 及更高版本)
其实做起来很简单,用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))
目前,当我使用警报样式创建 NSUserNotification 时,除非我手动关闭它,否则它不会隐藏。
有没有办法让我在 2 秒后自动 close/hide 它说?
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 Is there a way i can auto close/hide it say after 2 sec?
不,在 OSX 10.11 之前你没有任何这样的选择,可能在未来 Apple 可能会提供。
用户可以通过三种方式自定义 NSUserNotification
也称为 Growl 通知:
- None
- 横幅
- 警报
您作为开发人员无法控制系统设置。这取决于用户启用或禁用以及选择他喜欢的通知类型。
如果您希望向用户显示任何警报,您可以创建自己的警报 window 并将其显示在那个角落。您可以设置一个计时器来关闭,或提供操作按钮以在需要时关闭它。
更新 1:
Cocoa 提供 NSWindow
& NSPanel
(HUD 和普通面板)。您可以根据需要自定义 Window 或面板。检查是否有多个选项可以帮助您根据您的要求形成。
如果你不能得到,说你想要一个圆角然后你需要自定义 window/view 等
您可以使用 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 及更高版本)
其实做起来很简单,用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))