我在崩溃日志中看到以下异常:
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是否正确.
发生的事情是,我在PendingIntent包中包含了对图标的整数引用,并且该整数稍后在发布到NotificationManager时被引用.
在获取整数引用和待处理的意图之间,应用程序已更新并且所有可绘制引用都已更改.用于引用正确drawable的整数现在引用了不正确的drawable或者根本没有引用(根本没有 - 导致此崩溃)
每当我想在Kitkat上发布通知时,我都会遇到同样的问题.导致这个问题的原因是我已经定义了xml中的每个图标(来自svg),小图标和动作图标.在我用png-s替换它们后,问题在我身边解决了.
VectorXml
已知在您的通知中使用会导致此问题.使用png
android.app.RemoteServiceException:发布错误通知
我有同样的问题,但我得到了解决.我的问题是远程视图的".xml文件".
在我的xml文件中,我View
在LinearLayout
for divider 之间添加了一个.
<View android:layout_ android:layout_ android:id="@+id/view" android:background="#000000" />
上面的View
组件创建了Bad通知异常.此例外原因仅是Remoteviews的xml文件.
删除该View组件后,我的代码正确执行,没有任何异常.所以我觉得通知抽屉不接受任何自定义视图.
因此,您不会在RemoteView
对象的.xml文件中绘制任何类似上述视图的内容.
我的问题是我使用的图标
.setSmallIcon(R.drawable.ic_stat_push_notif)
没有相应地生成。根据官方文件:
如提供特定于密度的图标集和支持多个屏幕中所述,您应该为所有通用的屏幕密度(包括低,中,高和超高密度屏幕)创建单独的图标。这样可以确保您的图标将在可以安装应用程序的一系列设备上正确显示。
因此,满足上述要求的最佳方法是使用Roman Nurik在https://romannurik.github.io/AndroidAssetStudio/index.html上提供的Notification Generator
这样,您可以使用图像(考虑到该背景必须具有透明背景),并让生成器为您生成通知图标的不同大小的工作。
最重要的是,如果浏览图像后使用的图标生成器显示白色填充的圆形或正方形,则图像存在问题,可能是因为它没有任何透明性,因此请确保你有这个确定。
在我的应用程序中,这种错误仅在升级期间发生.如果资源ID在较新版本中发生更改,则Android RemoteView
可能无法找到该资源并将其丢弃RemoteServiceException
.如果您发布第3版并且不更改资源ID,则错误可能只是暂时消失.
可以通过编辑res/values/public.xml
和减少这种错误res/values/ids.xml
.如果资源ID不在public.xml
或,则编译器将生成单独的资源ID ids.xml
.当您更改资源名称或添加一些新资源时,ID可能会更改,某些设备可能无法找到它.
所以步骤如下:
反编译apk文件并res/values
找到public.xml
和ids.xml
在您的应用中查找与RemoteView相关的所有资源并复制它们(字符串,尺寸,可绘制,布局,ID,颜色......)
创建public.xml
并ids.xml
在res/values
你的源代码,并粘贴你刚刚复制的行
注意:
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) } } } }
注意:此脚本不支持子模块项目.我正在努力解决它.