Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create windows 10 persistent notifications

I have successfully created a notification pop up in the windows 10 action center using this answer. The issue is that, the notification stays there for 5 seconds, and then once it disappears is removed from the action center altogether. How can I make the action center keep the notification until the user dismisses 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);
    }
}
like image 703
Blaine Avatar asked Oct 30 '22 13:10

Blaine


1 Answers

I think that's managed by Windows itself, or the native implementation of the JVM. At least, the public API doesn't offer an option to set an specific time on screen for the notifications.

Unless you need to stick to the action center, you may consider using external libraries for desktop notifications, like these:

  • JCarrierPigeon: It's tiny, and it's quick; although it has dependencies on the Timing Framework library. Even the API it brings is small.
  • JTelegraph: An extension of JCarrierPigeon, with some stock icons and styles out of the box. Naturally, this one relies on the Timing Framework library too.
  • JCommunique: One of the most complete options, which means a bigger footprint; but at least this one has no dependencies and it's really flexible, covering a lot of use-case scenarios.
  • Twinkle: It's stylish, but not so lightweight. Includes icons, animations and other resources out of the box. The code has some dependencies at compile time, but I think the distributable .jar comes with everything bundled already. It's free for non-commercial purposes.
  • DS Desktop Notify: It's tiny, lightweight, easy to set up and has no dependencies. It can be used in the same fashion than JOptionpane.showMessageDialog() or by building and customizing notification objects before posting them manually. Properties like color themes, icons, time on screen and actions can be customized, also stock themes and icons are provided.

You can grab and try out any of these for free.

like image 106
DragShot Avatar answered Nov 15 '22 06:11

DragShot