Having trouble using Darwin Notifications

I'm trying to learn how to use Darwin Notifications, and I'm having trouble getting my callback function to trigger. I've been writing a command line tool just to learn how to get Darwin notifications working, but eventually will use them in an iPhone app. I have sample code below; can anyone with Darwin Notifications experience point out what am I doing wrong?

//callback

static void myCallback(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo)

{

NSLog(@"In callback function");

}

int main(int argc, const char * argv[]) {

// Add Observer

CFNotificationCenterAddObserver(

CFNotificationCenterGetDarwinNotifyCenter(), //center

NULL, //observer

myCallback, //callback

CFSTR("sanity_check"), //event name

NULL, //object

CFNotificationSuspensionBehaviorDeliverImmediately

);

// Post notification 1

CFNotificationCenterPostNotification(

CFNotificationCenterGetDarwinNotifyCenter(), // center

CFSTR("sanity_check"), // event name

NULL, //object

NULL, //userinfo dictionary

true);

// Post notification 2

notify_post("sanity_check");

return 0;

}

I'm not familiar with Darwin. But I would first look at how callBack is passed as argument.

See discussion here, with detailed eskimo's comments ; hope that helps (even though it is for Swift code):

https://developer.apple.com/forums/thread/71630

Note: When you post code, you should use code formatter and Paste and Match Style to avoid all extra lines:

//callback
static void myCallback(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo)
{
NSLog(@"In callback function");
}

int main(int argc, const char * argv[]) {
    
    // Add Observer
    CFNotificationCenterAddObserver(
        CFNotificationCenterGetDarwinNotifyCenter(), //center
        NULL, //observer
        myCallback, //callback
        CFSTR("sanity_check"), //event name
        NULL, //object
        CFNotificationSuspensionBehaviorDeliverImmediately
    );
    
    // Post notification 1
    CFNotificationCenterPostNotification(
        CFNotificationCenterGetDarwinNotifyCenter(), // center
        CFSTR("sanity_check"), // event name
        NULL, //object
        NULL, //userinfo dictionary
        true);
    
    // Post notification 2
    notify_post("sanity_check");
    
    return 0;
}

Have you checked the doc of CFNotificationCenterGetDarwinNotifyCenter?

Discussion

... As with distributed notifications, the main thread's run loop must be running in one of the common modes (usually kCFRunLoopDefaultMode) for Darwin-style notifications to be delivered.

Please try adding CFRunLoopRun(); at the end of your code:

int main(int argc, const char * argv[]) {
    // Add Observer
    CFNotificationCenterAddObserver(
        CFNotificationCenterGetDarwinNotifyCenter(), //center
        NULL, //observer
        myCallback, //callback
        CFSTR("sanity_check"), //event name
        NULL, //object
        CFNotificationSuspensionBehaviorDeliverImmediately
    );
    // Post notification 1
    CFNotificationCenterPostNotification(
        CFNotificationCenterGetDarwinNotifyCenter(), // center
        CFSTR("sanity_check"), // event name
        NULL, //object
        NULL, //userinfo dictionary
        true
    );
    // Post notification 2
    notify_post("sanity_check");
    
    CFRunLoopRun(); //<-
    return 0;
}
Having trouble using Darwin Notifications
 
 
Q