How to show badge count with app icon on Redmi?

冷暖自知 提交于 2019-12-06 07:23:28

问题


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.


回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/38685550/how-to-show-badge-count-with-app-icon-on-redmi

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