5

I am able to show Badge count with app icon on Samsung devices with following code -

<uses-permission android:name="com.sec.android.provider.badge.permission.READ" />
<uses-permission android:name="com.sec.android.provider.badge.permission.WRITE" />

    Intent intent = new Intent("android.intent.action.BADGE_COUNT_UPDATE");
    intent.putExtra("badge_count", badgeCount);
    intent.putExtra("badge_count_package_name", componentName.getPackageName());
    intent.putExtra("badge_count_class_name", componentName.getClassName());
sendBroadcast(intent);

But I am not able to achieve this on Redmi devices, though Facebook, Whatsapp etc are able to. Please let me know if you are aware of the Action Intent and the Extras to use for the Broadcast. Appreciate your help.

Krishnan V S
  • 1,124
  • 1
  • 13
  • 31

2 Answers2

1

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

jack
  • 129
  • 3
  • 1
    Thanks for your reply. You are right, they might have to enable, but the challenge I am facing is that I am not able to figure out what permission string I have to use and also what Intent string I have to set, for Redmi. It is varying from one manufacturer to another, as I found out with Samsung, HTC and Sony. Could not find out any helpful documentation on Redmi. – Krishnan V S Nov 12 '16 at 16:38
0

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

Shirish Herwade
  • 11,461
  • 20
  • 72
  • 111