3

I want to have push notifications in Mac OS style in my JavaFX project, which turned out to be actually really difficult.

I was following the approach described in the accepted answer on: Using 10.8 Notification Center in Java

I cloned the linked github repo. From the Xcode project I exported the .dylib file and moved it to /usr/local/lib. I took over the java interface to my project.

interface NsUserNotificationsBridge extends Library {
    NsUserNotificationsBridge instance = (NsUserNotificationsBridge)
            Native.loadLibrary("/usr/local/lib/NsUserNotificationsBridge.dylib", NsUserNotificationsBridge.class);

    public int sendNotification(String title, String subtitle, String text, int timeoffset);
}

Using INsUserNotificationsBridge.instance.sendNotification(params...) I am now able to send push notifications!

MacOS Notification when running my project with IntelliJ

This even works when running straight from the IDE (IntelliJ). In contradiction to the author's note that

If your java application is not in an app bundle then notifications will not be delivered.

That wonders me already. I build a single JAR artifact. Running the jar: everything is fine, the push notification is showing. Bundling it into an app: All fine, but no push notification! Also no error. So the very opposite of what the author wrote happened!

(See my Info.plist)

What did I do wrong? Why is it not showing when starting as .app? Is there maybe meanwhile an even better way to do MacOS push notifications with java? I already know about the way of executing an applescript but I don't like its behavior.

I'm on Java 9 btw.

primarykey123
  • 137
  • 1
  • 9

0 Answers0