7

I'm still quite new to this but I've been learning really quickly with the help of examples. I'm currently looking into posting notifications from one running program to another, and CFNotificationCenter is the way forward. The only problem is, I can't work out to use it and there don't seem to be any examples except for apple's videoviewer.

Would anybody be able to supply a mini example on how to set it up so I can write one application to post the notification, and one to receive the test notification and doSomething();? Any help is greatly appreciated!

1
  • 1
    Do you want an example on using CFNotificationCenter, or NSNotificationCenter? CFNotificationCenter is a CoreFoundation equivalent to NSNotificationCenter; they do the same thing. There is already an example of NSNotificationCenter on Stackoverflow that might help you out. Commented Aug 6, 2011 at 21:33

1 Answer 1

10

Alright, I wrote up a little example of CFNotificationCenter. Generally, nobody uses CoreFoundation for large projects, and instead use Foundation. If you are really writing this project in Objective-C (as I assume from your tags), I would suggest using NSNotificationCenter. Without further adue, here is the example:

#include <CoreFoundation/CoreFoundation.h>

void notificationCallback (CFNotificationCenterRef center,
                           void * observer,
                           CFStringRef name,
                           const void * object,
                           CFDictionaryRef userInfo) {
    CFShow(CFSTR("Received notification (dictionary):"));
    // print out user info
    const void * keys;
    const void * values;
    CFDictionaryGetKeysAndValues(userInfo, &keys, &values);
    for (int i = 0; i < CFDictionaryGetCount(userInfo); i++) {
        const char * keyStr = CFStringGetCStringPtr((CFStringRef)&keys[i], CFStringGetSystemEncoding());
        const char * valStr = CFStringGetCStringPtr((CFStringRef)&values[i], CFStringGetSystemEncoding());
        printf("\t\t \"%s\" = \"%s\"\n", keyStr, valStr);
    }
}

int main (int argc, const char * argv[]) {
    CFNotificationCenterRef center = CFNotificationCenterGetLocalCenter();
    // add an observer
    CFNotificationCenterAddObserver(center, NULL, notificationCallback, 
                                    CFSTR("MyNotification"), NULL, 
                                    CFNotificationSuspensionBehaviorDeliverImmediately);
    // post a notification
    CFDictionaryKeyCallBacks keyCallbacks = {0, NULL, NULL, CFCopyDescription, CFEqual, NULL}; 
    CFDictionaryValueCallBacks valueCallbacks  = {0, NULL, NULL, CFCopyDescription, CFEqual};
    CFMutableDictionaryRef dictionary = CFDictionaryCreateMutable(kCFAllocatorDefault, 1, 
                                                                  &keyCallbacks, &valueCallbacks);
    CFDictionaryAddValue(dictionary, CFSTR("TestKey"), CFSTR("TestValue"));
    CFNotificationCenterPostNotification(center, CFSTR("MyNotification"), NULL, dictionary, TRUE);
    CFRelease(dictionary);
    // remove oberver
    CFNotificationCenterRemoveObserver(center, NULL, CFSTR("TestValue"), NULL);
    return 0;
}

This example creates an observer, posts a simple dictionary to it, and removes the observer. More information on CFNotificationCenter can be found on Apple's CFNotificationCenter Reference.

3
  • Thank you, these bits and pieces are exactly what I was looking for and now I understand how it works. I'm doing this between two apps so I've used CFNotificationCenterGetDistributedCenter instead of the local one. But otherwise, thanks!
    – Tiago
    Commented Aug 6, 2011 at 22:40
  • 1
    When I cut/paste this into XCode 7.1, it won't compile and says "Subscript of pointer to incomplete type 'const void'" on the &keys[i] and &values[i] of the notificationCallback() function.
    – Volomike
    Commented Mar 31, 2016 at 16:06
  • sorry to dig up, but I'm challenging this answer because the doc said The observer. This value must not be NULL. for CFNotificationCenterRemoveObserver
    – Wingzero
    Commented Oct 26, 2018 at 6:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.