Hide NSUserNotification after a certain time

Currently, when I create NSUserNotification using the Alert style, it will not be hidden unless I manually close it.

enter image description here

Is there a way that I can automatically close / hide it after 2 seconds?

NSUserNotification code 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)
+4
source share
3 answers

This is actually very simple to do using NSObject performSelector:withObject:afterDelay:.

Since you plan to deliver notifications after a certain period of time, you need to add an additional delay before leaving before the initial delay before delivery. Here I wrote them as constants 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))
+4

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 )

+1

Blockquote , / 2 ?

, OSX 10.11, , Apple .

NSUserNotification, Growl:

  • None

, , . , , .

, - , . , , .

Update 1: Cocoa provides NSWindowboth NSPanel(HUD and normal dashboard). You can customize the window or panel to suit your needs. Check if there are several options that will help you shape as per your requirement.

If you can't get it, say you need a rounded corner, you need to adjust the window / view, etc.

0
source

Source: https://habr.com/ru/post/1621199/


All Articles