Is it possible to use UNUserNotificationCenter from a LaunchAgent?

Hi, I have a LaunchAgent (whose .plist is in /Library/LaunchAgents/ as expected)

It is build as a single un-bundled binary, and installed away from regular user access at /Library/MyCompany/executables/

When I try to init a reference to UNUserNotificationCenter:

Code Block
UNUserNotificationCenter *unc = [UNUserNotificationCenter currentNotificationCenter];


I immediately have an exception, like thus:

Code Block
2021-05-02 19:57:39.297823+0300 agent[12504:225879] *** Assertion failure in +[UNUserNotificationCenter currentNotificationCenter], /AppleInternal/BuildRoot/Library/Caches/com.apple.xbs/Sources/UserNotifications/UserNotifications-281.6/UNUserNotificationCenter.m:44
2021-05-02 19:57:39.299464+0300 agent[12504:225879] [General] An uncaught exception was raised
2021-05-02 19:57:39.299518+0300 agent[12504:225879] [General] bundleProxyForCurrentProcess is nil: mainBundle.bundleURL file:///Users/mshneor/Library/Developer/Xcode/DerivedData/agent-dgvdlbtivmsjdgdkhhjwaxgrkyht/Build/Products/Debug/


Obviously the problem is bundleProxyForCurrentProcess is nil: mainBundle.bundleURL

Now... I could try to bundle my agent somehow, but that's a bit awkward, and I don't know how - it's not an App, and It's not a part of any App, and I don't know about a special "Code Bundle" type (like plugins etc.) that fits a LaunchAgent. Moreover - it will never be "launched" by it's info.plist in the bundle - only via the .plist in /Library/LaunchAgents by launchd.

So... is it possible to work around this? Could I somehow tell my Agent's process "Here is our main-bundle" at runtime, and provide a special bundle the agent is not embedded in?

what does UNUserNotificationCenter need from main bundle? maybe I can supply this programmatically in advance?

Any hint will be greatly appreciated.

Hi suMac,

Did you had any chance to find a solution ? I'm looking also on a way to do local user notifications from an agent.

Local notifications can be sent using the legacy (deprecated) NSUserNotification & NSUserNotificationCenter.

The main bundle doesn’t seems to be on the critical path for this API.

I’ll file a feature request for supporting LaunchAgents hosted in XPC Service with UNNotificationCenter.

In the meantime NSUserNotificationCenter can act as a workarround.

I did not find a way to manage UNNotifications from a LaunchAgent. My work-around solution is: I created a little "menu-bar app" with that tiny icon, that my launch-agent launches when it needs to communicate with user. I also made my LaunchAgent a mach-xpc-service with an NSXPCListener and created a proprietary protocol between the two. via XPC I convey information from the LaunchAgent to that app, which in turn creates the local notifications using UNNotificationCenter.

I could not use the older NSNotificationCenter, because I need many of the newer features of UNNotificationCenter (coalescing and grouping user notifications, customizing their UI, adding user-options etc.)

The important thing is, when you "click" a notification in the user-notification-center, MacOS launches the App that created and sent that notification (my menu-bar app) - and sends it a delegate call with lots of information, to continue the dialog with the User.

A LaunchAgent like mine, has very dull UI and most of the time it doesn't "remember" the user's state, so in my case it is cumbersome, but acceptable solution.

Feature request is sent as FB9963670

Is it possible to use UNUserNotificationCenter from a LaunchAgent?
 
 
Q