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

posted @ 2022-10-25 10:05  Z_Chan  阅读(287)  评论(0编辑  收藏  举报