如何修复:android.app.RemoteServiceException:从包中发布的错误通知*:无法创建图标:StatusBarIcon
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25317659/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to fix: android.app.RemoteServiceException: Bad notification posted from package *: Couldn't create icon: StatusBarIcon
提问by FishStix
I'm seeing the following exception in crash logs:
我在崩溃日志中看到以下异常:
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)
I'm posting my Notification from an IntentService from a PendingIntent set via the AlarmManager using the following method. All values passed in here are from the bundle extras in the PendingIntent / IntentService.
我正在使用以下方法通过 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());
}
From what I've seen of other answers - the exception I'm seeing happens when setSmallIcon()
is not called properly.
从我看到的其他答案来看 - 我看到的异常发生在setSmallIcon()
没有正确调用时。
I've checked and double checked that the Resource IDs being passed are all correct.
我已经检查并仔细检查了传递的资源 ID 是否全部正确。
采纳答案by FishStix
What was happening was, I was including the integer reference to the icon in the PendingIntent bundle, and that integer was later being referenced while being posted to the NotificationManager.
发生的事情是,我在 PendingIntent 包中包含了对图标的整数引用,并且该整数后来在发布到 NotificationManager 时被引用。
In between getting the integer reference and the pending intent going off, the app was updated and all of the drawable references changed. The integer that used to reference the correct drawable now referenced either the incorrect drawable or none at all (none at all - causing this crash)
在获取整数引用和挂起的 Intent 之间,应用程序已更新并且所有可绘制引用都已更改。用于引用正确可绘制对象的整数现在引用了不正确的可绘制对象或根本没有(根本没有 - 导致此崩溃)
回答by Bubunyo Nyavor
Using VectorXml
inside your notification has been known to cause this issue. Use png's
VectorXml
已知在您的通知中使用会导致此问题。使用 png
回答by bendaf
Don't use SVG on Kitkat!
不要在 Kitkat 上使用 SVG!
I had the same issue every time when I wanted to show a notification on Kitkat. What caused the problem for me is that I have defined every icon in xml (from svg), the small icon and the action icon also. After I have replaced them with png-s the problem solved at my side.
每次我想在 Kitkat 上显示通知时都会遇到同样的问题。对我造成问题的原因是我已经定义了 xml(来自 svg)中的每个图标,小图标和动作图标。在我用 png-s 替换它们之后,问题在我身边解决了。
回答by Kona Suresh
android.app.RemoteServiceException: Bad notification posted
android.app.RemoteServiceException:发布错误通知
I had the same issue, but I was resolved. My problem is ".xml file" of Remote view.
我有同样的问题,但我已经解决了。我的问题是远程视图的“.xml 文件”。
In my xml file I was added one View
in between the LinearLayout
for divider.
在我的 xml 文件中,我View
在LinearLayout
for 分隔符之间添加了一个。
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:id="@+id/view"
android:background="#000000" />
The above View
component creating the Bad notification exception. This Exception reason is only xml file of Remoteviews.
上述View
组件创建了错误通知异常。此异常原因仅是 Remoteviews 的 xml 文件。
After removing that View component, My code executed properly, without any exception. So I felt that Notification drawer not accepting any customized views.
删除该 View 组件后,我的代码正确执行,没有任何异常。所以我觉得通知抽屉不接受任何自定义视图。
So you don't draw any thing like the above view in the .xml file of RemoteView
object.
所以你不会在RemoteView
对象的 .xml 文件中绘制任何类似上面视图的东西。
回答by Carlos Daniel
My problem was that the icon I was using on
我的问题是我使用的图标
.setSmallIcon(R.drawable.ic_stat_push_notif)
wasn't generated accordingly. According to the official doc:
没有相应地生成。根据官方文档:
As described in Providing Density-Specific Icon Sets and Supporting Multiple Screens, you should create separate icons for all generalized screen densities, including low-, medium-, high-, and extra-high-density screens. This ensures that your icons will display properly across the range of devices on which your application can be installed.
如提供特定密度图标集和支持多屏幕中所述,您应该为所有通用屏幕密度创建单独的图标,包括低、中、高和超高密度屏幕。这可确保您的图标在可以安装应用程序的设备范围内正确显示。
So the best way to fullfill the above, I used Notification Generatorprovided by Roman Nurik on https://romannurik.github.io/AndroidAssetStudio/index.html
所以最好的方法来完成上述,我使用了由 Roman Nurik 在https://romannurik.github.io/AndroidAssetStudio/index.html上提供的通知生成器
In that way, you can use an image (taking into consideration that this has to have transparent background) and let the generator do the job for you generating the different sizes for notification icons.
通过这种方式,您可以使用图像(考虑到它必须具有透明背景)并让生成器为您生成不同大小的通知图标。
The most important thing is that if the icon generator after you browse the image you are going to use shows you a white filled circle or square, there are problems with your image, maybe because it doesn't have any transparencies, so make sure that you has this ok.
最重要的是,如果在您浏览您要使用的图像后,图标生成器向您显示一个白色的实心圆圈或正方形,则您的图像存在问题,可能是因为它没有任何透明度,因此请确保你有这个。
回答by thundertrick
In my app, this kind of bug happens only during upgrading. If the resource id changes in the newer version, Android RemoteView
may fail to find the resource and throw out the RemoteServiceException
. If you publish a 3rd version and do not change the resource id, the bugs may disappear only temporarily.
在我的应用程序中,这种错误仅在升级期间发生。如果资源 id 在较新版本中发生变化,AndroidRemoteView
可能无法找到资源并抛出RemoteServiceException
. 如果您发布了第 3 个版本并且不更改资源 id,则错误可能只是暂时消失。
It is possible to reduce this kind of bugs by editing res/values/public.xml
and res/values/ids.xml
. Compiler will generate an individual resource id if the resource id is not in public.xml
or ids.xml
. When u change the resource name or add some new resources, the id may change and some devices may fail to find it.
可以通过编辑res/values/public.xml
和res/values/ids.xml
. 如果资源 id 不在public.xml
或 中,编译器将生成一个单独的资源 id ids.xml
。当您更改资源名称或添加一些新资源时,id 可能会更改,并且某些设备可能无法找到它。
So the step is as following:
所以步骤如下:
- Decompile the apk file and in
res/values
find thepublic.xml
andids.xml
- Find all resources related to RemoteView in your app and copy them ( strings, dimen, drawable, layout, id, color... )
- Create
public.xml
andids.xml
underres/values
in your source code and paste the lines u just copied
- 反编译apk文件并在其中
res/values
找到public.xml
和ids.xml
- 在您的应用中找到与 RemoteView 相关的所有资源并复制它们(字符串、尺寸、可绘制、布局、id、颜色...)
- 创建
public.xml
并ids.xml
在res/values
你的源代码,并粘贴你刚刚复制的行
Note:
笔记:
Gradle 1.3.0 and above ignore the local public.xml
. To make it work, u need to add some script in your build.gradle
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)
}
}
}
}
Note: This script does not support submodules project. I am trying to fix it.
注意:此脚本不支持子模块项目。我正在尝试修复它。
回答by Tarun Umath
You have pass same icon
你有相同的图标
<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);
回答by Ashwin Balani
Just in case the icon is not important, you can replace,
以防万一图标不重要,您可以替换,
R.drawable.your_icon
To
到
android.R.drawable.some_standard_icon
This works!
这有效!
回答by Haider Malik
In Android Studio version 3.0.0 and above, when adding a new image in the drawables folder choose drawable instead of drawable-v24.
在 Android Studio 3.0.0 及以上版本中,在 drawables 文件夹中添加新图像时,请选择 drawable 而不是 drawable-v24。
If the image,you are using, is alread a (v24) just copy it and paste it in its same directory (eg. drawables). This time it will ask you which regular or v24 - just make sure its not the v24 and try it again this should fix the error.
如果您正在使用的图像已经是 (v24),只需将其复制并粘贴到其同一目录中(例如 drawables)。这次它会询问您是常规版还是 v24 - 只需确保它不是 v24,然后再试一次,这应该可以解决错误。
回答by Вячеслав Бондаренко
I had RemoteServiceException when use Notification in my class extends from FirebaseMessagingService. I added the following code to AndroidManifest.xml:
在我的类中使用 Notification 从 FirebaseMessagingService 扩展时,我遇到了 RemoteServiceException。我在 AndroidManifest.xml 中添加了以下代码:
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@drawable/ic_small" />
Alsoresource ic_small set in instance of a class Notification.Builder by method setSmallIcon(int icon).
还通过方法 setSmallIcon(int icon) 在类 Notification.Builder 的实例中设置资源 ic_small。