Home » Android » android – Couldn't expand RemoteViews for: StatusBarNotification

android – Couldn't expand RemoteViews for: StatusBarNotification

Posted by: admin June 15, 2020 Leave a comment

Questions:

This question has been asked before but none of the answers helped me hence posting my case.

I am trying to build a custom notification using a layout file. But I get the following error:

android.app.RemoteServiceException: Bad notification posted from
package com.eswaraj.app.eswaraj: Couldn’t expand RemoteViews for:
StatusBarNotification(pkg=com.eswaraj.app.eswaraj user=UserHandle{0}
id=8888 tag=null score=0: Notification(pri=0
contentView=com.eswaraj.app.eswaraj/0x7f030052 vibrate=null sound=null
defaults=0x0 flags=0x0 kind=[null]))

My layout file is:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="left">

<com.makeramen.RoundedImageView
    android:id="@+id/nImage"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:src="@drawable/profile_image"
    android:padding="15dp"
    android:scaleType="fitCenter"
    app:riv_corner_radius="30dip"
    app:riv_border_width="2dip"
    app:riv_border_color="#333333"
    app:riv_mutate_background="true"
    app:riv_oval="true"/>

<TextView
    android:id="@+id/nTitle"
    android:text="eSwaraj: Your voice is heard"
    android:layout_toRightOf="@+id/nImage"
    android:paddingTop="12dp"
    android:textSize="20dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<TextView
    android:id="@+id/nMessage"
    android:layout_below="@+id/nTitle"
    android:layout_toRightOf="@+id/nImage"
    android:text="Your comaplaint viewed"
    android:textSize="15dp"
    android:paddingTop="5dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<TextView
    android:id="@+id/nTime"
    android:layout_toRightOf="@+id/nImage"
    android:layout_below="@+id/nMessage"
    android:textSize="13dp"
    android:paddingTop="5dp"
    android:text="12:23PM"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

</RelativeLayout>

And I am sending notification as follows:

public void sendNotification(Context caller, Class<?> activityToLaunch, Bitmap icon,   GcmMessageDto gcmMessageDto, int id) {
        Intent toLaunch;
        RemoteViews remoteViews = new RemoteViews(caller.getPackageName(), R.layout.notification);
        if(activityToLaunch != null) {
            toLaunch = new Intent(caller, activityToLaunch);
       }
       else {
            toLaunch = new Intent();
        }
        PendingIntent intentBack = PendingIntent.getActivity(caller, 0, toLaunch, 0);

        NotificationManager notifier = (NotificationManager) caller.getSystemService(Context.NOTIFICATION_SERVICE);
        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(caller)
                    .setSmallIcon(android.R.drawable.ic_notification_overlay)
                    .setContentIntent(intentBack);

        remoteViews.setTextViewText(R.id.nMessage, gcmMessageDto.getMessage());
        remoteViews.setTextViewText(R.id.nTime, new Date().toString());
        if(icon != null) {
            remoteViews.setImageViewBitmap(R.id.nImage, icon);
        }
        mBuilder.setContent(remoteViews);

        notifier.notify(id, mBuilder.build());
    }

Any help would be appreciated.

How to&Answers:

Shortly put, the

com.makeramen.RoundedImageView

is messing with your job.

Not all views can be used in RemoteViews which are used for building custom notifications. According to this official page only the following can be used for creating RemoteViews:

  • FrameLayout
  • LinearLayout
  • RelativeLayout
  • GridLayout
  • AnalogClock
  • Button
  • Chronometer
  • ImageButton
  • ImageView
  • ProgressBar
  • TextView
  • ViewFlipper
  • ListView
  • GridView
  • StackView
  • AdapterViewFlipper

So you must find a workaround for your modified ImageView class and use plain ImageView instead.

Answer:

I also had the same issue, i was using a custom TextView, resorting to stock TextView solved my problem.

Answer:

I had a same issue. As according to the accepted answer there are specific controls that can be used in the notification. I had a View in my layout for notification as below.

<View
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="0.04"/>

This was causing the crash. I removed it and it worked fine. Hope it helps someone.