如何将通知发送到某些窗口10 PC
我已经完成了系统托盘将通知发送到PC并运行Java程序。 如何将其修改以使用IP地址发送到某些PC?
以下是我完成的代码。
import java.awt.*;
import java.awt.TrayIcon.MessageType;
public class MemoSample {
public static void main(String[] args) throws AWTException {
if (SystemTray.isSupported()) {
MemoSample td = new MemoSample();
td.displayTray();
System.exit(0);
} 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();
//Get image
Image trayIconImage = Toolkit.getDefaultToolkit().createImage("icon.png");
int trayIconWidth = new TrayIcon(trayIconImage).getSize().width;
TrayIcon trayIcon = new TrayIcon(trayIconImage.getScaledInstance(trayIconWidth, -1, Image.SCALE_SMOOTH));
//Let the system resizes the image if needed
trayIcon.setImageAutoSize(true);
trayIcon.setToolTip("Notification from system");
tray.add(trayIcon);
trayIcon.displayMessage("You Have a pending job", "Notification from system" ,MessageType.INFO);
}
}
I had done the system tray to send the notification to the PC and run the java program.
How can I modify it to be able to send to certain PC using an IP address?
Below is the code that I had done.
import java.awt.*;
import java.awt.TrayIcon.MessageType;
public class MemoSample {
public static void main(String[] args) throws AWTException {
if (SystemTray.isSupported()) {
MemoSample td = new MemoSample();
td.displayTray();
System.exit(0);
} 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();
//Get image
Image trayIconImage = Toolkit.getDefaultToolkit().createImage("icon.png");
int trayIconWidth = new TrayIcon(trayIconImage).getSize().width;
TrayIcon trayIcon = new TrayIcon(trayIconImage.getScaledInstance(trayIconWidth, -1, Image.SCALE_SMOOTH));
//Let the system resizes the image if needed
trayIcon.setImageAutoSize(true);
trayIcon.setToolTip("Notification from system");
tray.add(trayIcon);
trayIcon.displayMessage("You Have a pending job", "Notification from system" ,MessageType.INFO);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决方案会有些复杂...
您可以通过使用Java serversocket设置服务器来完成此操作。然后,您可以使用插座连接到该服务器,然后发送和接收消息。然后,您可以在接收特定命令时显示推送通知。
您还可以在每个设备上设置服务器,该设备应该能够收到通知,这将使单个始终运行的服务器过时。但是我不建议这样做。
您可以在Java中了解有关此类网络通信的信息,例如此 page 。
A solution for this would be a bit complicated...
You can accomplish this by setting up a server with the java ServerSocket. You can then connect to that server with Sockets and send and recieve messges. Then you can show push notifications when receiving specific commands.
You could also set up a server on every device that should be able to recieve notifications which would make a single always running server obsolete. But I would not recommend doing it that way.
You can learn something about such network communication in Java on for example this Page.