Android Notification通知栏的使用,通知栏在Android8.0系统不显示的问题;

1、正常使用通知栏:

 /**
         *  创建通知栏管理工具
         */

        NotificationManager notificationManager = (NotificationManager) getSystemService
                (NOTIFICATION_SERVICE);

        /**
         *  实例化通知栏构造器
         */

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(MainActivity.this);
        /**
         *  设置Builder
         */
        //设置标题
        mBuilder.setContentTitle("我是标题")
                //设置内容
                .setContentText("我是内容")
                //设置大图标
                .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))
                //设置小图标
                .setSmallIcon(R.mipmap.ic_launcher_round)
                //设置通知时间
                .setWhen(System.currentTimeMillis())
                //首次进入时显示效果
                .setTicker("我是测试内容")
                //设置通知方式,声音,震动,呼吸灯等效果,这里通知方式为声音
                .setDefaults(Notification.DEFAULT_SOUND);
        //发送通知请求
        notificationManager.notify(10, mBuilder.build());

2、在8.0系统通知栏无法显示的问题:

String id = "ccb";
        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        Notification notification = null;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel mChannel = new NotificationChannel("ccb", "CCB", NotificationManager.IMPORTANCE_HIGH);
            notificationManager.createNotificationChannel(mChannel);
            notification = new Notification.Builder(this)
                    .setChannelId(id)
                    .setContentTitle("新消息提示")
                    .setContentText("我是")
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .build();
        } else {
            notification = new NotificationCompat.Builder(this)
                    .setContentTitle("新消息提示")
                    .setContentText("我是")
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setOngoing(true)
                    .setChannel(id)
                    .build();
        }
        notificationManager.notify(1, notification);

猜你喜欢

转载自blog.csdn.net/qq_35605213/article/details/84992726