如何使用通知图标修复小米特定的 RemoteServiceException?

How to fix Xiaomi specific RemoteServiceException with notification icon?

我们在 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)

我在网上找到了很多类似的崩溃报告和文章。这是一些:

How to fix: android.app.RemoteServiceException: Bad notification posted from package *: Couldn't create icon: 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 通知,必须以类似的方式设置,但在不同的通知上 class。我不知道这是否相关。有没有人 运行 对此有任何建议,除了在制造商中包装 setSmallIcon and/or setLargeIcon 方法和 Android 版本检查之外,如何解决这个问题?

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

编辑 2:

编辑 3:

https://xiaomi.eu/community/threads/miui-9.47247/

https://pl.forum.elvenar.com/index.php?threads/problem-z-uruchomieniem-23566.3348/

最后一条评论 t运行 指向: "We have a temporary solution to the problem with Xiaomi, please try to disable notifications forced from the Elvenar application in the phone settings. After restarting the application, the error should disappear."

编辑 4:

我们正在使用 ShortcutBadger(版本 1.1.13)。这里说我们应该使用不同的方法来获得小米徽章:

https://github.com/leolin310148/ShortcutBadger/wiki/Xiaomi-Device-Support

在 1.1.13 版本之后,他们删除了对小米的默认支持,您必须使用上面的通知 link。

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

我们买了红米Note 4X。事情是这样的:

  • 设备是 MIUI 8.5。通知按预期工作。没有崩溃。

  • 我们通过OTA更新升级到MIUI 9.5。当我们打开应用程序或在某些屏幕之间切换时,奇怪的通知开始出现。仍然没有崩溃。

  • 我们通过OTA更新升级到MIUI 10.1。使用以前版本的应用程序在启动时崩溃。当我将 ShortcutBadger 更新到 1.1.22 时,崩溃消失了。很明显,这是因为小米设备没有在新版本的库中执行 applyCount() 方法(他们必须使用 applyNotification() 代替),但我没有深入挖掘。

我对任何能够详细解释发生的事情的人开放这个问题和赏金。否则,到目前为止我们对这个结果很满意。对于下一个版本,我可能会尝试修复小米的徽章和通知,因为它们显示不正确。

我和用户有同样的问题。我相信这是由以下代码引起的——从 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);

更新:

我知道现在发生了什么...shortcutbadge 中的一些奇怪代码。 resolveInfo 是默认的主页启动器(MIUI 主页)activity 和 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 的错误,它应该只发生在使用上述代码的应用程序上。