Darwin Notification

Darwin Notification

  • 跨进程间消息收发
  • 接收到系统发出的消息

发送通知:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/* This function is called when a notification is received. */
void MyNotificationCenterCallBack(CFNotificationCenterRef center,
void *observer,
CFStringRef name,
const void *object,
CFDictionaryRef userInfo)
{
printf("Notification center handler called\n");
}

...

/* Create a notification center */
CFNotificationCenterRef center = CFNotificationCenterGetDarwinNotifyCenter();

/* Tell notifyd to alert us when this notification
is received. */
if (center) {
CFNotificationCenterAddObserver(center,
NULL,
MyNotificationCenterCallBack,
CFSTR("org.apache.httpd.configFileChanged"),
NULL,
CFNotificationSuspensionBehaviorDeliverImmediately);
...
}

// Post
CFNotificationCenterPostNotification(center, CFSTR("Notification Name"), NULL, NULL, TRUE);

参考

钟颖 Cyan知乎专栏
Apple doc
开源项目