在 10.7.2 上,我无法让标准 NSDistributedNotifications 开箱即用。
即使回到(完整的 XCode 版本 https://github.com/dirkx/Example-NSDistribtuedNotification-Failing ),我得到如下简单的内容:
- “本地”通知工作出色(如 NSNotificationCenter),但是
- 当我启动应用两次(例如从命令行)时,没有应用间通信
- 当其他应用为此(或无)注册时不会收到通知
- 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) 不会显示通知 - 但通知会在应用内处理。
最佳答案
事实证明,当没有其他原因导致 CFRunLoop
返回时,消息会无限期地排队。这似乎是设计使然。
我找到了三种解决方法 - 没有一个那么好 - 它们都涉及
- 在发布中添加
deliverImmediately:YES
, - a
deliverImmediately:
withNSNotificationSuspensionBehaviorDeliverImmediately
给观察者或 - 设置一个计时器,有意每秒中断一次 runLoop。
显然 - 这些都不便宜。
深度
关于xcode - NSDistributedNotifications 未在(相同)应用程序的实例之间分发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9140305/