如何检测用户何时打开OS X通知中心?

时间:2013-03-09 22:05:17

标签: macos cocoa

如何检测用户何时打开OS X Mountain Lion通知中心?

我能观察到的NSNotification(呃,非常相似的术语)我可以观察到吗?

1 个答案:

答案 0 :(得分:0)

我不知道任何官方记录的解决方案或通知(让我知道!),但是当我测试它时,这似乎有效(至少在OS X 10.10上),只要我的应用程序在前景/我相信最前面的窗口。

以观察者身份添加对象:

[[NSDistributedNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationCenterOpened:) name:@"com.apple.HIToolbox.beginMenuTrackingNotification" object:nil];

[[NSDistributedNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationCenterClosed:) name:@"com.apple.HIToolbox.endMenuTrackingNotification" object:nil];

向对象添加与以下内容类似的方法,确保检查正确的ToolboxMessageEventData号码(4927),例如:

- (void)notificationCenterOpened:(NSNotification*)notification {
    if ([notification.userInfo[@"ToolboxMessageEventData"] isEqual: @4927]) {
        NSLog(@"Notification center opened");
    }
}

- (void)notificationCenterClosed:(NSNotification*)notification {
    if ([notification.userInfo[@"ToolboxMessageEventData"] isEqual: @4927]) {
        NSLog(@"Notification center closed");
    }
}

让我知道这对你有用还是不对。

没关系 - 重启/注销+重新登录后,ToolboxMessageEventData似乎发生了变化。