20

this is just a general question. I'm not having an actual project or something else where I want to do this. Is it possible to build an App in Java and add some framework stuff or something else and use Notication Center?

4 Answers 4

16
+50

I wrote a simple C -> Objective-C shim .dylib that exported a function showNotification That delivered a notification immediately.

Then I wrote a JNA wrapper to the function exported in the dylib. I'm sure that you could use a combination of Rococoa and JNAerator to eliminate this dylib altogether.

Then I wrote a simple java application that I turned into an app bundle. This is the important point. If your java application is not in an app bundle then notifications will not be delivered.

Messages delivered to the notification center.

Screenshot of delivered notification

It's eminently doable, you just have to play along with the rules of the NSUserNotificationCenter - it must be in an app bundle.

I used java 7, update 9 for this. Another question seems to have had even more success.

The code used to build this simple application is available on github. It contains an xcode project for creating the .dylib, and a netbeans project that makes use of the .dylib to deliver notifications.

2
  • 1
    I've updated the answer with a link to the relevant code. It's not complicated in the least and is only intended as sample code. Jan 12, 2013 at 19:54
  • See this answer for how to send notifications without an app bundle. Aug 26, 2014 at 1:07
12

I' ve found a solution that might work for you, you can use Apple script to show notifications on osx, try this:

Runtime.getRuntime().exec(new String[] { "osascript", "-e", "display notification "This is a message" with title "Title" subtitle "Subtitle" sound name "Funk"" });

You can read about Apple script's notifications in here

2
  • This is actually the most simple way i found to do this, works on el capitan without an app bundle for me Sep 28, 2017 at 4:23
  • This just works. Excellent solution!
    – suomi35
    Jan 12, 2023 at 6:11
5

One possible solution will be Growl when the App will be updated to Growl 2.0 which will forward Growl Notifications to the Notification Center.

Informations about Growl 2

Growl Messages can be forwarded with the current Versions by using MoutainGrowl

Growl can be used under Java with libgrowl or jgntp

Maybe it's not the cleanest solution and it has some requirements to the user but it's definitely one that works

4

I suggest you take a look at how IntelliJ IDEA handles notifications in MountainLionNotifications and GrowlNotifications.

As suggested in other answers, it uses JNA to manipulate NSObjects.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.