How to show badge count with app icon on Redmi?

点点圈 提交于 2019-12-04 12:44:02

Perhaps, User needs to explicitly enable the option from the settings. Check here for reference: badge count on launcher icon

code to show badge on Mi/Xiomi mobiles

public static final String INTENT_ACTION = "android.intent.action.APPLICATION_MESSAGE_UPDATE";
public static final String EXTRA_UPDATE_APP_COMPONENT_NAME = "android.intent.extra.update_application_component_name";
public static final String EXTRA_UPDATE_APP_MSG_TEXT = "android.intent.extra.update_application_message_text";
private ResolveInfo resolveInfo;

@Override
public void executeBadge(Context context, ComponentName componentName, int badgeCount) throws ShortcutBadgeException {
    try {
        Class miuiNotificationClass = Class.forName("android.app.MiuiNotification");
        Object miuiNotification = miuiNotificationClass.newInstance();
        Field field = miuiNotification.getClass().getDeclaredField("messageCount");
        field.setAccessible(true);
        try {
            field.set(miuiNotification, String.valueOf(badgeCount == 0 ? "" : badgeCount));
        } catch (Exception e) {
            field.set(miuiNotification, badgeCount);
        }
    } catch (Exception e) {
        Intent localIntent = new Intent(
                INTENT_ACTION);
        localIntent.putExtra(EXTRA_UPDATE_APP_COMPONENT_NAME, componentName.getPackageName() + "/" + componentName.getClassName());
        localIntent.putExtra(EXTRA_UPDATE_APP_MSG_TEXT, String.valueOf(badgeCount == 0 ? "" : badgeCount));
        if (BroadcastHelper.canResolveBroadcast(context, localIntent)) {
            context.sendBroadcast(localIntent);
        }
    }
    if (Build.MANUFACTURER.equalsIgnoreCase("Xiaomi")) {
        tryNewMiuiBadge(context, badgeCount);
    }
}

@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();
        try {
            Field field = notification.getClass().getDeclaredField("extraNotification");
            Object extraNotification = field.get(notification);
            Method method = extraNotification.getClass().getDeclaredMethod("setMessageCount", int.class);
            method.invoke(extraNotification, badgeCount);
            mNotificationManager.notify(0, notification);
        } catch (Exception e) {
            throw new ShortcutBadgeException("not able to set badge", e);
        }
    }
}

This is copied code from github of a ShortcutBadger application on project link(I don't have idea about license/permissions for using this code)

Mi specific badge showing file code

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!