Sending windows notifications within processing

is it possible to send my own windows notifications using processing?
I have tried to follow this stack overflow question but it doesn’t seem to be working in processing. I would really appreciate any help! Thanks in advance!

Hi Shachar :slight_smile:

Could you share your best attempt to port this example to Processing and the issues you are running into? This would make it easier for other community members to offer a solution.

1 Like

Hi, I tried to copy the code, deleted “public” and “static” that didn’t work, but the code still can’t be run. now it looks like this:

import java.awt.*;
import java.awt.TrayIcon.MessageType;
class TrayIconDemo {
      void main(String[] args) throws AWTException {
        if (SystemTray.isSupported()) {
            TrayIconDemo td = new TrayIconDemo();
            td.displayTray();
        } else {
            System.err.println("System tray not supported!");
        }
    }
    public void displayTray() throws AWTException {
        //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.getDefaultToolkit().createImage(getClass().getResource("icon.png"));
        TrayIcon trayIcon = new TrayIcon(image, "Tray Demo");
        //Let the system resize 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);
    }
}

UiBooster is a library that can do this.

2 Likes

The following code runs on my system (Mac). The ‘icon.png’ does not have to be in your sketch folder but you will need the complete url to load it (edit the one supplied below before running the code). Note that the demo uses Toolkit…getImage() instead of .createImage(). You should see the icon appear on the menuBar (right hand side on a Mac). When clicking on the icon, hold down the ‘control’ key at the same time and you should see a message dialog on the JOptionPane. Quit the sketch to close the icon.

import java.awt.*;
import java.awt.TrayIcon.MessageType;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;

void setup() {
  surface.setVisible(false);
  if (SystemTray.isSupported()) {
    println("supported.");
  } else {
    println("System tray not supported!");
  }
  SystemTray tray = SystemTray.getSystemTray();
  Image image = Toolkit.getDefaultToolkit().getImage("/Users/yourName/Desktop/images/icon.png");
  TrayIcon trayIcon = new TrayIcon(image, "Tray Demo");
  trayIcon.setImageAutoSize(true);
  trayIcon.setToolTip("Tray icon demo");
  trayIcon.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) { // Hold down control key while clicking on tray icon
      println("Icon clicked.");
      JOptionPane.showMessageDialog(null, "Some action performed.");
    }
  }
  );
  try {
    tray.add(trayIcon);
  }
  catch(AWTException e) {
    println(e);
  }
}
2 Likes

still not working …:upside_down_face:

still not working

Works fine on my system. Are you using Windows or MacOS? Any error messages? Usual reason for ‘not working’ is due to non-loading of the .png image. Path to image has to be well-defined; sticking the image in the sketch folder doesn’t work with this demo (unless you provide a complete path to the folder). It’s very difficult to tell what the problem is without seeing the exact code that you tried to run.

I’m using windows, and I checked that the file exists. the sketch is running but it doesn’t seem to be doing anything…

Do you see your icon image on the menuBar? On a Mac it won’t do anything unless you hold down the control key while you simultaneously click on the icon. At that point you should see the message dialog.

no, nothing happens :cry:

Do you see the image?

i cannot see the image…

i cannot see the image…

Most likely you haven’t loaded the image. I just tested it on an old 32-bit Windows PC with Processing 3.5.4 and it works on that system also. Make sure that the url looks something like this:

/Users/yourName/Desktop/images/icon.png

with the slash before Users. Also on Windows you have to double click while holding down the ‘ctrl’ key on your keyboard. There is another demo which uses a popUp menu which is easier to use, but I didn’t post it because it’s more complicated. The code that I posted is a Processing conversion of the original SO code that you copy/pasted.