Skip to content

Instantly share code, notes, and snippets.

@zhangkn
Created May 14, 2018 06:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zhangkn/7d776435fd56b01034b68bbd0c5adb2c to your computer and use it in GitHub Desktop.
Save zhangkn/7d776435fd56b01034b68bbd0c5adb2c to your computer and use it in GitHub Desktop.
CFUserNotificationRef CFUserNotificationCreate(CFAllocatorRef allocator, CFTimeInterval timeout, CFOptionFlags flags, SInt32 *error, CFDictionaryRef dictionary) {
CHECK_FOR_FORK();
CFUserNotificationRef userNotification = NULL;
SInt32 retval = ERR_SUCCESS;
static uint16_t tokenCounter = 0;
SInt32 token = ((getpid() << 16) | (tokenCounter++));
CFStringRef sessionID = (dictionary ? CFDictionaryGetValue(dictionary, kCFUserNotificationSessionIDKey) : NULL);
mach_port_t replyPort = MACH_PORT_NULL;
if (!allocator) allocator = __CFGetDefaultAllocator();
retval = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &replyPort);
if (ERR_SUCCESS == retval && MACH_PORT_NULL != replyPort) retval = _CFUserNotificationSendRequest(allocator, sessionID, replyPort, token, timeout, flags, dictionary);
if (ERR_SUCCESS == retval) {
userNotification = (CFUserNotificationRef)_CFRuntimeCreateInstance(allocator, CFUserNotificationGetTypeID(), sizeof(struct __CFUserNotification) - sizeof(CFRuntimeBase), NULL);
if (userNotification) {
userNotification->_replyPort = replyPort;
userNotification->_token = token;
userNotification->_timeout = timeout;
userNotification->_requestFlags = flags;
userNotification->_responseFlags = 0;
userNotification->_sessionID = NULL;
userNotification->_responseDictionary = NULL;
userNotification->_machPort = NULL;
userNotification->_callout = NULL;
if (sessionID) userNotification->_sessionID = CFStringCreateCopy(allocator, sessionID);
} else {
retval = unix_err(ENOMEM);
}
} else {
if (dictionary) CFUserNotificationLog(CFDictionaryGetValue(dictionary, kCFUserNotificationAlertHeaderKey), CFDictionaryGetValue(dictionary, kCFUserNotificationAlertMessageKey));
}
if (ERR_SUCCESS != retval && MACH_PORT_NULL != replyPort) mach_port_destroy(mach_task_self(), replyPort);
if (error) *error = retval;
return userNotification;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment