使用自定义通知布局时获取空白通知

时间:2016-07-01 11:19:20

标签: android android-notifications

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

        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"/>

1 个答案:

答案 0 :(得分:0)

自定义内容View和丰富的内容样式似乎无法很好地协同工作,但我没有找到任何明确表示不会使用的来源 - 官方或其他来源。如果设置了样式,那么正常Notification区域中唯一允许的是标题。

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