builder.setPriority(NotificationCompat.PRIORITY_LOW);

NotificationCompat.PRIORITY_LOW 收缩在通知块中,不会显示全部,点击通知栏后,才会显示

  • 通知栏关闭音效:channel.setSound(null, null); 在notification中:.setSound(null)
    再将chanel_idchanel_name换一个唯一的,这样就ok了 常驻通知栏: NotificationManager
    notificationManager= (NotificationManager)
    context.getSystemService(Context.NOTIFICATION_SERVICE);//常驻通知栏

    普通的就设置:Context.NOTIFICATION_SERVICE



public class NotificationHelper {
    private static final String CHANNEL_ID="channel_id";   //通道渠道id
    public static final String  CHANEL_NAME="chanel_name"; //通道渠道名称
    private static SimpleDateFormat sdf=new SimpleDateFormat("HH:mm:ss");


    @TargetApi(Build.VERSION_CODES.O)
    public static  void  show(Context context){
        NotificationChannel channel = null;
        if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){
            //创建 通知通道  channelid和channelname是必须的(自己命名就好)
            channel = new NotificationChannel(CHANNEL_ID, CHANEL_NAME, NotificationManager.IMPORTANCE_DEFAULT);
            channel.enableLights(true);//是否在桌面icon右上角展示小红点
            channel.setLightColor(Color.GREEN);//小红点颜色
            channel.setShowBadge(false); //是否在久按桌面图标时显示此渠道的通知
        }
        Notification notification;
        //获取Notification实例   获取Notification实例有很多方法处理    在此我只展示通用的方法(虽然这种方式是属于api16以上,但是已经可以了,毕竟16以下的Android机很少了,如果非要全面兼容可以用)
        if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){
            //向上兼容 用Notification.Builder构造notification对象

            notification = new Notification.Builder(context,CHANNEL_ID)
                    .setContentTitle("通知栏标题1")
                    .setContentText("这是消息通过通知栏的内容1")
                    .setWhen(System.currentTimeMillis())
                    .setSmallIcon(R.drawable.ic_launcher_background)
                    .setColor(Color.parseColor("#FEDA26"))
                    .setLargeIcon(BitmapFactory.decodeResource(context.getResources(),R.mipmap.ic_launcher))
                    .setTicker("巴士门1")
                    .build();
        }else {
            //向下兼容 用NotificationCompat.Builder构造notification对象
            notification = new NotificationCompat.Builder(context)
                    .setContentTitle("通知栏标题1")
                    .setContentText("这是消息通过通知栏的内容1")
                    .setWhen(System.currentTimeMillis())
                    .setSmallIcon(R.drawable.ic_launcher_background)
                    .setColor(Color.parseColor("#FEDA26"))
                    .setLargeIcon(BitmapFactory.decodeResource(context.getResources(),R.mipmap.ic_launcher))
                    .setTicker("巴士门1")
                    .build();

        }
   notification.flags=Notification.FLAG_AUTO_CANCEL;//取消通知栏
        Intent intent=new Intent(context,MainActivity.class);
        intent.putExtra("message",":"+sdf.format(new Date()));
        //用当前时间充当通知的id,这里是为了区分不同的通知,如果是同一个id,前者就会被后者覆盖
        int requestId=(int) new Date().getTime();
        //第一个参数连接上下文的context
        // 第二个参数是对PendingIntent的描述,请求值不同Intent就不同
        // 第三个参数是一个Intent对象,包含跳转目标
        // 第四个参数有4种状态
        PendingIntent pendingIntent=PendingIntent.getActivity(context,requestId,intent,PendingIntent.FLAG_UPDATE_CURRENT);
//        builder.setContentIntent(pendingIntent);

        notification.contentIntent=pendingIntent;
        //发送通知
        int  notifiId=1;
        //创建一个通知管理器
        NotificationManager   notificationManager= (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){
            notificationManager.createNotificationChannel(channel);
        }
        notificationManager.notify(notifiId,notification);
    }
}

调用的时候,只需要:NotificationHelper.show(this);
原地址:https://blog.csdn.net/zheglei/article/details/79869689#commentBox,在他的基础上,添加了一个跳转事件

创建多个图标及图标对应的事件跳转:

public class NotificationHelper {

    private static final String CHANNEL_ID="channel_id";   //通道渠道id
    public static final String  CHANEL_NAME="chanel_name"; //通道渠道名称
    private static SimpleDateFormat sdf=new SimpleDateFormat("HH:mm:ss");
    private static int  notifiId=1;


