android.app.RemoteServiceException: 发布的通知有误

7
有时我的应用程序会出现这样的异常:
Fatal Exception: android.app.RemoteServiceException: Bad notification posted from package com.packagename: Couldn't expand RemoteViews for: StatusBarNotification(pkg=com.packagename user=UserHandle{0} id=136094146 tag=null score=0: Notification(pri=0 contentView=com.packagename/0x109007e vibrate=default sound=default defaults=0xffffffff flags=0x11 kind=[null]))
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
   at android.os.Handler.dispatchMessage(Handler.java:102)
   at android.os.Looper.loop(Looper.java:149)
   at android.app.ActivityThread.main(ActivityThread.java:5257)
   at java.lang.reflect.Method.invokeNative(Method.java)
   at java.lang.reflect.Method.invoke(Method.java:515)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
   at dalvik.system.NativeStart.main(NativeStart.java)

代码创建通知:

PendingIntent intent = PendingIntent.getActivities(this, id,
        notificationIntents, PendingIntent.FLAG_UPDATE_CURRENT);


int color = ContextCompat.getColor(this, R.color.notif_background);

NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
        .setContentText(description)
        .setSmallIcon(getSmallIcon())
        .setLargeIcon(getLargeIcon())
        .setColor(color)
        .setDefaults(NotificationCompat.DEFAULT_ALL)
        .setAutoCancel(true)
        .setStyle(new NotificationCompat.BigPictureStyle().bigLargeIcon(largeIcon))
        .setContentIntent(intent);

if (title != null) {
    notificationBuilder.setContentTitle(title)
            .setTicker(title)
            .setStyle(new NotificationCompat.BigTextStyle()
                    .setBigContentTitle(title).bigText(description));
} else {
    notificationBuilder.setStyle(new NotificationCompat.BigTextStyle()
            .bigText(description));
}

if (image != null) {
    notificationBuilder
            .setStyle(new NotificationCompat.BigPictureStyle()
                    .bigPicture(image).setSummaryText(description));
}
android.app.Notification notification = notificationBuilder.build();
notificationManager.notify(id, notification);

我已经在stack overflow上看过几乎所有的解决方案,它们都显示这个问题与自定义布局有关。但是我没有使用自定义布局。我无法理解到底出了什么问题。有人能帮忙吗?


以下。 Vidha你有没有找到解决方案? - karan
我遇到了完全相同的问题。你找到任何解决方案了吗?我没有使用自定义视图,但是这个错误提示说无法扩展远程视图让我感到困惑。 - Apsaliya
6个回答

3

当我在使用通知图标时,遇到了同样的问题,问题出在使用了 .webp 格式的图标上。 请确保对于 Android 版本为 KitKat 及以下的设备,使用 .png 格式的图标。


0

你的样式发生了冲突——标题有时可能为空,因此将应用NotificationCompat.BigTextStyle()并设置bigText,然后如果图像不为空,则应用NotificationCompat.BigPictureStyle()。问题是,虽然设置了bigText,但样式却不相关。


0

当您在.setSmallIcon(R.drawable.ic_notification) 中使用Vector时,这个崩溃会发生。我使用带渐变项的矢量图像。我删除了渐变部分。它在5.0(棒棒糖)和6.0(棉花糖)通知上开始正常工作。


0

这也会在应用程序更新之间发生,与此问题非常相似。

此答案提供了一个可能的解释:

在获取整数引用和挂起意图之间,应用程序已更新并且所有可绘制引用都已更改。以前引用正确可绘制对象的整数现在引用不正确的可绘制对象或根本没有可绘制对象(导致崩溃)。


0

-1

对我来说,这项工作:

RemoteViews remoteViews = new RemoteViews(getApplicationContext().getPackageName(), R.layout.toolbar_custom_layout);

NotificationCompat.Builder builder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
                        .setContent(remoteViews)
                        .setContentTitle("example example")
                        .setSmallIcon(R.drawable.app_icon_example)
                        .setStyle(new NotificationCompat.DecoratedCustomViewStyle())
                        .setContentInfo("content example");

NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, builder.build());

同时,在通知布局中将RelativeLayout设置为您的主要父级。在我的情况下,我将ConstraintLayout作为父级,当我将其更改为RelativeLayout时,一切开始正常工作。 希望这能帮到您。


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