1

How to add small image(icon) at end of notification(Local notification) body. I am using NotificationCompat.Builder to create notification.I want to add small icon at the end of notification body.

Joe
  • 550
  • 2
  • 11
  • 21

2 Answers2

2

I think you can use spannable String because setContentText(CharSequence message) accepts CharSequence.

  String msg="Hello world";
    SpannableStringBuilder ssb = new SpannableStringBuilder(msg);
    ssb.setSpan(new ImageSpan(this, R.drawable.app_icon), msg.length()-1, msg.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);

And add it to message in notification builder.

 NotificationCompat.Builder  notification
                = new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.app_icon)
                .setStyle(new NotificationCompat.BigTextStyle().bigText(ssb))
                .setContentTitle("Hello")
                .setContentText(ssb);

I haven't tested it. But i think it should work. Watch out for index.

ADM
  • 20,406
  • 11
  • 52
  • 83
  • Your code add image to string,If I set string to textview works fine but in notification I am not getting image. – Joe Jan 19 '18 at 07:10
  • Yeah as i said i have not tested it . Its weird i though it should work with `CharSequence`. Thx for clarification . You should move forward with [RemoteView](https://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html#setCustomContentView(android.widget.RemoteViews)). Thx – ADM Jan 19 '18 at 07:43
0

You can use NotificationCompat.Builder#setCustomContentView(RemoteViews). In this related question you can see an example.

Take into account that the notification template has changed in Android N if you go that route, and usually the users expect a consistent UI. See Android N: Introducing upgraded Notifications

Xavier Rubio Jansana
  • 6,388
  • 1
  • 27
  • 50