【问题标题】:NSUserNotificationCenter delegate not work when the app is not focus当应用程序没有焦点时,NSUserNotificationCenter 委托不起作用
【发布时间】:2014-01-17 15:09:03
【问题描述】:

我有这样的代码。

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

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

有什么帮助吗?

【问题讨论】:

    标签: macos cocoa nsusernotificationcenter


    【解决方案1】:

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

    其次,在documentation for -userNotificationCenter:shouldPresentNotification: 中,它声明:

    userNotificationCenter:shouldPresentNotification:

    当用户通知中心决定不展示您的通知时发送给委托人。

    即您将NSUserNotificationCenter 已决定显示您的通知时看到调用此方法。如果它决定应该显示它,它就不会费心询问。

    【讨论】:

    • 谢谢,我刚刚找到了这个答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-02-10
    • 2020-10-02
    • 1970-01-01
    • 2023-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多