使用自定义通知布局时收到空白通知

Getting blank notification when using custom notification layout

我正在为非英语市场创建一个应用程序。因此,我必须更改通知的字体。我有以下代码。但是每当通知到来时,它都是空白的,没有显示徽标或文本。小图标显示在顶部。

        DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
        int width = displayMetrics.widthPixels;

        Bitmap bitmapTitle = Bitmap.createBitmap(width - 72, 84, Bitmap.Config.ARGB_8888);
        Canvas canvasTitle = new Canvas(bitmapTitle);
        Paint paintTitle = new Paint();
        paintTitle.setAntiAlias(true);
        paintTitle.setSubpixelText(true);
        paintTitle.setTypeface(AppApplication.hindiFont); //Font in Hindi
        paintTitle.setStyle(Paint.Style.FILL);
        paintTitle.setColor(ContextCompat.getColor(context, R.color.positive));
        paintTitle.setTextSize(70);
        paintTitle.setFakeBoldText(true);
        paintTitle.setTextAlign(Paint.Align.LEFT);
        canvasTitle.drawText("pqrst", 80, 60, paintTitle);

        Bitmap bitmapText = Bitmap.createBitmap(width - 72, 84, Bitmap.Config.ARGB_8888);
        //Similar code as above for bitmapText

        int notificationID = (int) System.currentTimeMillis();
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        Intent notificationIntent = new Intent(context, MainActivity.class);
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        RemoteViews contentView = new RemoteViews(context.getPackageName(), R.layout.notification_row);
        contentView.setImageViewBitmap(R.id.rnotification_iv_title, bitmapTitle);
        contentView.setImageViewBitmap(R.id.rnotification_iv_text, bitmapText);

        Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        Notification.Builder notificationBuilder = new Notification.Builder(context);
        Notification notification = notificationBuilder
                .setSmallIcon(R.drawable.small_icon)
                .setPriority(Notification.PRIORITY_HIGH)
                .setSound(alarmSound)
                .setAutoCancel(true)
                .setWhen(System.currentTimeMillis())
                .setDefaults(Notification.DEFAULT_VIBRATE | Notification.DEFAULT_LIGHTS)
                .setStyle(new Notification.BigPictureStyle().bigPicture(BitmapFactory.decodeResource(context.getResources(), R.drawable.drawer_image)))
                .build();
        notification.contentIntent = pendingIntent;
        notification.contentView = contentView;
        notificationManager.notify(notificationID, notification);

这是xml

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/custom_notification"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:padding="2dp">

<ImageView
    android:id="@+id/rnotification_iv_title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="2dp"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:layout_alignParentTop="true"/>

<ImageView
    android:id="@+id/rnotification_iv_text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="2dp"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:layout_below="@+id/rnotification_iv_title"
    android:layout_alignParentLeft="true"/>

自定义内容 View 和丰富的内容样式似乎不能很好地协同工作,尽管我没有找到任何来源(官方或其他)明确表示它们不能协同工作。如果设置了样式,显然正常 Notification 区域中唯一允许的是标题。

解决方案是自己处理所有事情,创建一个 RemoteViews 可以将所有内容放在一个布局中而不使用 setStyle()