android - 如何使用通知图标修复小米特定的 RemoteServiceException?
问题描述
我们有很多特定于 Android 6 和 7 上的小米手机的崩溃:
Fatal Exception: android.app.RemoteServiceException: Bad notification posted from package x.y.z: Couldn't create icon: StatusBarIcon(icon=Icon(typ=RESOURCE pkg=x.y.z id=0x7f0200ad) visible user=0 )
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1715)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:163)
at android.app.ActivityThread.main(ActivityThread.java:6358)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:880)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:770)
我在网上找到了很多类似的崩溃报告和文章。这里有几个:
如何修复:android.app.RemoteServiceException:从包发布的错误通知 *:无法创建图标:StatusBarIcon
https://medium.com/@Miqubel/the-story-of-a-hard-to-fix-bug-ac6ed819cb49
但不同之处在于,我们仅在小米手机(Android 6 和 7)上存在这些问题,并且可能不会在更新期间出现,因为同一用户在同一版本中多次崩溃。
有趣的是,我在网上找不到关于这个特定案例的任何信息,而且我们周围没有任何小米手机。
我将通知设置为:
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, NOTIFICATION_CHANNEL_NAME, importance);
notificationChannel.enableLights(true);
notificationManager.createNotificationChannel(notificationChannel);
}
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID)
.setSmallIcon(R.drawable.ic_notification)
.setPriority(NotificationCompat.PRIORITY_MAX)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
.setContentText(body == null ? "" : body)
.setAutoCancel(true)
.setContentIntent(PendingIntent.getActivity(
context,
0,
pendingIntent,
PendingIntent.FLAG_UPDATE_CURRENT
));
我们也有 Facebook 通知,它们必须以类似的方式设置,但在不同的 Notification 类上。我不知道这是否相关。除了在制造商和 Android 版本检查中包装 setSmallIcon 和/或 setLargeIcon 方法之外,是否有人遇到过这个问题或有任何建议来解决这个问题?
编辑:我找不到解决方案,但这里有一些新想法:
我们发布了一个新版本,但从通知中排除小米用户没有帮助!现在我认为问题是由 ActivityThread.java 中的自定义代码引起的。MIUI 可能会在某些事件上从此处触发通知。这里的库存 Android 中有几十个事件,但没有一个会触发通知。但是我们的图标出了点问题,所以它们崩溃了。
但是我们的图标有什么问题?我们有一个 ic_notification,它可能不用于此目的。另一方面,ic_launcher 是一个 mipmap。也许是这个?但我找不到关于小米和 mipmap 的任何问题。
崩溃报告总是在多个应用版本中提到相同的资源 ID:0x7f0200ad。出于某种原因,这很特别吗?如何对我们的应用程序进行逆向工程以获取资源名称?
编辑2:
- 我用apktool对app进行了逆向工程,但是resource id不在public.xml中,貌似和R.java是等价的。我们的 ic_notification 和 ic_launcher 在列表中具有不同的 id。那么这是MIUI找不到的系统资源吗?
编辑 3:
- 其他人有同样问题的第一个证据:
https://xiaomi.eu/community/threads/miui-9.47247/
- 在波兰论坛上找到的临时解决方案:
https://pl.forum.elvenar.com/index.php?threads/problem-z-uruchomieniem-23566.3348/
最后一条评论翻译为:“我们对小米的问题有一个临时解决方案,请尝试在手机设置中禁用来自 Elvenar 应用程序的强制通知。重新启动应用程序后,错误应该会消失。”
编辑4:
我们正在使用 ShortcutBadger(版本 1.1.13)。这里它说我们应该对小米徽章使用不同的方法:
https://github.com/leolin310148/ShortcutBadger/wiki/Xiaomi-Device-Support
在 1.1.13 版本之后,他们删除了对小米的默认支持,您必须使用上述链接中的通知。
其他受影响的人使用这个吗?
解决方案
我和用户有同样的问题。我相信这是由以下代码引起的——从 APK 反编译我没有源代码,它来自Shortcutbadger
ResolveInfo.getIconResource() 返回了一个无效的资源 ID(0x7f0200ad,所有应用程序都相同,看起来它只在 MIUI10 上)因此崩溃。
iget-object v1, p0, Lme/leolin/shortcutbadger/impl/XiaomiHomeBadger;->a:Landroid/content/pm/ResolveInfo;
invoke-virtual/range {v1 .. v1}, Landroid/content/pm/ResolveInfo;->getIconResource()I
move-result v1
invoke-virtual {p1, v1}, Landroid/app/Notification$Builder;->setSmallIcon(I)Landroid/app/Notification$Builder;
作为用户,一个简单的解决方法是完全禁用该应用程序的通知——至少让它可用。
作为开发人员,使用自己的图标可能会更容易,或者在 Shortcutbadger 中添加一些错误处理。
builder.setSmallIcon(R.drawable.myicon);
更新:
我明白了现在发生的事情......快捷方式徽章中有一些奇怪的代码。resolveInfo 是默认的主页启动器(MIUI 主页)活动,resolveInfo.getIconResource() 是 miui 主页的图标
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
private void tryNewMiuiBadge(Context context, int badgeCount) throws ShortcutBadgeException {
if (resolveInfo == null) {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
resolveInfo = context.getPackageManager().resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
}
if (resolveInfo != null) {
NotificationManager mNotificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification.Builder builder = new Notification.Builder(context)
.setContentTitle("")
.setContentText("")
.setSmallIcon(resolveInfo.getIconResource());
Notification notification = builder.build();
从 miuihome.apk 反编译,这里是 0x7f0200ad。
<public type="drawable" name="icon_launcher" id="0x7f0200ad" />
那么为什么第 3 方应用会尝试用 miui home 的图标设置通知图标呢?是出于兼容性原因还是只是一个错误?我用上面的代码片段编写了一个简单的应用程序,在模拟器上测试,它失败但没有崩溃应用程序,可能与旧 MIUI 上的情况相同,因为 setSmallIcon(resID) 正在从自己的包中寻找具有 resID 的图标。好消息是,这不是 MIUI10 的错误,它应该只发生在使用上述代码的应用程序上。
推荐阅读
- mysql - 将数据库的sql脚本导入phpmyadmin后出现缺失索引错误的解决方法是什么
- apache-kafka - Spring cloud Stream kafka binder - 主题的自动配置
- php - 检测 Laravel 中不必要的文件和文件夹并删除它们
- c# - 限制可用整数的数量
- c++ - 为什么|| 操作员在我的情况下工作
- python - 在 IIS 10 上部署 Python Flask - 发布路由失败
- java - 如何使用 Jackson (Java) 正确打开 SSL 流到多个 URL?
- r - 如何将“base R”位置图迁移到“ggplot2”并避免“for”循环?
- excel - Excel 生成两张工作表之间的价格差异
- javascript - 错误:使用 google meet 链接创建活动时会议类型值无效