2

在 10.7.2 上,我无法让标准 NSDistributedNotifications 开箱即用。

即使回到只是(https://github.com/dirkx/Example-NSDistribtuedNotification-Failing上的完整 XCode 版本)到如下简单的东西我得到:

  1. 出色的“本地”通知(就像 NSNotificationCenter 一样)但是
  2. 当我启动应用程序两次时没有应用程序间通信(例如从命令行)
  3. 当其他应用程序注册此(或为零)时没有通知
  4. distnoted 守护程序日志中也没有调试/信息。

我错过了什么?

 NSString * kSayNotification = @"org.webweaving.sayExample";

 // Send a Distributed Notification on button press.
 //
 -(IBAction)buttonChange:(NSButton *)sender {
    NSString * str = (sender.state == NSOnState) ? @"Yes" : @"No";
    [[NSDistributedNotificationCenter defaultCenter] 
            postNotificationName:kSayNotification 
                          object:str
     ];
 }

 // Update a label on receiving a Notification. 
 //
 -(void)notif:(NSNotification *)nf {
    .. snipped time string ...

    // Textfield with the time of arrival and the value passed in the notification.
    //
    textField.stringValue = [NSString stringWithFormat:@"%@: %@", 
                              dStr, (NSString *)nf.object
                            ];
 }

 - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
 {
    // Register for the notifications.
    //
    [[NSDistributedNotificationCenter defaultCenter] 
        addObserver:self 
           selector:@selector(notif:) 
               name:kSayNotification
             object:nil];

 }

顺便说一句 - 通知观察者(https://github.com/melo/notification-watcher)不显示通知 - 但通知在应用程序内处理的。

4

1 回答 1

0

事实证明,当没有其他理由CFRunLoop返回时 - 消息无限期地排队。这似乎是设计使然。

我为此找到了三个解决方法——没有一个很好——它们都涉及

  1. deliverImmediately:YES在帖子中添加一个,
  2. deliverImmediately:观察者NSNotificationSuspensionBehaviorDeliverImmediately
  3. 设置一个计时器以故意每隔几秒中断一次 runLoop。

显然——这些都不便宜。

于 2012-02-07T15:06:42.127 回答