Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can JavaFX natively show OS notifications?

AWT's TrayIcon class has a method called displayMessage that shows a native OS message that in Windows 10 looks like this:

enter image description here

as a pop up and like this:

enter image description here

in the notification area.

Can JavaFX do this natively? I know JavaFX doesn't implement traybar support yet and one has to use AWT, but are these notifications traybar dependent?

like image 529
pupeno Avatar asked Dec 12 '17 01:12

pupeno


Video Answer


3 Answers

For anyone coming to this question in 2020, here's a stab at showing native OS notifications working with JavaFX:

https://gist.github.com/wiverson/d2edf0d66ad195c96793d0d25290753b

As noted in the sample, OS native notifications are best if the app is in the background - use ControlsFX notifications if the app is in the foreground.

This works on macOS Big Sur, should also work on Windows.

[Edit 1/5/21] Here is a project that also will help with this:

https://github.com/dustinkredmond/FXTrayIcon

like image 127
Will Iverson Avatar answered Oct 28 '22 11:10

Will Iverson


Apparently javaFx Still doesn't provide way to show tray notifications, but you can use 3rd party library to achieve your goal .

TrayNotification

    String title = "Congratulations sir";
    String message = "You've successfully created your first Tray Notification";

    Notification notification = Notifications.SUCCESS;
    TrayNotification tray = new TrayNotification(title, message, notification);
    tray.showAndWait();

enter image description here

* * * * * * * *

ControlsFX

Notifications.create()
              .title("Title Text")
              .text("Hello World 0!")
              .showWarning();

enter image description here

like image 41
aKilleR Avatar answered Oct 28 '22 11:10

aKilleR


you don't need any 3drparti libraries.

you can use osascript and display in the mac.

// java version
Runtime.getRuntime().exec(new String[] { "osascript", "-e", "display notification \"Message\" with title \"Title\"" });

// koltin version
Runtime.getRuntime().exec(arrayOf("osascript", "-e", "display notification \"Message\" with title \"Title\""))
like image 34
Rasoul Miri Avatar answered Oct 28 '22 13:10

Rasoul Miri