Objective-C 在 Libgdx 中初始化 UNNotificationAttachment class 时初始化方法返回 nil

Objective-C initialization method returned nil while initializing UNNotificationAttachment class in Libgdx

我正在尝试将图像附加到 Libgdx 项目的 iOS 模块中的通知。但是在初始化 UNNotificationAttachment 时出现以下错误

2020-08-26 08:09:56.711 IOSLauncher[29001:1089461] -[NSURL init] called; this results in an NSURL instance with an empty URL string. Please use one of the documented NSURL initialization methods instead (initWithString:, initFileURLWithPath:, etc.). This message shown only once.
2020-08-26 08:10:03.864 IOSLauncher[29001:1089573] XPC connection interrupted
SLF4J: Failed to load class "org.slf4j.impl.StaticMDCBinder".
SLF4J: Defaulting to no-operation MDCAdapter implementation.
SLF4J: See http://www.slf4j.org/codes.html#no_static_mdc_binder for further details.
Exception in thread "main" java.lang.RuntimeException: Objective-C initialization method returned nil
    at org.robovm.objc.ObjCObject.initObject(ObjCObject.java:108)
    at org.robovm.objc.ObjCObject.<init>(ObjCObject.java:97)
    at org.robovm.apple.foundation.NSObject.<init>(NSObject.java:136)
    at org.robovm.apple.usernotifications.UNNotificationAttachment.<init>(UNNotificationAttachment.java:54)
    at org.robovm.apple.usernotifications.UNNotificationAttachment.<init>(UNNotificationAttachment.java:51)
    at ua.gram.munhauzen.NotificationDelegate.scheduleNotification(NotificationDelegate.java:109)
    at ua.gram.munhauzen.IOSLauncher.showNotificaiton(IOSLauncher.java:414)
    at ua.gram.munhauzen.IOSLauncher.willResignActive(IOSLauncher.java:644)
    at com.badlogic.gdx.backends.iosrobovm.IOSApplication$Delegate.$cb$applicationWillResignActive$(IOSApplication.java)
    at org.robovm.apple.uikit.UIApplication.main(Native Method)
    at org.robovm.apple.uikit.UIApplication.main(UIApplication.java:446)
    at ua.gram.munhauzen.IOSLauncher.main(IOSLauncher.java:288)

正如错误行 1 明确指出的那样: “这会导致 NSURL 实例具有空 URL 字符串。请改用已记录的 NSURL 初始化方法之一(initWithString:, initFileURLWithPath:, ETC。)” 您可能在初始化 NSURL.

时遇到问题

下面是 UNNotificationAttachment 的工作示例:

    UNNotificationAttachment attachment = null;

    File file = new File(filePath);

    URL url = null;
    try {
        url = file.toURI().toURL();
    } catch (MalformedURLException e) {
        System.out.println("URL--------------------->" + url);
    }
    if (url != null) {
        NSURL nsURL = new NSURL(url);
        System.out.println("NSURL -------------" + nsURL);
        try {
            attachment = new UNNotificationAttachment("image", nsURL, null);
        } catch (NSErrorException e) {
            System.out.println("Attachment Error:----------------->" + e);
        }
        System.out.println("Attachment : " + attachment);
    }