Search code examples
javapush-notificationnotificationsdesktop-application

Create Java Desktop Notification


The requirement is to create a desktop notification which can register a click-event. I cannot use web-sockets or any browser notifications.
I am unable to use Tray-Icons and SystemTray because they cannot register Click-Events on DISPLAY MESSAGE. They can have click-events on the trayicon but not on the display message. The closest example - "When we register a click on a Skype message, it opens Skype for us"

Screenshot


Skype Notification Pop-Up


On clicking the above Notification Skype chat opens-up. The same functionality is not supported with Tray-Icons. Either a work around it or a new approach will be do.
Hope I am clear thanks.


Solution

  • I used the following repository from github DorkBox.
    Simply add maven dependency as instructed on the github link. However, I was unable to check how to change the UI for the notifications.

    Notify.create()
          .title(text)
          .text(title)
          .position(Pos.TOP_RIGHT)
          .onAction( new ActionHandler<Notify>() {
                @Override
                public void handle(Notify value) {
                    if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) {
                        try {
                            Desktop.getDesktop().browse(new URI(targetUrl));
                        } catch (IOException e) {
                            e.printStackTrace();
                        } catch (URISyntaxException e) {
                            e.printStackTrace();
                        }
                    }
                }
          })
          .hideAfter(5000)
          .shake(250, 5)
          .darkStyle()      // There are two default themes darkStyle() and default.
          .showConfirm();   // You can use warnings and error as well.
    

    Add the following code in your main block and you are good to go.