简体   繁体   中英

Blank icon in Windows 10 notification

My java application shows its icon on the system tray using code that looks more or less like this:

Toolkit mainToolkit = Toolkit.getDefaultToolkit();
SystemTray mainTray = SystemTray.getSystemTray();
Image trayIconImage = mainToolkit.createImage(getClass().getResource(resourcePath));
TrayIcon mainTrayIcon = new TrayIcon(trayIconImage);
mainTray.add(mainTrayIcon);

Sometimes I change that icon like this:

Image newImage = mainToolkit.createImage(getClass().getResource(otherPath));
mainTrayIcon.setImage(newImage);

From time to time my app needs to show some notification (using a baloon message coming from its tray icon):

mainTrayIcon.displayMessage(someCaption,  msg, TrayIcon.MessageType.NONE);

All this code is actually somehow simplified but grasps this functionality pretty well.

So everything's fine on Windows 7. But it turns out that on Windows 10 it is being shown differently. On the notification there's an icon shown on the left. It usually is my app's current tray icon, but sometimes it's just blank:

错误的通知

In the upper red circle (on the notification) is that blank icon which sometimes appears instead of my app's icon (in the lower red circle, on the system's tray). I have no idea why does it occur. All I know is this happens only when the app's tray icon and notification message change before first notification (which always shows its icon correctly) disappears. If the notification is shown, then fades / is closed manually AND THEN app's tray icon and notifications change, next notification (with new message that was just set) will show app's icon correctly.

Just came across this issue and found the correct solution:

mainTrayIcon.setImageAutoSize(true);

Here's a method to send a notification on Windows:

public static void sendNotification(String title, String subtitle, String pathToIcon) {
    SystemTray mainTray = SystemTray.getSystemTray();
    Image trayIconImage = Toolkit.getDefaultToolkit().getImage(pathToIcon);
    TrayIcon mainTrayIcon = new TrayIcon(trayIconImage);
    mainTrayIcon.setImageAutoSize(true);
    try {
        mainTray.add(mainTrayIcon);
        mainTrayIcon.displayMessage(title,  subtitle, TrayIcon.MessageType.NONE);
    }
    catch (Exception e) {
        e.printStackTrace();
    }
}

Calling sendNotification("Title", "Subtitle", "icons/icon-128.png"); shows

工作通知

//this method to showing notification
public void notifacation(String filenamev, String tooltipv, String etats, String textv, String capationv, String messagetype) {
    try {
        //Obtain only one instance of the SystemTray object
        SystemTray tray = SystemTray.getSystemTray();

        // If you want to create an icon in the system tray to preview
        Image image = Toolkit.getDefaultToolkit().createImage(filenamev);
        //Alternative (if the icon is on the classpath):
        //Image image = Toolkit.getDefaultToolkit().createImage(getClass().getResource("icon.png"));

        TrayIcon trayIcon = new TrayIcon(image, tooltipv);
        //Let the system resize the image if needed
        trayIcon.setImageAutoSize(true);
        //Set tooltip text for the tray icon
        trayIcon.setToolTip(etats);
        tray.add(trayIcon);

        // Display info notification:
        // trayIcon.displayMessage(capationv, textv, TrayIcon.MessageType.ERROR
        trayIcon.displayMessage(capationv, textv, MessageType.valueOf(messagetype));

        // Error:
        // trayIcon.displayMessage("Hello, World", "Java Notification Demo", MessageType.ERROR);
        // Warning:
        // trayIcon.displayMessage("Hello, World", "Java Notification Demo", MessageType.WARNING);
    } catch (Exception ex) {
        System.err.print(ex);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM