I've been observing an annoying phenomenom with NSUserNotificationCenter/NSUserNotification.
I have 2 processes:
- procD: a launchd daemon launched at startup
- procA: a launchd agent launched at login
. procA can also be launched by procD using a watch path.
. procD communicates through XPC with procA using an anonymousListener endpoint provided by procA.
. procD can send a message (MSG) to procA through XPC. The message in procA delivers a NSUserNotification.
What I've been observing:
Case that works:
procD is running.
procA is running and has been launched automatically after login.
1. procD sends MSG to procA. procA receives the message, uses the NSUserNotification API to deliver a user notification: it works fine.
Step 1 can be repeated again and again and it works fine in every single case.
Case that does not work:
procD is running.
procA is running and has been launched automatically after login.
1. procA is killed (via Activity Monitor for instance).
2. procD triggers the launch of procA using the watch path => procA is launched.
3. procD sends MSG to procA. procA receives the message, uses the NSUserNotification API to deliver a user notification
=> the user notification does not appear. It i reported as delivered to the NSUserNotificationCenter delegate. Uh?!
4. procD sends MSG to procA. procA receives the message, uses the NSUserNotification API to deliver a user notification
=> the notification does appear.
It's as if delivering a user notification following a kill of the process is not working. I have added a NSBeep() before delivering the notification and it's played. So the issue really is with the user notification code and not the messaging code.
Workaround that I've found so far:
I call [[NSUserNotificationCenter defaultUserNotificationCenter] removeAllDeliveredNotifications] in the + (void)initialize method of the class I use to call the NSUserNotification API and deliver the notifications.
Doing this enables the first user notification to be displayed as it should.
Question:
Could it be that there is a bug in the NSUserNotificationCenter/NSUserNotification API/server where some cached data remains after a process has been killed?
I've read that these APIs are deprecated but they are the only ones that work on macOS < 10.14 which is still supported here.
macOS 10.14.6 / Objective-C code