如何解决:android.app.RemoteServiceException: Bad notification posted from package *: Couldn't create icon: StatusBarIcon。

153
我在崩溃日志中看到了以下异常信息:
android.app.RemoteServiceException: Bad notification posted from package com.my.package: Couldn't create icon: StatusBarIcon(pkg=com.my.package user=0 id=0x7f02015d level=0 visible=true num=0 )
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1456)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:146)
    at android.app.ActivityThread.main(ActivityThread.java:5487)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
    at dalvik.system.NativeStart.main(Native Method)

我正在使用以下方法从AlarmManager设置的PendingIntent中的IntentService发布我的通知。所有传递的值都来自PendingIntent / IntentService中的bundle extras。

/**
 * Notification 
 *
 * @param c
 * @param intent
 * @param notificationId
 * @param title
 * @param message
 * @param largeIcon
 * @param smallIcon
 */
public static void showNotification(Context c, Intent intent,
        int notificationId, String title, String message, int largeIcon,
        int smallIcon) {
    PendingIntent detailsIntent = PendingIntent.getActivity(c,
            notificationId, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    // BUILD
    NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(
            c);
    // TITLE
    mNotifyBuilder.setContentTitle(title).setContentText(message);

    // ICONS
    mNotifyBuilder.setSmallIcon(smallIcon);
    if (Util.isAndroidOSAtLeast(Build.VERSION_CODES.HONEYCOMB)) {
        Bitmap large_icon_bmp = ((BitmapDrawable) c.getResources()
                .getDrawable(largeIcon)).getBitmap();
        mNotifyBuilder.setLargeIcon(large_icon_bmp);
    }

    mNotifyBuilder.setContentIntent(detailsIntent);
    mNotifyBuilder.setVibrate(new long[] { 500, 1500 });
    mNotifyBuilder.setTicker(message);
    mNotifyBuilder.setContentText(message);

    // NOTIFY
    NotificationManager nm = (NotificationManager) c
            .getSystemService(Context.NOTIFICATION_SERVICE);
    nm.notify(notificationId, mNotifyBuilder.build());
}

从我看到的其他答案来看,我遇到的异常是因为setSmallIcon()没有被正确调用。

我已经检查并重复检查了传递的资源ID都是正确的。


3
我遇到了同样的错误(来自实时应用程序的崩溃报告),但在我的设备上无法复现。我目前认为这可能是因为有人修改了.apk文件。 - darken
大家好,我想问一下有什么方法可以解决这个问题?我使用PNG格式的图片,但是有时会导致应用程序崩溃。 - Roman Soviak
26个回答

3
您需要通过相同的图标。
     <meta-data
        android:name="com.google.firebase.messaging.default_notification_icon"
        android:resource="@drawable/ic_stat_name" />

and you notification

 NotificationCompat.Builder notificationBuilder =
                new NotificationCompat.Builder(this, channelId)
                        .setSmallIcon(R.drawable.ic_stat_name)
                        .setContentTitle("Title")
                        .setContentText(messageBody)
                        .setAutoCancel(true)
                        .setSound(defaultSoundUri)
                        .setContentIntent(pendingIntent);

2
我已将矢量图像作为资源传递,我认为这可能导致了一加/安卓6设备的崩溃。不确定是否百分之百正确。 - Alexey

2

我也遇到了同样的问题,但已经解决了。

原因:这是因为我们在RemoteViews中使用矢量图形,并且矢量图形会在编译时生成drawable。此外,我不确定为什么有些设备无法检测到在升级应用程序时生成的drawable资源ID,但是这就是原因。

复现 步骤 1. 安装先前的版本。 2. 发送通知 3. 更新具有下一个版本代码的构建 4. 在更新应用程序后不要打开应用程序并再次发送通知

解决方案 将矢量图形替换为普通的drawable(.png或.jpg)文件。

希望这解决了问题。


1

这不是关于图像类型(矢量、png等),而是你所引用的文件夹。

对我而言,造成错误的原因是设备(Nougat)引用了位于v26文件夹中的图标。

解决方案:只需将相关图标添加到其他文件夹中,问题就会消失。


1

您可以将该矢量 XML 文件放置在 drawable 和 drawable-v24 文件夹中。Android 6 及以下版本将从 drawable 文件夹中获取,而 Android 6 以上的新版本将从 drawable-v24 文件夹中获取。


0

当我在 bundle 中设置通知时,遇到了同样的问题。我尝试了以下方法,解决了我的问题:

builder.setLargeIcon(large_icon);
builder.setSmallIcon(R.drawable.small_icon);

确保在调用setSmallIcon()之前先调用setLargeIcon()。


7
为什么这会有帮助? - limitium

0

我也遇到了同样的问题。问题出在你使用的图标上,我使用的是android.R.drawable.stat_sys_download。去drawables.xml中粘贴这个图标。

<resources>
 <item name="ic_start_download" type="drawable">@android:drawable/stat_sys_download</item>
</resource>

然后在你的代码中

builder.setSmallIcon(R.drawable.ic_start_download);

你可以尝试使用其他图片代替这张图片


0
如果仍然遇到此问题: 在您的drawable文件夹中添加一个名为ic_stat_ic_notification的png文件(或任何您喜欢的名称)
并在您的清单中添加以下几行
您可以在此处创建自己的图标 - > https://romannurik.github.io/AndroidAssetStudio/icon

0

(这不是当前问题的解决方案,但可以帮助遇到类似核心问题的人)我也遇到了同样的问题,但在我的情况下,我使用了一个损坏的.png文件,导致了相同的问题。所以我删除了它,并重新包含了正确的.png文件。


0
请在自定义通知布局中将<android.support.v7.widget.AppCompatTextView替换为<TextView

因为android.support.v7.widget.AppCompatTextViewandroid.support.v7.widget.AppCompatImageView仅在运行时起作用。

因此,请使用TextViewImageView


0
在我的Flutter Android应用程序中遇到了这个错误,检查了所有资源是否存在并命名正确,但仍然无法工作。我使用的是mipmap资源。我将其更改为drawable,并将通知图标放置在drawble文件夹中,解决了问题,希望能帮助后来的人。

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