2

I am creating an app for a non-English market. Hence, I have to change the font of the notification. I have the following code. But whenever the notification comes, it is blank, there is no logo or text displayed. The small icon is shown at the top.

        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);

This is the 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"/>

suku
  • 10,507
  • 16
  • 75
  • 120
  • Well, it seems like a rich content style and a custom content `View` don't work together, though I've not found anything that explicitly says they don't. I also tried setting the `bigContentView` manually, but that was even worse. It looks like the only thing you can do in the "normal" notification area is set the title. A possible workaround would be to adjust the `drawer_image` to add some extra blank space at the top, and draw the title and text there instead. – Mike M. Jul 01 '16 at 12:19
  • 1
    Thanks. That was the problem. I have changed the layout to accommodate the image and have removed the `setStyle`. When I tried `bigContentView`, that also got messed up, I suppose because of this itself. Please write your comment as the answer. – suku Jul 01 '16 at 12:28
  • Oh, yeah, duh. Don't know why I didn't think of that. Good call. Cheers! – Mike M. Jul 01 '16 at 12:30

1 Answers1

0

It seems that a custom content View and a rich content style do not work well together, though I've not found any source - official or otherwise - that explicitly says they don't. If a style is set, apparently the only thing allowed in the normal Notification area is the title.

The solution is to just handle everything yourself, creating a RemoteViews that can hold everything in one layout and not use setStyle().

suku
  • 10,507
  • 16
  • 75
  • 120
Mike M.
  • 38,532
  • 8
  • 99
  • 95