有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

macos从Java应用程序访问UNUserNotificationCenter

我正在尝试从macOS上的JavaFX应用程序中访问UNUserNotificationCenter,以发送用户通知。为此,我使用jfa,它是一个围绕jna的包装器,与苹果基金会框架进行交互。这样,我的代码基本上如下所示:

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");

访问和加载UserNotifications框架工作正常,但是我无法获得currentNotificationCenter,因为此调用失败,出现以下错误:

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
)

我尝试在不同的时间访问通知中心,比如应用程序启动时,或者用户单击按钮以确保(希望)应用程序已完全加载时

然而,一切都导致了同样的错误。我在objective-c中的一个本地macOS应用程序中尝试了相同的代码,一切都很顺利

这是100%可复制的,所以我猜我在这里做错了什么,或者错过了一些必要的步骤。有人知道这是什么原因吗


共 (0) 个答案