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