Create persistent Windows 10 notifications

I successfully created a notification that appeared in the Windows 10 action center using this answer . The problem is that the notification remains there for 5 seconds, and then, as soon as it disappears, it is completely removed from the action center. How can I get the action center to keep the notification until the user rejects it? Here is the code:

import java.awt.*; import java.awt.TrayIcon.MessageType; import javax.swing.JOptionPane; public class Win10Notif { public static void main(String[] args) throws AWTException, java.net.MalformedURLException { if (SystemTray.isSupported()) { Win10Notif td = new Win10Notif(); td.displayTray(); } else { System.err.println("System tray not supported!"); } } public void displayTray() throws AWTException, java.net.MalformedURLException { //Obtain only one instance of the SystemTray object SystemTray tray = SystemTray.getSystemTray(); //If the icon is a file Image image = Toolkit.getDefaultToolkit().createImage("icon.png"); //Alternative (if the icon is on the classpath): //Image image = Toolkit.getToolkit().createImage(getClass().getResource("icon.png")); TrayIcon trayIcon = new TrayIcon(image, "Tray Demo"); //Let the system resizes the image if needed trayIcon.setImageAutoSize(true); //Set tooltip text for the tray icon trayIcon.setToolTip("System tray icon demo"); tray.add(trayIcon); trayIcon.displayMessage("Hello, World", "notification demo", MessageType.INFO); } } 
+5
source share
1 answer

I think this is controlled by Windows itself or its own version of the JVM. At the very least, the public API does not provide the ability to set a specific time on the screen for notifications.

If you don’t need to stick to the action center, you can use external libraries for desktop notifications, for example:

  • JCarrierPigeon : he's tiny, and he's fast; although it has dependencies on the Timing Framework library. Even the API it brings is small.
  • JTelegraph : An extension to JCarrierPigeon , with some fountain icons and styles out of the box. Naturally, this also depends on the Timing Framework library.
  • JCommunique : one of the most complete options, which means a larger footprint; but at least it has no dependencies and it is really flexible, covering many use cases.
  • Twinkle : It's stylish but not light. Includes icons, animations, and other resources. The code has some dependencies at compile time, but I think the redistributable .jar comes with everything that is already connected. It is free for non-commercial purposes.
  • DS Desktop Notification : It's tiny, lightweight, easy to set up and no dependencies. It can be used in the same way as JOptionpane.showMessageDialog() , or by creating and setting up notification objects before publishing them manually. Properties such as color themes, icons, on-screen time and actions can be configured, and themes and stock icons are available.

You can get and try any of them for free.

+2
source

Source: https://habr.com/ru/post/1263485/


All Articles