找到你要的答案

Q:How to observe notifications from a different application?

Q:如何观察来自不同应用程序的通知?

I'd like to get notified when a certain application triggers an event. I'm not an Objective-C developer nor do I know the OS X APIs—so I hope this question is not too basic.

My goal is to write the meta info of the currently playing song to a log file. For iTunes, I got this working with the following Objective-C line:

[[NSDistributedNotificationCenter defaultCenter]
 addObserver: myObserver selector: @selector(observeNotification:)
 name: @"com.apple.iTunes.playerInfo" object:nil];

However, I also need this for AirServer (which is an software AirPlay receiver). Unfortunately, the following doesn't work—the observer never gets called:

[[NSDistributedNotificationCenter defaultCenter]
 addObserver: myObserver selector: @selector(observeNotification:)
 name: @"com.pratikkumar.airserver-mac" object:nil];

So apparently, AirServer does not send these type of notifications. However, there's a notification in the Notification Center when a new song starts playing.

My next step would be to periodically check for new notifications in the OS X Notification Center (as described here: http://stackoverflow.com/a/25930769/1387396). This is not too clean though—so my questions is: is there another option in this particular case?

当某个应用程序触发事件时,我希望得到通知。我不是一个Objective-C的开发者也不知道OS X的API,所以我希望这个问题不太基本了。

我的目标是将当前播放歌曲的元信息写入日志文件。对于iTunes,我用下面的Objective-C线工作:

[[NSDistributedNotificationCenter defaultCenter]
 addObserver: myObserver selector: @selector(observeNotification:)
 name: @"com.apple.iTunes.playerInfo" object:nil];

然而,我也需要这airserver(这是一个软件AirPlay接收器)。不幸的是,以下是不工作的观察员从未得到调用:

[[NSDistributedNotificationCenter defaultCenter]
 addObserver: myObserver selector: @selector(observeNotification:)
 name: @"com.pratikkumar.airserver-mac" object:nil];

很显然,airserver不发送这些类型的通知。然而,有一个通知在通知中心时,新的歌曲开始播放。

我的下一步将定期检查在OS X的通知中心新的通知(如下所述:HTTP:/ /计算器。COM /一/ 25930769 / 1387396)。这不是太干净,但我的问题是:在这个特殊的情况下,还有其他选择吗?

answer1: 回答1:

Firstly, you have to understand that while the NSDistributedNotificationCenter has the word Notification in it; it's not related. From the About Local Notifications and Remote Notifications, it does state:

Note: Remote notifications and local notifications are not related to broadcast notifications (NSNotificationCenter) or key-value observing notifications.

So, at that point, I'm going to answer from the perspective of NSDistributedNotificationCenter, and not about Remote/Local notifications - you already have a potential solution in the answer you linked for observing the DB file that contains a record of the notifications in that way.

The first thing you need to do is listen for the correct notification. Creating a simple application that listens for all events; e.g. using:

NSDistributedNotificationCenter *center = [NSDistributedNotificationCenter defaultCenter];
[center addObserver:self
           selector:@selector(notificationEvent:)
               name:nil
             object:nil];


-(void)notificationEvent:(NSNotification *)notif {
    NSLog(@"%@", notif);
}

It indicates that the notifications are:

__CFNotification 0x6100000464b0 {name = com.airserverapp.AudioDidStart; object = com.pratikkumar.airserver-mac; userInfo = {
    ip = "2002:d55e:dbb2:1:10e0:1bfb:4e81:b481";
    remote = YES;
}}
__CFNotification 0x618000042790 {name = com.airserverapp.AudioDidStop; object = com.pratikkumar.airserver-mac; userInfo = {
    ip = "2002:d55e:dbb2:1:10e0:1bfb:4e81:b481";
    remote = YES;
}}

This indicates that the name parameter in the addObserver call should be com.airserverapp.AudioDidStart and com.airserverapp.AudioDidStop.

You can use code like that to determine all notifications, which will allow you to add the relevant observers when you want the particular observer. This is probably the easiest way to observe notifications of this type.

首先,你必须明白,而nsdistributednotificationcenter有字的通知;不相关的。从本地通知和远程通知,它的状态:

注:远程通知和地方通知没有广播通知相关(NSNotificationCenter)或键值观察通知。

所以,在这一点上,我要回答的nsdistributednotificationcenter的角度,而不是远程/本地通知你在回答你联系的观察,包含了这样的通知记录的数据库文件已经有一个潜在的解决方案。

你需要做的第一件事是倾听正确的通知。创建一个简单的应用程序,监听所有事件;例如使用:

NSDistributedNotificationCenter *center = [NSDistributedNotificationCenter defaultCenter];
[center addObserver:self
           selector:@selector(notificationEvent:)
               name:nil
             object:nil];


-(void)notificationEvent:(NSNotification *)notif {
    NSLog(@"%@", notif);
}

它表明通知是:

__CFNotification 0x6100000464b0 {name = com.airserverapp.AudioDidStart; object = com.pratikkumar.airserver-mac; userInfo = {
    ip = "2002:d55e:dbb2:1:10e0:1bfb:4e81:b481";
    remote = YES;
}}
__CFNotification 0x618000042790 {name = com.airserverapp.AudioDidStop; object = com.pratikkumar.airserver-mac; userInfo = {
    ip = "2002:d55e:dbb2:1:10e0:1bfb:4e81:b481";
    remote = YES;
}}

这表明在addObserver呼叫名称参数应com.airserverapp.audiodidstart和com.airserverapp.audiodidstop。

您可以使用这样的代码来确定所有通知,这将允许您添加相关的观察员时,您需要特定的观察员。这可能是观察这种类型通知的最简单方法。

objective-c  osx  cocoa  foundation  airserver