欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android P 发通知

程序员文章站 2022-06-22 21:20:16
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); Notification notification = null; //第二步:实例化通知栏构造器NotificationCompat.Builder: if (Build.VERSION.SDK_INT >= Build.VERSION.....
 NotificationManager manager =
                (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        Notification notification = null;
        //第二步:实例化通知栏构造器NotificationCompat.Builder:
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {//判断API
            NotificationChannel mChannel = new NotificationChannel(id, name,
                    NotificationManager.IMPORTANCE_HIGH);

            manager.createNotificationChannel(mChannel);

            notification = new NotificationCompat.Builder(this, id)

                    .setContentTitle("这是一个内容标题11")//设置通知栏标题
                    .setContentText("10") //设置通知栏显示内容
                    .setWhen(System.currentTimeMillis())//通知产生的时间。
                    // 会在通知信息里显示,通常是系统获取到的时间
                    .setSmallIcon(R.drawable.b)//设置通知小ICON
                   
                    .build();
        } else {
            NotificationCompat.Builder notificationBuilder =
                    new NotificationCompat.Builder(this, id)
                            .setContentTitle("这是一个内容标题")
                            .setContentText("1")
                            .setSmallIcon(R.drawable.a)
                            .setLargeIcon(BitmapFactory.decodeResource(getResources()
                                    , R.mipmap.ic_launcher));
//                                    .setOngoing(true);
            notification = notificationBuilder.build();

        }
        //第三步:对Builder进行配置:
        manager.notify(1, notification);

 

本文地址:https://blog.csdn.net/Aaron121314/article/details/107518585

相关标签: Android开发