通知栏添加自定义布局 , 一定要注意
添加channel, 还有构建的时候把id加进去。
String channelId = null; if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { channelId = "1"; NotificationChannel channel = new NotificationChannel(channelId, "Channel1", NotificationManager.IMPORTANCE_DEFAULT); channel.enableLights(true); //是否在桌面icon右上角展示小红点 channel.setLightColor(Color.RED); //小红点颜色 channel.setShowBadge(true); //是否在久按桌面图标时显示此渠道的通知 mNotificationManager.createNotificationChannel(channel); } // channelId非常重要,不设置通知栏不展示 // builder.setChannelId(context.packageName) Notification notification = new NotificationCompat.Builder(getContext(), channelId) .setContentTitle("通知2") // 创建通知的标题 .setContentText("这是第二个通知") // 创建通知的内容 .setSmallIcon(R.drawable.icon_app) // 创建通知的小图标 .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.icon_app)) // 创建通知的大图标 /* * 是使用自定义视图还是系统提供的视图,上面4的属性一定要设置,不然这个通知显示不出来 */ .setDefaults(Notification.DEFAULT_ALL) // 设置通知提醒方式为系统默认的提醒方式 .setContent(getRemoteView()) // 通过设置RemoteViews对象来设置通知的布局,这里我们设置为自定义布局 .setChannelId(channelId)//channelId非常重要,不设置通知栏不展示 .build(); // 创建通知(每个通知必须要调用这个方法来创建) mNotificationManager.notify(2, notification); // 发送通知