Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

The erroneous notification

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.

like image 851
PookyFan Avatar asked Jan 18 '16 23:01

PookyFan


1 Answers

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

Working notification

like image 142
David Corbin Avatar answered Oct 12 '22 11:10

David Corbin