Using 10.8 Notification Center in Java

孤者浪人 提交于 2019-11-30 04:48:49
Petesh

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.

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.

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

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!