android 自定义通知消息设置背景色不生效,导致部分机型显示白色字体,白色文字,用户以为不显示

为什么要自定义 notification

android 自带的通知控件一般不符合公司要求,所以需要自定义notification。

怎么自定义

自定义时需要使用 RemoteViews
  NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(MainActivity.this, "TestMess"); 
  // 一定要设置小图标  .setSmallIcon(R.mipmap.ic_launcher_round) 否则会崩溃
  Notification notification = notificationBuilder
                .setSmallIcon(R.mipmap.ic_launcher_round)   
                .setAutoCancel(true).build();
  RemoteViews views =
                new RemoteViews(context.getPackageName(), R.layout.notification_layout);
        notification.contentView = views;
        notification.contentView.setViewVisibility(R.id.ll_review, View.VISIBLE);
		// 注意要把notification 的背景隐藏,否则部分机型不生效,设置的布局文件的背景色不生效
        notification.contentView.setViewVisibility(R.id.notification_background, View.GONE);

        // 图标
        Bitmap headBitmap = BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher);
        views.setImageViewBitmap(R.id.imageview_notifi_head, headBitmap);
        // 标题
        views.setTextViewText(R.id.textview_notifi_title, "testTitle");
        // 内容
        views.setTextViewText(R.id.textview_notifi_chat_content,  "testContent");
        // 时间
        views.setTextViewText(R.id.textview_notifi_time, "testTime");

容易忽略

注意要把notification 的背景隐藏,否则部分机型不生效,设置的布局文件的背景色不生效
notification.contentView.setViewVisibility(R.id.notification_background, View.GONE);
将 notification_layout.xml 布局文件中  在对应的 view 上设置颜色值和大小

8.0通知不显示

android 8.0 以后对通知栏有了渠道的概念,同一个渠道会自动折叠。但需要适配否则不显示
 //  notificationBuilder.setChannelId(String channelId); 一般使用 packageName 可以保证唯一性
 notificationBuilder.setChannelId(context.getPackageName());

猜你喜欢

转载自blog.csdn.net/ff_hh/article/details/89212267