一.首先   来一个普通的通知

public  static  void  wzx(int id){
        NotificationManager notificationManager= (NotificationManager)mContext. getSystemService(NOTIFICATION_SERVICE);
        NotificationCompat.Builder builder=new NotificationCompat.Builder(mContext);
        builder.setPriority(NotificationCompat.PRIORITY_MIN);//安卓发送通知不显示状态栏图标
        Notification notification=builder.setContentTitle("NBA2KOL——2")
                .setContentText("实力单打").setWhen(System.currentTimeMillis())
                .setDefaults(Notification.DEFAULT_LIGHTS).setSmallIcon(R.mipmap.text)   //5.0  有个小图标  需要
                .setColor(Color.parseColor("#880000FF"))
                .setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(),R.mipmap.icon))  //图标
                .setContentIntent(PendingIntent.getActivities(mContext,0x0001,new Intent[]{new Intent(mContext,NotifyActivity.class)},PendingIntent.FLAG_UPDATE_CURRENT))
                .build();
        notification.flags |= Notification.FLAG_NO_CLEAR;       //这个系统无法清除的标识
        notificationManager.notify(id,notification);
    }

二 来一个宽的通知

//按钮的点击方法
    public  static void showtext(){
        NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext);
        builder .setDefaults(Notification.DEFAULT_LIGHTS).setSmallIcon(R.mipmap.text);   //5.0  有个小图标  需要一张只有透明和白色组成的图片
        builder.setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(),R.mipmap.icon));  //图标
        builder.setContentTitle("wzx");//系统限制,可能不显示
        builder.setContentText("我们打袁绍吧");//系统限制,可能不显示
        builder.setDefaults(Notification.DEFAULT_ALL);
        //添加宽视图
        NotificationCompat.InboxStyle style = new NotificationCompat.InboxStyle();
        style.setBigContentTitle("十胜十败");
        //由手机屏幕像素决定显示多少
        style.addLine("第一是道胜");
        style.addLine("第二是义胜");
        style.addLine("第三是治胜");
        style.addLine("第四是度胜");
        style.addLine("第五是谋胜");
        style.addLine("第六是德胜");
        style.addLine("第七是仁胜");
        style.addLine("第八是明胜");
        style.addLine("第九是文胜");
        style.addLine("第十是武胜");
        style.setSummaryText("作者:子轩大大");//添加概要
        builder.setStyle(style);
        Notification n = builder.build();
        n.flags |= Notification.FLAG_NO_CLEAR;
        NotificationManager manager = (NotificationManager)mContext. getSystemService(NOTIFICATION_SERVICE);

        manager.notify(1, n);
    }

三 接下来就是自定义布局的通知了

//首先你得开启一个广播
IntentFilter filter = new IntentFilter();
        filter.addAction(NOTIFICA_A);
        filter.addAction(NOTIFICA_b);
        context. registerReceiver(onClickReceiver, filter);
//这里是进行点击事件的操作
static BroadcastReceiver onClickReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(NOTIFICA_A)) {
                Toast.makeText(mContext, "wzx", Toast.LENGTH_SHORT).show();

            }
            if (intent.getAction().equals(NOTIFICA_b)) {
                Toast.makeText(mContext, "njx", Toast.LENGTH_SHORT).show();

            }
        }
    };

 public static void shwoNotify(int id) {

       RemoteViews  view_custom = new RemoteViews(mContext.getPackageName(), R.layout.notification_text1);


            Intent buttonIntent = new Intent(NOTIFICA_A);
            PendingIntent pendButtonIntent = PendingIntent.getBroadcast(mContext, 0, buttonIntent, 0);
            //要点击控件的id  和Intent
            view_custom.setOnClickPendingIntent(R.id.notification1_img,pendButtonIntent);


            Intent buttonIntenttext = new Intent(NOTIFICA_b);
            PendingIntent pendButtonIntenttext = PendingIntent.getBroadcast(mContext, 0, buttonIntenttext, 0);
            view_custom.setOnClickPendingIntent(R.id.notification1_text,pendButtonIntenttext);



        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mContext);
        mBuilder
                .setContent(view_custom)
//                .setCustomBigContentView(view_custom)
                .setWhen(System.currentTimeMillis())// 通知产生的时间,会在通知信息里显示
                .setTicker("有新资讯")
                .setPriority(Notification.PRIORITY_HIGH)// 设置该通知优先级
                .setOngoing(false)//不是正在进行的   true为正在进行  效果和.flag一样
                .setSmallIcon(R.mipmap.icon)
        ;
        Notification notify = mBuilder.build();

        notify.flags |= Notification.FLAG_NO_CLEAR;
        NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(NOTIFICATION_SERVICE);
        notificationManager.notify(id, notify);
    }

还有二个标识

private static final String NOTIFICA_A="WZX";
    private static final String NOTIFICA_b="NJX";

注意事项

1.由于安卓版本问题(targetSdkVersion>21)  通知栏里 通知的图标会出现 白色方块  

解决办法  图标为透明和白色组成的图片

2. 如果需要更新通知(即UI更新)  只需要重新通知一遍(该通知的ID必须一致)

3. 通知高度如何设置(犹豫机型不同实现的效果也不同  第一个是不同布局 第二个是大布局)

