首页 > 解决方案 > 如何使用通知图标修复小米特定的 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 方法之外,是否有人遇到过这个问题或有任何建议来解决这个问题?

编辑:我找不到解决方案,但这里有一些新想法:

编辑2:

编辑 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 版本之后,他们删除了对小米的默认支持,您必须使用上述链接中的通知。

其他受影响的人使用这个吗?

标签: androidpush-notificationxiaomi

解决方案


我和用户有同样的问题。我相信这是由以下代码引起的——从 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 的错误,它应该只发生在使用上述代码的应用程序上。


推荐阅读