-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNotificationApp.java
More file actions
91 lines (78 loc) · 3.84 KB
/
NotificationApp.java
File metadata and controls
91 lines (78 loc) · 3.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import java.awt.*;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.SwingUtilities;
/**
* A simple application to display a desktop notification using Java's SystemTray.
*/
public class NotificationApp {
// --- Configuration for the Notification ---
private static final String NOTIFICATION_TITLE = "Crowd Alert!";
private static final String NOTIFICATION_MESSAGE = "Someone in your area has tested positive for COVID-19. Tap for more information.";
private static final TrayIcon.MessageType MESSAGE_TYPE = TrayIcon.MessageType.WARNING;
// NOTE: This path should point to a small image (e.g., 16x16 or 32x32)
// for the tray icon. The image must be available when running the program.
private static final String ICON_PATH = "default_icon.png";
public static void main(String[] args) {
// Ensure the code runs on the Event Dispatch Thread (EDT) for GUI operations
SwingUtilities.invokeLater(NotificationApp::runNotification);
}
/**
* Initializes the SystemTray and displays the notification.
*/
private static void runNotification() {
// Step 7: Handle errors during setup
if (!SystemTray.isSupported()) {
System.err.println("System Tray is not supported on this platform.");
return;
}
try {
// Step 2 & 4: Design/Set the notification icon
// We need a dummy icon to represent the application in the system tray
// The image file must be present in the project folder!
Image icon = Toolkit.getDefaultToolkit().getImage(ICON_PATH);
// Step 2: Create a TrayIcon
TrayIcon trayIcon = new TrayIcon(icon, "COVID-19 Notifier");
trayIcon.setImageAutoSize(true);
// Step 3: Get the SystemTray instance and add the icon
final SystemTray tray = SystemTray.getSystemTray();
tray.add(trayIcon);
// Step 5: Allow user interaction (Click listener)
// This is what happens when the user clicks the notification pop-up.
ActionListener clickListener = e -> {
System.out.println("Notification clicked! Opening web page...");
// Step 5: Example of user action - opening a URL
try {
// Open a URL in the default browser (for more info)
Desktop.getDesktop().browse(java.net.URI.create("https://www.who.int/emergencies/diseases/novel-coronavirus-2019"));
} catch (IOException ex) {
System.err.println("Could not open browser: " + ex.getMessage());
}
};
trayIcon.addActionListener(clickListener);
// Step 4 & 3: Display the notification (Balloon Tip)
// The notification appearance is OS-dependent.
trayIcon.displayMessage(
NOTIFICATION_TITLE,
NOTIFICATION_MESSAGE,
MESSAGE_TYPE
);
// Optional: Remove the icon after a delay (e.g., 10 seconds)
// The notification will disappear automatically after a few seconds based on OS settings.
// We keep the icon visible for 10 seconds for user to interact with the tray.
new java.util.Timer().schedule(
new java.util.TimerTask() {
@Override
public void run() {
tray.remove(trayIcon);
System.exit(0); // Exit the application
}
},
10000 // 10 seconds
);
} catch (AWTException | IllegalArgumentException e) {
// Step 7: Handle potential exceptions
System.err.println("Error setting up System Tray or Icon: " + e.getMessage());
}
}
}