2013-03-09 60 views
12

如何檢測用戶何時打開OS X Mountain Lion通知中心?如何檢測用戶何時打開OS X通知中心?

是否有NSNotification(呃,非常相似的術語不同的東西),我可以觀察?

+0

我最初認爲這可能是通過檢查'[[NSScreen mainScreen] visibleFrame]'來完成的,但這似乎不適用於通知中心抽屜(OS X 10.9)。 – pkamb 2015-02-16 23:08:26

+0

你有沒有找到解決這個問題的方法? – WCByrne 2017-05-02 04:31:06

回答

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似乎會改變。

相關問題