Show notification window from daemon

Hello,

I have a background daemon which is launched by launchd at system startup. I need to show a notification window to reports some event occationally. My snippet is as follows.


CFStringRef message = CFStringCreateWithCString(NULL, filePath, kCFStringEncodingUTF8);
    
CFUserNotificationDisplayNotice( 3.0f, kCFUserNotificationStopAlertLevel | kCFUserNotificationNoDefaultButtonFlag,
                                     NULL, NULL, NULL,
                                     CFSTR("alert"),
                                     message,
                                     NULL);
  
CFRelease(message);


The CFUserNotificationDisplayNotice returns 0, but no window shows up.

I cannot find much information about how to use this API.

Could anybody show me the correct way to show some UI from a daemon? Thanks!

Replies

I need to show a notification window to reports some event occationally.

Don’t do this. The best way to handle this is:

  • Have a launchd agent that runs in each GUI login session that can service notification requests from the daemon.

  • The agent is running in a GUI login session, so it has full access to all GUI facilities.

  • When the daemon wants to notify the user of something, it can consult with the list of connected agents and decide how to proceed.

  • You’ll need to decide on a policy for how the daemon should proceed if there are no agents connected.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"