Android应用程序远程服务异常:包发布的通知有问题。无法扩展StatusBarNotification的RemoteViews。

6

我已经使用RemoteViews小部件实现了自定义通知。我在Android 5.0.2和Android 6.0上进行了测试,一切正常。但是过了一段时间后,每次从GCM接收到通知时就会崩溃。

崩溃转储 -

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)

我是一名有用的助手,可以为您翻译文本。

我布局RemoteViews中使用了LinearLayout和自定义的TextView类,这些类都继承自TextView。

我的创建通知的代码如下:

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

我的 RemoteViews 布局大致如下

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

我看过几乎所有在堆栈溢出网站上的解决方案。似乎没有一个能够解决问题。

有趣的是,之前它一直都能够正常工作。但在两个小时内就开始崩溃了。


2
你不能在RemoteViews中使用自定义视图(View)。你只能使用此处列出的那些类。请注意,其中说“这些类的子类不受支持”。 - Mike M.
2
谢谢 Mike。似乎是自定义 TextViews 的问题。现在已经解决了。 :-) - Code-Warrior
3个回答

7

正如Mike所评论的,即使自定义视图是原生视图的直接子类,我们也无法使用它们。

我之前在使用TextViewYMRegular,它是继承自TextView的。我把所有自定义的TextView改为了原生的。

我们只能使用这里列出的类与RemoteViews一起使用。


1
在我的情况下,我有


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

在我的layout.xml文件中!

我只是将它们更改为ImageView和TextView,问题就解决了!


0
另一个关于这个问题的转折 - 我在我的应用程序中实现了一个动态特性模块,其中包含一个服务和一个通知。当通知的RemoteViews的布局文件在动态模块的res目录中时,我在Android 6上遇到了完全相同的崩溃,而在Android 10上则可以正常工作。我将布局资源移动到主模块的res目录中,然后在Android 6上它也开始正常工作了。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接