.setCustomContentView(createContentView(context))
 .setCustomBigContentView(createContentBigView(context))

private static RemoteViews createContentBigView(Context activity) {
        final RemoteViews view = new RemoteViews(activity.getPackageName(), R.layout.notify_big_view);
        return view;
    }

    private static RemoteViews createContentView(Context activity) {
        final RemoteViews view = new RemoteViews(activity.getPackageName(), R.layout.notify_view);
        return view;
    }

4. 犹豫机型问题 通知也各不一  最近有遇到新问题所以写了下来自己的bug

8.0以上又得重新写

首先判断通知栏权限

**
     * 获取通知权限
     * @param context
     */
    @TargetApi(Build.VERSION_CODES.KITKAT)
    private static boolean isNotificationEnabled(Context context) {

        String CHECK_OP_NO_THROW = "checkOpNoThrow";
        String OP_POST_NOTIFICATION = "OP_POST_NOTIFICATION";

        AppOpsManager mAppOps = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
        ApplicationInfo appInfo = context.getApplicationInfo();
        String pkg = context.getApplicationContext().getPackageName();
        int uid = appInfo.uid;

        Class appOpsClass = null;
        try {
            appOpsClass = Class.forName(AppOpsManager.class.getName());
            Method checkOpNoThrowMethod = appOpsClass.getMethod(CHECK_OP_NO_THROW, Integer.TYPE, Integer.TYPE,
                    String.class);
            Field opPostNotificationValue = appOpsClass.getDeclaredField(OP_POST_NOTIFICATION);

            int value = (Integer) opPostNotificationValue.get(Integer.class);
            return ((Integer) checkOpNoThrowMethod.invoke(mAppOps, value, uid, pkg) == AppOpsManager.MODE_ALLOWED);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public  static void setNotificationPermissions(Context context){
        boolean enabled = isNotificationEnabled(context);
        if (!enabled) {
            /**
             * 跳到通知栏设置界面
             * @param context
             */
            Intent localIntent = new Intent();
            //直接跳转到应用通知设置的代码:
            if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                localIntent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
                localIntent.putExtra("app_package", context.getPackageName());
                localIntent.putExtra("app_uid", context.getApplicationInfo().uid);
            } else if (android.os.Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {
                localIntent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                localIntent.addCategory(Intent.CATEGORY_DEFAULT);
                localIntent.setData(Uri.parse("package:" + context.getPackageName()));
            } else {
                //4.4以下没有从app跳转到应用通知设置页面的Action,可考虑跳转到应用详情页面,
                localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                if (Build.VERSION.SDK_INT >= 9) {
                    localIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
                    localIntent.setData(Uri.fromParts("package", context.getPackageName(), null));
                } else if (Build.VERSION.SDK_INT <= 8) {
                    localIntent.setAction(Intent.ACTION_VIEW);
                    localIntent.setClassName("com.android.settings", "com.android.setting.InstalledAppDetails");
                    localIntent.putExtra("com.android.settings.ApplicationPkgName", context.getPackageName());
                }
            }
            context.startActivity(localIntent);
        }
    }

然后就可以发送通知了  不开启通知栏权限是通知不的  8.0以上增加了

 

setChannelId不设置这个是显示不了的
private static final int PUSH_NOTIFICATION_ID = (0x001);
    private static final String PUSH_CHANNEL_ID = "PUSH_NOTIFY_ID";
    private static final String PUSH_CHANNEL_NAME = "PUSH_NOTIFY_NAME";

    public static void Notification(Context context) {
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(PUSH_CHANNEL_ID, PUSH_CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
            if (notificationManager != null) {
                notificationManager.createNotificationChannel(channel);
            }
        }
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context,PUSH_CHANNEL_ID);
        Intent notificationIntent = new Intent(context, GlucoseActivity.class);
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
        //设置通知栏标题
        builder.setContentTitle("")
                //设置通知栏点击意图
                .setContentIntent(pendingIntent)
                .setContentText("")
//                .setNumber(1)
                //通知首次出现在通知栏,带上升动画效果的
                .setTicker("消息服务")
                //通知产生的时间,会在通知信息里显示,一般是系统获取到的时间
                .setWhen(System.currentTimeMillis())
                //设置通知小ICON     设置没有反应请在清单文件设置APP图标
                .setSmallIcon(R.drawable.text_icon)
//                //图标
                .setLargeIcon(BitmapFactory.decodeResource(context.getResources(),R.mipmap.notification_small_icon))
                .setChannelId(PUSH_CHANNEL_ID)
                //设置优先级 和默认提示 搭配使用 可实现 悬浮提示效果
                .setPriority(NotificationCompat.PRIORITY_MAX)
                //true不可以滑动删除 false 可以滑动删除
//                .setOngoing(true)
                .setDefaults(Notification.DEFAULT_ALL);

        Notification notification = builder.build();
        notification.flags |= Notification.FLAG_NO_CLEAR;
        if (notificationManager != null) {
            notificationManager.notify(PUSH_NOTIFICATION_ID, notification);
        }



    }

如果状态栏图片没有变化需要在清单文件里面设置通知的icon

android 11下滑会显示通知栏如何禁用 安卓11通知栏bug_ico