Cocoa:在app最前面强制NSUserNotification

Elb*_*mio 4 cocoa delegates nsusernotification

相当简单,我有一个应该发送通知的应用程序,但如果应用程序是最前面的,它将不会这样做.从NSUserNotificationCenter类参考:

用户通知中心保留决定是否向用户呈现所传递的用户通知的权利.例如,如果应用程序已经位于最前面(委托可以覆盖此操作),它可能会禁止通知.

从我可以收集的内容来看,它与userNotificationCenter:shouldPresentNotification:NSUserNotificationCenterDelegate协议参考中显示的方法有关,但我无法弄清楚如何实现它.我已经将我的课程设置为NSUserNotificationCenterDelegate,所以不是这样.

有任何想法吗?

编辑:在我设置为NSUserNotificationCenterDelegate的类中,我使用它来尝试覆盖原始方法,如果应用程序位于最前面,则默认为NO:

- (BOOL)userNotificationCenter:(NSUserNotificationCenter *)center shouldPresentNotification:(NSUserNotification *)notification {
return YES;
}
Run Code Online (Sandbox Code Playgroud)

但没有任何反应.

小智 5

NSUserNotificationCenter*center = [NSUserNotificationCenter defaultUserNotificationCenter]; [中心时间表通知:通知]; center.delegate = self;

  • (BOOL)userNotificationCenter :( NSUserNotificationCenter*)center shouldPresentNotification:(NSUserNotification*)notification {return YES;}