gpt4 book ai didi

android.app.RemoteServiceException : Bad notification posted from package. 无法扩展 RemoteViews:StatusBarNotification

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:45:24 29 4
gpt4 key购买 nike

我已经使用 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 中有 Imageview 和扩展 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>

关于stack overflow的解决方案我几乎都看过了。似乎没有任何效果。

有趣的是,它之前工作得很好。但一段时间后开始崩溃。全部在 2 小时内完成。

最佳答案

正如 Mike 所说,我们不能使用自定义 View ,即使它们是 native View 的直接后代。

我使用的是扩展 TextView 的 TextViewYMRegular。我将所有自定义 TextView 更改为原生。

我们只能使用类 listed here与 RemoteViews。

关于android.app.RemoteServiceException : Bad notification posted from package. 无法扩展 RemoteViews:StatusBarNotification,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38790702/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com