Android发送通知支持安卓11
上代码
private final static String NOTIFICATION_CHANNEL_ID = "1001";
private final static String NOTIFICATION_CHANNEL_NAME = "Service";
private NotificationManager notificationManager;
private NotificationChannel notificationChannel;
Notification nof=getNotification("title","content");
notificationManager.notify(1124,nof);
private Notification getNotification(String title, String message) { if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { notificationChannel = new NotificationChannel( NOTIFICATION_CHANNEL_ID, NOTIFICATION_CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH ); notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC); notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); notificationManager.createNotificationChannel(notificationChannel); } //创建一个通知消息的构造器 Notification.Builder builder = new Notification.Builder(this); if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { //Android8.0开始必须给每个通知分配对应的渠道 builder = new Notification.Builder(this, NOTIFICATION_CHANNEL_ID); } builder.setAutoCancel(false)//设置是否允许自动清除 .setSmallIcon(R.drawable.ic_launcher_foreground)//设置状态栏里的小图标 .setWhen(System.currentTimeMillis())//设置推送时间,格式为"小时:分钟" .setContentTitle(title)//设置通知栏里面的标题文本 .setContentText(message);//设置通知栏里面的内容文本 //根据消息构造器创建一个通知对象 if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) { Notification notify = builder.build(); return notify; } return null; }
GitHub地址 下载前给star
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了