2

On 10.7.2 I have trouble getting standard NSDistributedNotifications to work out of the box.

Even when brought back to just (Full XCode version at https://github.com/dirkx/Example-NSDistribtuedNotification-Failing) to something as simple as below I get:

  1. Splendidly working notifications 'locally' (Like with a NSNotificationCenter) but
  2. No inter-app comms when I start the app up twice (e.g. from the command line)
  3. No notifications when another apps registers for this (or for nill)
  4. No debug/info in the distnoted daemon logs either.

What am I missing ?

 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];

 }

As an aside - notification watcher (https://github.com/melo/notification-watcher) does not show the Notifiactions - but the notifications are processed within the app.

cacau
  • 3,606
  • 3
  • 21
  • 42
Dirk-Willem van Gulik
  • 7,566
  • 2
  • 35
  • 40
  • What does the code where you register for the notification look like? – sbooth Feb 04 '12 at 12:37
  • That is the function in - (void)applicationDidFinishLaunching:(NSNotification *)aNotification in above (or see the link for the actual git up https://github.com/dirkx/Example-NSDistribtuedNotification-Failing/blob/master/SayHi/SayHi/AppDelegate.m - line 56 and beyond). – Dirk-Willem van Gulik Feb 04 '12 at 13:32
  • Thanks, I guess I overlooked that earlier. – sbooth Feb 04 '12 at 14:30

1 Answers1

0

Turns out that when there are no other reasons for CFRunLoop to return - the messages are queued indefinitely. This seems by design.

I found three work arounds for this - none of which is that nice - they both involve

  1. adding a deliverImmediately:YES to the posting,
  2. a deliverImmediately: with NSNotificationSuspensionBehaviorDeliverImmediately to the observer or
  3. setting a timer to intentionally interrupt the runLoop every seconds.

Obviously - none of this comes cheap.

Dw

cacau
  • 3,606
  • 3
  • 21
  • 42
Dirk-Willem van Gulik
  • 7,566
  • 2
  • 35
  • 40