KD.
KD.

Reputation: 2055

Hide NSUserNotification after certain time

Currently when i create a NSUserNotification using Alert style it won't hide unless i manually close it.

enter image description here

Is there a way i can auto close/hide it say after 2 sec?

NSUserNotification code is for reference :

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)

Upvotes: 6

Views: 1950

Answers (3)

ElmerCat
ElmerCat

Reputation: 3155

It's actually very simple to do this, using NSObject's performSelector:withObject:afterDelay: method.

Because you're scheduling the notification delivery after a certain time interval, you need to add the additional delay before dismissing, to the initial delay before delivering. Here, I've written them out as constants of 10 seconds before delivery, and 2 seconds before dismissal:

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))

And for Swift 5 you can use the following:

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))

Upvotes: 6

Parag Bafna
Parag Bafna

Reputation: 22930

You can use removeDeliveredNotification: or removeAllDeliveredNotifications with timer

// 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 and later)

Upvotes: 1

Anoop Vaidya
Anoop Vaidya

Reputation: 46543

Blockquote Is there a way i can auto close/hide it say after 2 sec?

No, you do not have any such option till OSX 10.11, may be in future Apple may provide.

There are three ways a user can customise the NSUserNotification also known as Growl notification:

  1. None
  2. Banner
  3. Alert

You as a developer can not control over the system settings. This is upto the user to enable or disable and choose what kind of notification he likes.

If you want any alert to be shown to user you can create your own alert window and show it in that corner. You can set a timer to close, or provide action button to close it once you need.

Update 1: Cocoa provides NSWindow & NSPanel(HUD and normal panel). You can either customize Window or panel according to your need. Check there are multiple options that would help you to form as per your requirement.

If you can't get, say you wanted a rounded corner then you need to customize the window/view etc.

Upvotes: 0

Related Questions