【问题标题】:How to fix: android.app.RemoteServiceException: Bad notification posted from package *: Couldn't create icon: StatusBarIcon如何修复:android.app.RemoteServiceException:从包发布的错误通知 *:无法创建图标:StatusBarIcon
【发布时间】:2014-10-08 16:07:49
【问题描述】:

我在崩溃日志中看到以下异常:

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 是否正确。

【问题讨论】:

  • 我遇到了同样的错误(来自实时应用的崩溃报告)。我无法在我的设备上复制它。我目前认为这是因为人们修改了 .apk
  • 各位大佬,我有什么办法可以解决这个问题。我使用 png,但很少有应用崩溃

标签: android android-notifications android-resources android-drawable


【解决方案1】:

发生的事情是,我在 PendingIntent 包中包含了对图标的整数引用,后来在发布到 NotificationManager 时引用了该整数。

在获取整数引用和挂起的 Intent 之间,应用程序被更新并且所有可绘制引用都发生了变化。用于引用正确可绘制对象的整数现在引用了不正确的可绘制对象或根本没有引用(根本没有 - 导致此崩溃)

【讨论】:

  • 奇怪。换句话说,这是不可修复的,不是吗?我在我的应用中看到了同样的崩溃。
  • 它似乎无法修复。将来,我将不得不设计一种不同的方式来发布带有图像的通知......
  • 如果我们尝试将远程视图的 textSize 设置为 ret.setInt(id, "setTextSize", 20);它也会崩溃。我知道我们还必须提供单位,但是我该如何使用这种方法签名来做到这一点。有指针吗??
  • 当应用程序被杀死以更新时,是否可以清除通知和所有相关内容? (假设这将使应用程序避免该问题)
  • 如果您想引用 R.drawable.my_image,将其保存为字符串(bundle.putString("img", "my_image"),然后在需要时转换为实际的 @DrawableRes 整数,如下所示:ctx.resources.getIdentifier(bundle.getString("img"), "drawable", ctx.packageName)
【解决方案2】:

已知在通知中使用VectorXml 会导致此问题。使用png的

【讨论】:

  • 只使用了 PNG。
  • 就我而言,我在drawable 文件夹中有矢量,在drawable-hdpixhdpi 等中有png。但是drawable-mdpi 没有png,它在MDPI 设备上崩溃了发布通知。
【解决方案3】:

不要在 Kitkat 上使用 SVG!

每次我想在 Kitkat 上显示通知时都会遇到同样的问题。对我来说造成问题的原因是我已经定义了 xml 中的每个图标(来自 svg)、小图标和操作图标。在我用 png-s 替换它们之后,问题就在我身边解决了。

【讨论】:

  • 我可以确认。通知中的矢量可绘制对象导致 KitKat 上的 RemoteServiceException。
  • 在 KitKat 的情况下,当我在以下位置用 PNG 替换 XML 时问题得到解决: new NotificationCompat.Builder(context, CHANNEL_ID).setSmallIcon(R.drawable.my_icon)
  • 这对我有用。我正在运行 API 19 并使用 SVG 文件作为通知图标并给了我这个错误,直到我将它切换为一个小的 png 图标
【解决方案4】:

我的问题是我使用的图标

.setSmallIcon(R.drawable.ic_stat_push_notif)

没有相应地生成。根据official doc

如提供特定于密度的图标集和支持中所述 多个屏幕,您应该为所有通用创建单独的图标 屏幕密度,包括低、中、高和 超高密度屏幕。这可确保您的图标将显示 正确地跨越您的应用程序可以在其上的设备范围 已安装。

因此,完成上述内容的最佳方式是,我使用了 Roman Nurik 在https://romannurik.github.io/AndroidAssetStudio/index.html

上提供的通知生成器

这样,您可以使用图像(考虑到它必须具有透明背景)并让生成器为您生成不同大小的通知图标。

最重要的是,如果您浏览要使用的图像后图标生成器显示为白色实心圆形或正方形,则您的图像有问题,可能是因为它没有任何透明度,所以确保你有这个没问题。

【讨论】:

  • 就我而言,我在drawable 文件夹中有矢量,在drawable-hdpixhdpi 等中有png。但是drawable-mdpi 没有png,它在MDPI 设备上崩溃发布通知。
【解决方案5】:

android.app.RemoteServiceException:发布错误通知

我有同样的问题,但我已经解决了。我的问题是远程视图的“.xml 文件”。

在我的 xml 文件中,我在 LinearLayout 之间添加了一个 View 作为分隔符。

<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:id="@+id/view"
    android:background="#000000" />

上述View 组件创建了Bad 通知异常。这个异常原因只是Remoteviews的xml文件。

删除该视图组件后,我的代码正常执行,没有任何异常。所以我觉得通知抽屉不接受任何自定义视图。

所以你不要在RemoteView对象的.xml文件中绘制任何类似上面视图的东西。

【讨论】:

    【解决方案6】:

    在我的应用程序中,这种错误仅在升级过程中发生。如果在较新版本中资源id发生变化,AndroidRemoteView可能会找不到资源而抛出RemoteServiceException。如果您发布第 3 个版本并且不更改资源 ID,则错误可能会暂时消失

    可以通过编辑res/values/public.xmlres/values/ids.xml 来减少此类错误。如果资源 id 不在 public.xmlids.xml 中,编译器将生成一个单独的资源 id。当您更改资源名称或添加一些新资源时,id 可能会更改,并且某些设备可能无法找到它。

    所以步骤如下:

    1. 反编译apk文件,在res/values找到public.xmlids.xml
    2. 在您的应用中查找与 RemoteView 相关的所有资源并复制它们(字符串、尺寸、可绘制、布局、id、颜色...)
    3. 在您的源代码中的res/values 下创建public.xmlids.xml 并粘贴您刚刚复制的行

    注意:

    Gradle 1.3.0 及以上版本忽略本地public.xml。为了让它工作,你需要在你的build.gradle中添加一些脚本

    afterEvaluate {
        for (variant in android.applicationVariants) {
            def scope = variant.getVariantData().getScope()
            String mergeTaskName = scope.getMergeResourcesTask().name
            def mergeTask = tasks.getByName(mergeTaskName)
            mergeTask.doLast {
                copy {
                    int i=0
                    from(android.sourceSets.main.res.srcDirs) {
                        include 'values/public.xml'
                        rename 'public.xml', (i == 0? "public.xml": "public_${i}.xml")
                        i++
                    }
                    into(mergeTask.outputDir)
                }
            }
        }
    }
    

    注意: 此脚本不支持子模块项目。我正在尝试修复它。

    【讨论】:

      【解决方案7】:

      万一图标不重要,可以替换,

      R.drawable.your_icon
      

      android.R.drawable.some_standard_icon
      

      这行得通!

      【讨论】:

      • 非常感谢 :)
      【解决方案8】:

      在 Android Studio 3.0.0 及以上版本中,在 drawables 文件夹中添加新图像时,请选择 drawable 而不是 drawable-v24.

      如果您正在使用的图像已经是 (v24),只需将其复制并粘贴到其同一目录中(例如 drawables)。这次它会询问您哪个常规版本或 v24 - 只需确保它不是 v24 并再次尝试,这应该可以修复错误。

      【讨论】:

      • 注意 - 如果您在“Android”视图中,请将其切换到“项目”视图。在 Android 视图中,您可能看不到 drawables v24 目录
      • 我认为这对我来说很好。您可以将该矢量 xml 文件放在 drawable 和 drawable v24 中。小于等于marsha mellow的android版本会从drawable文件夹中提取,新版本会从drawable v24中提取。
      • 是的,它发生在我的案例中,我的 mipmap 仅适用于 v26
      【解决方案9】:

      当在我的类中使用从 FirebaseMessagingService 扩展的通知时,我遇到了 RemoteServiceException。我在 AndroidManifest.xml 中添加了以下代码:

      <meta-data
      android:name="com.google.firebase.messaging.default_notification_icon"
      android:resource="@drawable/ic_small" />
      

      资源 ic_small 通过方法 setSmallIcon(int icon) 设置在类 Notification.Builder 的实例中。

      【讨论】:

      • 这个元数据必须添加到 标签内吗?
      【解决方案10】:

      你通过了相同的图标

           <meta-data
              android:name="com.google.firebase.messaging.default_notification_icon"
              android:resource="@drawable/ic_stat_name" />
      

      你的通知

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

      【讨论】:

      • 我已将矢量图像作为资源传递,我认为它会导致 oneplus/android 6 设备上的崩溃。不确定 100%。
      【解决方案11】:

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

      原因:发生这种情况是因为我们为 RemoteViews 使用了矢量可绘制对象,而矢量可绘制对象在编译时生成了可绘制对象。另外我不确定为什么在我们升级应用程序时某些设备无法检测到生成的可绘制资源 ID。但是是的,这就是原因。

      重现 脚步 1.安装以前的版本。 2. 发送通知 3. 使用下一个版本代码更新构建 4.更新应用后不要打开应用并再次发送通知

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

      我希望这能解决问题。

      【讨论】:

        【解决方案12】:

        如果您在更新应用时看到通知,您可能会遇到这种情况。您可能需要做的是创建一个等待 PACKAGE_CHANGED 消息的 BroadcastReceiver,此时您关闭所有服务,这些服务也会关闭它们的相关通知。

         <receiver android:name=".MyBroadcastReceiver">
            <intent-filter>
                <action android:name="android.intent.action.PACKAGE_CHANGED" />
            </intent-filter>
        </receiver>
        

        【讨论】:

          【解决方案13】:

          您可以将该矢量 xml 文件放在 drawable 和 drawable v24 中。低于或等于Android 6的android版本会从drawable文件夹中提取,android 6以上的版本会从drawable v24中提取。

          【讨论】:

            【解决方案14】:

            我在捆绑包中设置通知时遇到了同样的问题。 我试过了,它解决了我的问题:

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

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

            【讨论】:

            • 为什么会有帮助?
            【解决方案15】:

            我也有同样的问题。问题出在您使用的图标上,我使用的是 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);
            

            你可以试试其他图片来代替这张图片

            【讨论】:

              【解决方案16】:

              我解决这个问题的方法:我的布局中有“坏”视图(例如:复选框) - 所以我删除了它们。

              RemoteViews 似乎只支持图像和文本(通过阅读文档来确认)。

              【讨论】:

                【解决方案17】:

                使用 android.support.constraint.ConstraintLayout 时出现 RemoteServiceException。将其更改为线性布局或相对 还有 android:layout_height="wrap_content" 用于容器

                【讨论】:

                  【解决方案18】:

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

                  【讨论】:

                    【解决方案19】:

                    请将自定义通知布局中的 &lt;android.support.v7.widget.AppCompatTextView 替换为 &lt;TextView

                    因为android.support.v7.widget.AppCompatTextViewandroid.support.v7.widget.AppCompatImageView 仅在运行时有效。

                    所以使用 TextViewImageView

                    【讨论】:

                      【解决方案20】:

                      如果有人仍然面临这个问题: 在您的可绘制文件夹中添加一个名为 ic_stat_ic_notification(或您喜欢的任何名称)的 png 文件

                      并在您的清单中添加以下几行

                      你可以在这里创建你的图标 - > https://romannurik.github.io/AndroidAssetStudio/icon

                      【讨论】:

                        【解决方案21】:

                        由于通知视图中 textview 中的 autolink 和 linksClickable 选项,我收到此错误:

                        <TextView
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_gravity="center_horizontal"
                                android:autoLink="all"
                                android:linksClickable="true"
                                android:text="@string/app_name"
                                android:textAppearance="?android:attr/textAppearanceMedium"
                                android:textColor="@color/white" />
                        

                        小心!

                        【讨论】:

                          【解决方案22】:

                          这是图标位置的问题。在我的情况下,通知图标在 drawable-anydpi-v24 中,我只是将图标复制到 drawable 文件夹中,错误就消失了。

                          【讨论】:

                            【解决方案23】:

                            同步项目,然后清理它, 文件>同步然后:构建>清理项目

                            希望对大家有帮助,对我有用

                            【讨论】:

                              【解决方案24】:

                              对我有用。注释图标的引用,从而可以毫无问题地使用 NotificationCompat.Builder。

                                  $msg_notificacion = [
                                                      'title'         => "titulonotif",
                                                     // "icon" : "myicon",
                                                      'body'          =>"cuerponotif"
                                                  ];
                              

                              【讨论】:

                                猜你喜欢
                                • 2014-09-18
                                • 2018-06-15
                                • 1970-01-01
                                • 1970-01-01
                                • 2018-06-18
                                • 1970-01-01
                                • 1970-01-01
                                • 1970-01-01
                                相关资源
                                最近更新 更多