JAVA向win10通知栏推送消息

import java.awt.*;

public class Main {

    public static void main(String[] args) throws AWTException {
        if (SystemTray.isSupported()) {
            Main nd = new Main();
            nd.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", TrayIcon.MessageType.INFO);
    }

}

Android通知是用来显示推送消息的重要组件之一。要在Android应用中实现通知推送消息,你需要进行以下步骤: 1. 创建通知渠道:从Android 8.0(API级别26)开始,你需要创建通知渠道来管理和组织通知。使用NotificationChannel类来创建通知渠道,并设置其名称、描述和重要性级别等参数。 2. 构建通知内容:使用NotificationCompat.Builder类来构建通知的内容,包括标题、文本、图标、大图等。 3. 设置点击行为:可以为通知设置点击行为,比如打开应用的某个界面或执行特定的操作。使用PendingIntent类来定义点击通知时要执行的动作。 4. 发送通知:通过NotificationManager类的notify()方法发送通知。指定一个唯一的通知ID以及之前创建的NotificationCompat.Builder对象。 下面是一个示例代码,演示了如何发送一个简单的通知: ```java // 创建通知渠道 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel channel = new NotificationChannel("channel_id", "Channel Name", NotificationManager.IMPORTANCE_DEFAULT); channel.setDescription("Channel Description"); // 在NotificationManager中创建通知渠道 NotificationManager notificationManager = getSystemService(NotificationManager.class); notificationManager.createNotificationChannel(channel); } // 构建通知内容 NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "channel_id") .setSmallIcon(R.drawable.notification_icon) .setContentTitle("Notification Title") .setContentText("Notification Text") .setPriority(NotificationCompat.PRIORITY_DEFAULT) .setContentIntent(pendingIntent) // 设置点击行为 .setAutoCancel(true); // 点击后自动取消通知 // 发送通知 NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this); notificationManager.notify(notificationId, builder
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一点博客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值