android.app.RemoteServiceException: Bad notification posted from package. Couldn't expand RemoteViews for: StatusBarNotification

4

I have implemented custom notification with RemoteViews widget. I tested it on Android 5.0.2 and Android 6.0. It was working fine. But after sometime, it started crashing everytime it received notification from GCM.

Crash Dump -

Process: package.name, PID: 27743
android.app.RemoteServiceException: Bad notification posted from package package.name: Couldn't expand RemoteViews for: StatusBarNotification(pkg=package.name user=UserHandle{0} id=1524095391 tag=null key=0|package.name|1524095391|null|10247: Notification(pri=0 contentView=package.name/0x7f040033 vibrate=null sound=null defaults=0x0 flags=0x10 color=0x00000000 category=recommendation vis=PUBLIC))
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1631)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

My layout for RemoteViews have Imageview inside LinearLayout and custom TextView classes which are extending TextView.

My code for creating notification is

    private void showCustomNotification(ABCNotification notification) {
        RemoteViews notificationView = new RemoteViews(getPackageName(), R.layout.layout_notification);
        PendingIntent contentPendingIntent = PendingIntent.getBroadcast(mContext, 0, notification.getContentIntent(), PendingIntent.FLAG_UPDATE_CURRENT);

                NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext);
                builder.setSmallIcon(R.drawable.ic_notification).setAutoCancel(true).setContentIntent(contentPendingIntent);

                // Set Notification Priority
                builder.setPriority(notification.getNotificationPriority());

                // Set Notification Category
                builder.setCategory(notification.getNotificationCategory());

                // Set Notification Visibility
                builder.setVisibility(notification.getNotificationVisibility());

notificationView.setTextViewText(R.id.tv_notification_title, notification.getTitle());
        notificationView.setTextViewText(R.id.tv_notification_message, notification.getMessage());
        notificationView.setTextViewText(R.id.tv_notification_subtext, notification.getSubtext());
        Notification notif = builder.build();
                if (null != notif) {
                    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
                        notif.bigContentView = notificationView;
                    }
                    notif.contentView = notificationView;
                    NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
                    notificationManager.notify(mNotificationId, notif);
                }
    }

My Layout for RemoteViews is something like this

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:paddingBottom="4dp"
            android:paddingTop="4dp">

            <com.abc.ui.customviews.fonts.TextViewYMRegular
                android:id="@+id/tv_notification_title"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="8dp"
                android:ellipsize="end"
                android:singleLine="true"
                android:textColor="@color/black"
                android:textSize="16sp" />

            <com.abc.ui.customviews.fonts.TextViewYMMedium
                android:id="@+id/tv_notification_message"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="8dp"
                android:textColor="@color/black"
                android:textSize="14sp" />

            <com.abc.ui.customviews.fonts.TextViewYMMedium
                android:id="@+id/tv_notification_subtext"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textColor="@color/black"
                android:textSize="12sp" />
        </LinearLayout>

</LinearLayout>

I have seen almost all the solution on stack overflow. Nothing seems to work.

Funny part is, it was working perfectly before. But started crashing after sometime. All in span of 2 hours.

android
push-notification
notifications
remoteview
custom-notification
asked on Stack Overflow Aug 5, 2016 by Code-Warrior

3 Answers

7

As commented by Mike, we cannot use custom views even if they are direct descendants of native views.

I was using TextViewYMRegular which was extending TextView. I changed all custom TextViews to native.

We can only use the classes listed here with RemoteViews.

answered on Stack Overflow Aug 5, 2016 by Code-Warrior
0

in my case I had

androidx.appcompat.widget.AppCompatTextView
androidx.appcompat.widget.AppCompatImageView

in my layout.xml!

I just changed them to ImageView and TextView and my problem solved!

answered on Stack Overflow Jun 19, 2020 by sana ebadi
0

Another twist on this issue - I implemented a dynamic feature module in my app, that contained a service and a notification. I had exactly the same crash on Android 6, when they layout file for the notification's RemoteViews as in the res directory of the dynamic module, while it worked fine on Android 10. I moved they layout resource to the main module res directory, and then on Android 6 it started working normally as well.

answered on Stack Overflow Aug 23, 2020 by gregko

User contributions licensed under CC BY-SA 3.0