3

I need to show a notification in my android application.

I am using the following code:

NotificationCompat.Builder mBuilder= new NotificationCompat.Builder(baseContext).setLargeIcon(large_icon)
                .setSmallIcon(R.drawable.message_received_small_icon)
                .setContentIntent(pendingIntent)
                .setAutoCancel(true)
                .setSound(soundUri);
                .setContentTitle("New Message");
                .setContentText("video");

This notification shows , Message Received icon on the left. To the right of the received message icon, it shows "DHan Nexus" as title and below it, it shows "Photo".

But instead of showing only "photo" string, i want to show camera icon + "photo" string . I have not found any way to show camera icon in the setContentText() API. Please help how to achieve this. Do i need to make a custom layout or any default approach will work .

Here is the image to make te question more clear:

enter image description here

i tried to use spannableString to show both icon and text . But it looks to be not working .

CharSequence cs = getContentIcon(text);
.setContentText(cs.toString());
    private CharSequence getContentIcon(String text)
    {
        Drawable image = ContextCompat.getDrawable(baseContext, R.drawable.camera_icon);

        image.setBounds(0, 0, image.getIntrinsicWidth(), image.getIntrinsicHeight());
        // Replace blank spaces with image icon
        SpannableString sb = new SpannableString("                      "+text);
        ImageSpan imageSpan = new ImageSpan(image, ImageSpan.ALIGN_BASELINE);
        sb.setSpan(imageSpan, 0, 20, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        return sb;
    }
Pardeep Kr
  • 479
  • 2
  • 6
  • 25

2 Answers2

1

You can use RemoteViews with custom layouts.

Note that Android 7 Nougat has some changes in the notifications (i haven't dived into it yet). Below code are for below Nougat.

// for standard notification
RemoteViews views = new RemoteViews(getPackageName(), R.layout.notification_simple);

// for expanded notification
// RemoteViews bigViews = new RemoteViews(getPackageName(), R.layout.notification_expanded);

views.setImageViewBitmap(R.id.picture, YOUR_IMAGE_BITMAP_HERE);
views.setTextViewText(R.id.text, YOUR_TEXT_HERE);

NotificationCompat.Builder notificationBuilder = 
        new NotificationCompat.Builder(this)
                .setSmallIcon(YOUR_NOTIFICATION_ICON)
                .setCustomContentView(views);
                // .setCustomBigContentView(bigViews); // if you've set it

notification = notificationBuilder.build();
ashazar
  • 714
  • 5
  • 11
0

Here i have written answer: https://stackoverflow.com/a/69086821/4498813 duplicating same here:

To display larger images/icons, you need to set custom remote view and customize your layout.xml.

To add small Emoji icons you can just add icon's Unicode into string.
To find Emoji icons go to here: https://emojipedia.org/google/

Example:

NotificationCompat.Builder(context, channelId)
        .setContentTitle("Your Title")
        .setContentText("\uD83D\uDCC4  -> This is Page Facing Up emoji icon")
Mitul Varmora
  • 3,832
  • 1
  • 12
  • 21