    @TargetApi(Build.VERSION_CODES.O)
    public static  void  show(Context context){
        NotificationChannel channel = null;
        if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){
            //创建 通知通道  channelid和channelname是必须的(自己命名就好)
            channel = new NotificationChannel(CHANNEL_ID, CHANEL_NAME, NotificationManager.IMPORTANCE_DEFAULT);
            channel.enableLights(true);//是否在桌面icon右上角展示小红点
            channel.setLightColor(Color.GREEN);//小红点颜色
            channel.setShowBadge(false); //是否在久按桌面图标时显示此渠道的通知
        }
        Notification notification;
        //获取Notification实例   获取Notification实例有很多方法处理    在此我只展示通用的方法(虽然这种方式是属于api16以上,但是已经可以了,毕竟16以下的Android机很少了,如果非要全面兼容可以用)
        if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){
            //向上兼容 用Notification.Builder构造notification对象

            RemoteViews remoteView = new RemoteViews(getPackageName(), R.layout.item_notification);
            Intent intent0 = new Intent(context, ScanRubbishActivity.class);//垃圾清理

            Intent intent1 = new Intent(context, MusicDetailActivity.class);//文件管理
            intent1.putExtra("type","1");
            intent1.putExtra("title","文件管理");

            Intent intent2 = new Intent(context, MusicDetailActivity.class);//安装包管理/app管理
            intent2.putExtra("type","2");
            intent2.putExtra("title","安装包");

            Intent intent3 = new Intent(context, MusicDetailActivity.class);//微信专清
            intent3.putExtra("type","3");
            intent3.putExtra("title","其他");

//            int requestId=(int) new Date().getTime();
//				0、1、2是为了防止Intent一样,造成获取不了不一样的pendingIntent,pengdingIntent不一样就可以
//				设置不一样的跳转事件;
            int requestId=1;
            PendingIntent pendingIntentClear=PendingIntent.getActivity(context,requestId,
                    intent0,PendingIntent.FLAG_UPDATE_CURRENT);
            PendingIntent pendingIntentFile=PendingIntent.getActivity(context,0,
                    intent1,PendingIntent.FLAG_UPDATE_CURRENT);
            PendingIntent pendingIntentApp=PendingIntent.getActivity(context,1,
                    intent2,PendingIntent.FLAG_UPDATE_CURRENT);
            PendingIntent pendingIntentWchat=PendingIntent.getActivity(context,2,
                    intent3,PendingIntent.FLAG_UPDATE_CURRENT);

//            remoteView.setImageViewResource(R.id.notifi_ic_img_clear,R.drawable.icon_clean_normal);
//            remoteView.setImageViewResource(R.id.notifi_ic_img_file,R.drawable.fun_fiction);
//            remoteView.setTextColor(R.id.re_text, Color.RED);
//            remoteView.setTextViewText(R.id.re_text, "remote view demo");
//            remoteView.setImageViewResource(R.id.re_image, R.drawable.btn_me_share);
            remoteView.setOnClickPendingIntent(R.id.notifi_ic_img_clear, pendingIntentClear);

            remoteView.setOnClickPendingIntent(R.id.notifi_ic_img_file,pendingIntentFile);

            remoteView.setOnClickPendingIntent(R.id.notifi_ic_img_app, pendingIntentApp);

            remoteView.setOnClickPendingIntent(R.id.notifi_ic_img_wchat, pendingIntentWchat);

            notification = new Notification.Builder(context,CHANNEL_ID)
                    .setCustomContentView(remoteView)
//                    .setContentTitle("通知栏标题1")
//                    .setContentText("这是消息通过通知栏的内容1")
//                    .setWhen(System.currentTimeMillis())
                    .setSmallIcon(R.drawable.ic_launcher_background)
//                    .setColor(Color.parseColor("#FEDA26"))
//                    .setLargeIcon(BitmapFactory.decodeResource(context.getResources(),R.mipmap.ic_launcher))
//                    .setTicker("巴士门1")
//                    .setContentIntent(pendingIntentClear)
                    .build();


            Log.d("-------", "show: ");
        }else {
            //向下兼容 用NotificationCompat.Builder构造notification对象
            notification = new NotificationCompat.Builder(context)
                    .setContentTitle("通知栏标题1")
                    .setContentText("这是消息通过通知栏的内容1")
                    .setWhen(System.currentTimeMillis())
                    .setSmallIcon(R.drawable.ic_launcher_background)
                    .setColor(Color.parseColor("#FEDA26"))
                    .setLargeIcon(BitmapFactory.decodeResource(context.getResources(),R.mipmap.ic_launcher))
                    .setTicker("巴士门1")
                    .build();

        }

//        Intent intent=new Intent(context,MainActivity.class);
//        intent.putExtra("message",":"+sdf.format(new Date()));
//        //用当前时间充当通知的id,这里是为了区分不同的通知,如果是同一个id,前者就会被后者覆盖
//        int requestId=(int) new Date().getTime();
//        //第一个参数连接上下文的context
//        // 第二个参数是对PendingIntent的描述,请求值不同Intent就不同
//        // 第三个参数是一个Intent对象,包含跳转目标
//        // 第四个参数有4种状态
//        PendingIntent pendingIntent=PendingIntent.getActivity(context,requestId,
//                intent,PendingIntent.FLAG_UPDATE_CURRENT);
////        builder.setContentIntent(pendingIntent);

//        notification.contentIntent=pendingIntent;
        //发送通知
        //创建一个通知管理器
        NotificationManager   notificationManager= (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);//常驻通知栏
        if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){
            notificationManager.createNotificationChannel(channel);
        }
        notificationManager.notify(1,notification);
    }

    public static boolean checkNotifySetting(Context context) {
        NotificationManagerCompat manager = NotificationManagerCompat.from(context);
        // areNotificationsEnabled方法的有效性官方只最低支持到API 19,低于19的仍可调用此方法不过只会返回true,即默认为用户已经开启了通知。
        return manager.areNotificationsEnabled();
    }

}


更多推荐