如何使NSUserNotification超时?

Clo*_*ync 0 xcode cocoa notifications timeout objective-c

我正在使用这种方法:

NSUserNotification *notification = [[NSUserNotification alloc] init];
            notification.title = @"Title";
            notification.informativeText = @"body";
            notification.soundName = NSUserNotificationDefaultSoundName;

            [[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:notification];
Run Code Online (Sandbox Code Playgroud)

如何让它在3秒后超时?

Rog*_*Rog 5

将NSTimer设置为在三秒钟后触发,然后使用NSUserNotificationCenters removeDeliveredNotification删除通知.

例如,使用NSTimer +块来实现紧凑和清晰:

[NSTimer scheduledTimerWithTimeInterval:3.0 block:^
{
    [[NSUserNotificationCenter defaultUserNotificationCenter] removeDeliveredNotification: notification];
} repeats:NO];
Run Code Online (Sandbox Code Playgroud)

请注意,这不是使用该NSTimer类别的说明或建议 - API不是那么好:-)