3

I'm trying to access the UNUserNotificationCenter from within a JavaFX application on macOS to send user notifications. Therefor, I'm using jfa which is a wrapper around jna to interact with apples foundation framework. With this, my code basically looks like the following:

NSBundle nsBundle = NSBundle.bundleWithPath("/System/Library/Frameworks/UserNotifications.framework");
nsBundle.load();
ID unUserNotificationCenter = nsBundle.classNamed("UNUserNotificationCenter");
// This call fails:
ID currentNotificationCenter = Foundation.invoke(unUserNotificationCenter, "currentNotificationCenter");

Accessing and loading the UserNotifications frameworks is working well, however i cannot manage to get the currentNotificationCenter as this call fails with the following error:

2021-02-10 21:35:50.346 java[76301:37176563] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'bundleProxyForCurrentProcess is nil: mainBundle.bundleURL file:///Users/jan/.sdkman/candidates/java/11.0.8-amzn/bin/'
*** First throw call stack:
(
    0   CoreFoundation                      0x00007fff38d5cb57 __exceptionPreprocess + 250
    1   libobjc.A.dylib                     0x00007fff71ba15bf objc_exception_throw + 48
    2   CoreFoundation                      0x00007fff38d85d08 +[NSException raise:format:arguments:] + 88
    3   Foundation                          0x00007fff3b477ead -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 191
    4   UserNotifications                   0x00007fff468a7a66 __53+[UNUserNotificationCenter currentNotificationCenter]_block_invoke + 911
    5   libdispatch.dylib                   0x00007fff72cf0658 _dispatch_client_callout + 8
    6   libdispatch.dylib                   0x00007fff72cf17de _dispatch_once_callout + 20
    7   UserNotifications                   0x00007fff468a76d5 +[UNUserNotificationCenter currentNotificationCenter] + 101
    8   jna10020548276359543057.tmp         0x000000010c3c0e74 ffi_call_unix64 + 76
    9   ???                                 0x0000700008150ca8 0x0 + 123145437908136
)

I tried accessing the notification center at different times like when the application starts or e.g. when the user clicks a button to make sure that (hopefully) the application is completely loaded.

However, everything resulted in the same error. I tried the same code in a native macOS app in objective-c and everything worked well right away.

This is 100% reproducible, so I'm guessing I'm doing something wrong here or missing some required step. Does anyone maybe have an idea what may cause this?

Jan Gassen
  • 3,406
  • 2
  • 26
  • 44
  • Have you assigned a delegate per [these docs](https://developer.apple.com/documentation/usernotifications/unusernotificationcenter)? (Just guessing, but it reads like it is required for incoming notifications.) – Daniel Widdis Feb 11 '21 at 04:22
  • @DanielWiddis no, i haven't. But the `delegate` is an instance property, so I'd need to get the notification center instance first... – Jan Gassen Feb 11 '21 at 07:09
  • Have you tried any of the steps in the answer here? https://stackoverflow.com/questions/27842740/nsinternalinconsistencyexception-reason-could-not-load-nib-in-bundle – Daniel Widdis Feb 11 '21 at 18:38
  • I looked over the steps but they seem to be more related to loading nib files/story boards. Also, the error is a different one :/ – Jan Gassen Feb 12 '21 at 08:07
  • There actually is a related issue here https://stackoverflow.com/questions/46595434/ios-11-crashing-with-bundleproxy-nil-error-on-using-unusernotificationcenter but I cannot hook into the app delegate early enough from Java to capture the AppDelegate.didFinishLaunching message... – Jan Gassen Feb 12 '21 at 08:11

0 Answers0