Deprecated: trim(): Passing null to parameter #1 ($string) of type string is deprecated in /var/www/html/extensions/Variables/includes/ExtVariables.php on line 198
CFNotificationCenter - iPhone Development Wiki

CFNotificationCenter

From iPhone Development Wiki
Revision as of 23:07, 10 April 2014 by BtTroubles (talk | contribs) (Added info on registering for CFNotificationCenter callbacks)

CFNotificationCenter is an object representing a local or remote notification center, which is a singleton object of its type that receives notifications from different sources, and distributes them to the listeners.

Great way to determine various PRIVATE events that may want to subscribe to

Add the observer below in your app startup code and implement the callback handler below. Then perform actions on your phone or other apps and log the events you may be interested in.

    CFNotificationCenterAddObserver(CFNotificationCenterGetLocalCenter(),
                                    NULL,
                                    notificationCallback,
                                    NULL,
                                    NULL,  
                                    CFNotificationSuspensionBehaviorDeliverImmediately);

And the callback:

void notificationCallback(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo)
{
     NSLog(@"Callback detected: \n\t name: %@ \n\t object:%@", name, object);
}

Hat tip: http://stackoverflow.com/questions/3725234/nsnotificationcenter-trapping-and-tracing-all-nsnotifications/3738387#3738387

Look up all observers of the local center

You can get the dictionary of all observers in the local center by doing this:

CFNotificationCenterRef center = CFNotificationCenterGetLocalCenter();
CFDictionaryRef dict = *(CFDictionaryRef*)(center + 4);
CFShow(dict);

Post notification with objects

DistributedCenter has been silently available since iOS 5. It can send property list objects(NSArray, NSDictionary, NSNumber, NSString, NSDate and NSData) to other processes.

// First, define function as external.
extern CFNotificationCenterRef CFNotificationCenterGetDistributedCenter(void);

// Sender.
CFMutableDictionaryRef dictionary = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
CFDictionaryAddValue(dictionary, @"key of string", @1);
CFNotificationCenterPostNotification(CFNotificationCenterGetDistributedCenter(), CFSTR("notification.identifier"), @"string object", dictionary, true);
CFRelease(dictionary);

...

// Call back function.
static void CallBackFunction(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo)
{
    ...
}

// Registration in receiver process.
CFNotificationCenterAddObserver(CFNotificationCenterGetDistributedCenter(),
    NULL,
    CallBackFunction,
    CFSTR("notification.identifier"),
    NULL,
    CFNotificationSuspensionBehaviorDeliverImmediately);

You can check if DistributedCenter can be used:

void *handle = dlopen("/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation", RTLD_LAZY);
void *impl = NULL;
if (handle)
    impl = dlsym(handle, "CFNotificationCenterGetDistributedCenter");
if (impl) {
    // Available.
}

References