NSUserNotificationCenter

NSUserNotificationCenter

我有这样的代码。

[[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:notification];
[[NSUserNotificationCenter defaultUserNotificationCenter] setDelegate:self];


另外,我实现了userNotificationCenter:shouldPresentNotification:。它工作正常。然而。当应用程序的窗口未聚焦时,不会调用此委托方法,并且通知始终存在。

有什么帮助吗?

最佳答案

首先,您应该在发送-deliverNotification:消息之前设置委托。如果您不这样做,则即使原本应该的,委托方法也可能不会被触发。

第二,在documentation for -userNotificationCenter:shouldPresentNotification:中,它指出:


  userNotificationCenter:应该存在的通知:
  
  当用户通知中心决定不显示您的通知时,发送给代表。


即,仅当NSUserNotificationCenter已决定不显示您的通知时,您才会看到此方法被调用。如果它决定显示它,就不会问。

关于macos - 当应用程序未关注时,NSUserNotificationCenter委托(delegate)不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20844497/

10-14 23:44