自定义通知栏的实现

3,379 阅读2分钟

 实现一个自定义通知栏,我们分成两个部分来讲,一个是通知,一个是自定义的通知栏界面

普通通知栏

 首先我们先实现一个通知,一些参数的说明已经写了注释了,其中NotificationChannel 的设置会将Notification .Builder()的设置给覆盖掉,当我们的版本不是8.0时请自行实现震动铃声之类的

这里的setOngoing属性要注意一下,用于后台服务,不想让用户清除掉的,就设置为true,普通的通知,即用户可以直接清理掉的,那就设置为false

        Intent intent = new Intent(this, LoginActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);

	    String id = "chang_id";
        CharSequence name="name";
        int importance = NotificationManager.IMPORTANCE_DEFAULT;
        //Android8.0以上的手机使用该办法来实现通知栏
        NotificationChannel notificationChannel = new NotificationChannel(id, name, importance);
        String description="descroption";
        notificationChannel.setDescription(description);//配置通知渠道的属性
        notificationChannel.enableLights(true);//当设备支持的时候出现提示灯
        notificationChannel.setLightColor(Color.RED);//提示灯的颜色为红色
        notificationChannel.enableVibration(true);//当设备支持时出现震动
        notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
        NotificationManager notificationManager= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.createNotificationChannel(notificationChannel);

        Notification notification = new Notification.Builder(this, id)
                .setAutoCancel(true)//点击通知栏的时候移除通知栏
                .setContentTitle("contentTitle")
                .setContentText("contenttext")
                .setContentIntent(pendingIntent)//跳转到指定的页面
                .setSmallIcon(R.drawable.ic_launcher_background)
                .setOngoing(false)//设置true表示是一个正在活动的后台任务,不会被清除掉,比如音乐播放器
                .setWhen(System.currentTimeMillis())
                .build();
        notificationManager.notify(1,notification);

RemoteViews

 实现自定义通知栏时,我们先提一下RemoteViews,他是用来跨进程显示view的,主要是用在通知栏与窗口小工具上,他虽然不是一个View,但是继承自Parcelable,所以可以跨进程使用,支持的界面布局控件也是有限的(不支持自定义view)

增加自定义通知栏界面

 我们使用Notification.Builder的setCustomContentView来添加一个通知栏页面,修改上面的部分代码,添加一个RemoteViews,RemoteViews不能用fingViewById去获取控件,但是能通过set去设置一些控件的属性值,

R.layout.notion_layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <ImageView
        android:src="@mipmap/ic_launcher"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

   <LinearLayout
       android:orientation="vertical"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content">

       <TextView
       	   android:id="@+id/not_title"
           android:text="Title 这是 一个标题"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content" />

       <TextView
           android:text="XXXXXXX这是一段内容XXXXX"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content" />

   </LinearLayout>

</LinearLayout>
   RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.notion_layout);
   remoteViews.setTextViewText(R.id.not_title, "这是一个推送过来的标题");
   
   Notification notification = new Notification.Builder(this, id)
                .setAutoCancel(true)//点击通知栏的时候移除通知栏
                .setContentTitle("contentTitle")
                .setContentText("contenttext")
                .setContentIntent(pendingIntent)//跳转到指定的页面
                .setSmallIcon(R.drawable.ic_launcher_background)
                .setOngoing(false)//设置true表示是一个正在活动的后台任务,不会被清除掉,比如音乐播放器
                .setCustomContentView(remoteViews)
                .setWhen(System.currentTimeMillis())
                .build();
   notificationManager.notify(1,notification);

 自定义通知栏就这么实现了,当不添加RemoteViews的时候使用的是Notification.Builder设置的内容,使用了RemoteViews我们就能根据推送过来的内容去自定义展示不同的通知栏了,可以根据type去设置不同的通知栏,将通知栏封装成一个工具类