How to add actions in Windows 10 Toast content notifications with Java - java

I'd like to customize a notification message in Windows 10 by adding two buttons in order to trigger two different actions. Microsoft toast content tutorial does exactly what I want, but the examples are in C# (which I've never used) and XML which I'm a bit familiar with. Is there a way to achieve the same result in pure java? Or alternatively how can i combine java and xml?
Here is an example of what I want, directly taken from the previously referenced tutorial.
At the moment I have managed to create a simple notification icon but as you can see from the output the are no actions.
public static void notify() throws AWTException{
SystemTray tray = SystemTray.getSystemTray();
Image image = Toolkit.getDefaultToolkit().createImage("");
TrayIcon trayIcon = new TrayIcon(image);
trayIcon.setImageAutoSize(true);
trayIcon.setToolTip("Demo");
tray.add(trayIcon);
trayIcon.displayMessage("Hello, World", "notification demo", TrayIcon.MessageType.INFO);
}

Related

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:
as a pop up and like this:
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?
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();
*
*
*
*
*
*
*
*
ControlsFX
Notifications.create()
.title("Title Text")
.text("Hello World 0!")
.showWarning();
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\""))
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

Notifications on Windows 10 from Java

I'm creating a Java program which pushes notifications and I want to remove or edit the line which says "Java(TM) Platform SE binary".
Is this possible? I searched on google but I couldn't see info about this.
Here is the related code. The last line is the line that pushes the notification.
public void mostrarNotificacion(Usuario user) throws AWTException, java.net.MalformedURLException, IOException {
//Obtain only one instance of the SystemTray object
SystemTray tray = SystemTray.getSystemTray();
//Get image
BufferedImage trayIconImage = ImageIO.read(getClass().getResource("favicon.png"));
int trayIconWidth = new TrayIcon(trayIconImage).getSize().width;
TrayIcon trayIcon = new TrayIcon(trayIconImage.getScaledInstance(trayIconWidth, -1, Image.SCALE_SMOOTH));
//Let the system resizes the image if needed
trayIcon.setImageAutoSize(true);
//Set tooltip text and notification text
if(user.getDescargos()>=5 || user.getOtrosPendientes()>=1){
mensaje = "Descargos pendientes: " + user.getDescargos() + "\nSecciones pendientes: "+ user.getOtrosPendientes();
}
trayIcon.setToolTip(mensaje);
tray.add(trayIcon);
trayIcon.displayMessage("Pendientes EKHI", mensaje, MessageType.WARNING);
}
"Java(TM) Platform SE binary" doesn't appear if you use TrayIcon.MessageType.NONE
Example Image:
The only way to solve this is to use a java native launcher.
My best choice would be javapackager which allows you to bundle a local copy of the JVM to make your app completely portable.
Other option is launch4j which just creates a launcher but still requires Java installed.

Display numbers on a tray icon with SWT

I would like to show some numbers on my tray icon indicating a number of events that happened to the user like what is done in this facebook notifications icons:
Do you think that it is possible ?
Thank you
You can do this using the TaskBar and TaskItem classes although it may not work on all platforms.
TaskBar taskBar = Display.getDefault().getSystemTaskBar();
// TODO may return null if not supported on the platform
// Get application item
TaskItem taskItem = taskBar.getItem(null);
if (taskItem != null)
taskItem.setOverlayText("your text");
Also try:
TaskItem taskItem = taskBar.getItem(shell);
where shell is your main application shell. The TaskItem JavaDoc suggests trying both methods of getting the TaskItem:
For better cross platform support, the application code should first
try to set this feature on the TaskItem for the main shell then on the
TaskItem for the application.

Setting width of controlsFX dialogs

I'm using controlsFX for dialogs and I can't figure out how to set the width. A lot of my messages only include the title, masthead and buttons. But the text in the buttons isn't fully display. For example:
Action deleteStuff = new DialogAction("Delete Stuff", ButtonBar.ButtonType.OTHER);
Action deleteMoreStuff = new DialogAction("Delete More Stuff", ButtonBar.ButtonType.OTHER);
Action response = Dialogs.create()
.owner(stage)
.title("Delete")
.masthead("This is a delete dialog")
.actions(deleteStuff,deleteMoreStuff,Dialog.ACTION_CANCEL)
.showConfirm();
And one of the buttons will show: "Delete Mo..."
And yes, I know I should be using the Alert class, but the project is for machines running older versions of Java. Thanks in advance.

Fading Indicator message in Java

How to/What is a good library, to create a fading indicator message in Java like that of Outlook when you get a message, or Ubuntu/Gnome when you've connected to a network?
Java 1.6 has a TrayIcon class that can be used to display notification messages.
SystemTray tray = SystemTray.getSystemTray();
Image image = Toolkit.getDefaultToolkit().getImage("tray.gif");
TrayIcon trayIcon = new TrayIcon(image, "Tray Demo");
tray.add(trayIcon);
trayIcon.displayMessage("Hello, World", "notification demo", MessageType.INFO);
Here's the result:
On Linux you may also have a little program called notify-send. It makes it easy to invokes the standard freedesktop.org notification system from the shell. You can also run it from Java.
String[] notifyCmd = {"notify-send", "Hello, World!"};
Runtime.getRuntime().exec(notifyCmd);
I had to apt-get install libnotify-bin to get this on my Ubuntu box.
I've tested these things on Windows 7 and Ubuntu 9.10. In each case the notification disappeared after some time which is I suppose the fading indicator effect that you want.
I would use Trident animation library. Your task would be almost trivial if you use it.
Also you could take a look at Timing Framework, but it wasn't updated for a long time.

Categories

Resources