Mark Tripoli I am HP Nonstop developer, who develops train control systems. I love open source projects and information and attempting to teach others new tools. I am a blockchain junkie and love its tech!

Creating Push Notifications With Java

  Reading Time:

In this tutorial, I will be showing you how to create push notifications using java. I will show you how to create one using SystemTray in AWT. JavaFX has not implemented their version as of now. While there are ways around it, I find that it becomes to tedious to implment compared to AWT, so I will not cover any methods.

Screen-Shot-2019-06-13-at-7.30.52-AM

Using AWT

Showing a notification is pretty easy using AWT. We need to utilize the SystemTray to create a notification. It must be noted, not all OS's use SystemTray, so you should do a quick check, in your app, prior to pushing. Since AWT is included in the JDK, we do not need any special dependencies.

package com.triippztech;

import java.awt.*;

public class Main  {

    public static void main(String[] args) {
        try {
            pushNotification();
        } catch (AWTException e) {
            e.printStackTrace();
        }
    }

    public static void pushNotification() throws AWTException {
        // Do a check to ensure SystemTray is supporteed for this OS
        if (SystemTray.isSupported())
        {
            //Obtain only one instance of the SystemTray object
            SystemTray tray = SystemTray.getSystemTray();

            //If the icon is a file
            Image image = Toolkit.getDefaultToolkit()
                                 .createImage("myImage.png");

            //Alternative (if the icon is on the classpath):
            // Image image = Toolkit.getDefaultToolkit().createImage(
                Main.class.getClassLoader().getResource("img/xlm.png"));

            TrayIcon trayIcon = 
                new TrayIcon( image,"Java AWT Push   Notification");

            //Let the system resize the image if needed
            trayIcon.setImageAutoSize(true);

            //Set tooltip text for the tray icon
            trayIcon.setToolTip("Notification Demo");

            tray.add(trayIcon);

            // Create our message and what type of Notification this is.
            trayIcon.displayMessage(
                "Wake up!", 
                "Java Push Notification Demo", 
                TrayIcon.MessageType.INFO);
            
            // If you want to add a timer for it to go away
            // trayIcon.wait(10000);
        } else {
            //Handle your errors accordingly
            System.err.println("SystemTray is not supported on this OS");
        }
    }
}

Screen-Shot-2019-06-13-at-8.20.26-AM

Screen-Shot-2019-06-13-at-8.21.04-AM

Screen-Shot-2019-06-13-at-8.21.36-AM

Go ahead and play with these Notifications. Try customizing them more, and utilize them in your application!

If you have any questions, feel free to drop a comment below.

Build an issue tracker with jHipster (part 2)

Introduction Welcome back! This is the second part of our JHipster tutorial. We will be working on our frontend portion of our PWA. We will be...

Stellar SDK Examples (Java)

In this post, I will be adding some examples of how to use the java-stellar-sdk. Hopefully this should give you a decent idea of how to...

Triippz Tech   Never miss a story from Triippz Tech, get updates in your inbox.