DEV Community

Jackson for HMS Core

Posted on • Updated on

How to Implement App Icon Badges

When users unlock their phones, they will often see a red oval or circle in the upper right corner of some app icons. This red object is called an app icon badge and the number inside it is called a badge count. App icon badges intuitively tell users how many unread messages there are in an app, giving users a sense of urgency and encouraging them to tap the app icon to read the messages. When used properly, icon badges can help improve the tap-through rate for in-app messages, thereby improving the app's user stickiness.

Image description

HMS Core Push Kit provides an API for configuring app icon badges and allows developers to encapsulate the badge parameter in pushed messages. So, how do we go about implementing app icon badges? The detailed procedure is as follows.

I. Setting an App Icon Badge Using the Client API

Supported platforms:

  • OS version: EMUI 4.1 or later
  • Huawei Home version: 6.3.29
  • Supported device: Huawei devices

Badge development:

1) Declare required permissions.

< uses - permission android: name = "android.permission.INTERNET" / >
    <
    uses - permission android: name = "com.huawei.android.launcher.permission.CHANGE_BADGE " / >
Enter fullscreen mode Exit fullscreen mode

2) Pass data to the Huawei Home app to display a badge for the specified app.

Bundle extra = new Bundle();
extra.putString("package", "xxxxxx");
extra.putString("class", "yyyyyyy");
extra.putInt("badgenumber", i);
context.getContentResolver().call(Uri.parse("content://com.huawei.android.launcher.settings/badge/"), "change_badge", null, extra);
Enter fullscreen mode Exit fullscreen mode

Key parameters:

  • package: app package name.
  • class: entry activity class of the app that needs to display a badge.
  • badgenumber: number displayed in the badge.
boolean mIsSupportedBade = true;
if (mIsSupportedBade) {
    setBadgeNum(num);
}
/** set badge number*/
public void setBadgeNum(int num) {
    try {
        Bundle bunlde = new Bundle();
        // com.test.badge is your package name
        bunlde.putString("package", "com.test.badge"); 
        // com.test. badge.MainActivity is your apk main activity
        bunlde.putString("class", "com.test. badge.MainActivity");
        bunlde.putInt("badgenumber", num);                
        this.getContentResolver().call(Uri.parse("content://com.huawei.android.launcher.settings/badge/"), "change_badge", null, bunlde);
    } catch (Exception e) {
        mIsSupportedBade = false;
    }
}
Enter fullscreen mode Exit fullscreen mode

Special situations:

1) Whether to continue displaying a badge when the app is opened and closed depends on the passed value of badgenumber. (The badge is not displayed if the badgenumber value is 0 and displayed if the badgenumber value is greater than 0.)
2) If the app package or class changes, the developer needs to pass the new app package or class.
3) Before calling the badge API, the developer does not need to check whether Huawei Home supports the badge function. If Huawei Home does not support the badge function, the API will throw an exception. The developer can add the try … catch(Exception e) statement to the place where the API is called to prevent app crashes.

II. Settings an App Icon Badge Using the Push SDK

In the downlink messaging API of Push Kit, three parameters in BadgeNotification are used to set whether to display the badge and the number displayed in the badge.

Parameter Mandatory Type Description
add_num Yes integer Accumulative badge number, which is an integer ranging from 1 to 99. For example, an app currently has N unread messages. If this parameter is set to 3, the number displayed in the app badge increases by 3 each time a message that contains this parameter is received, that is, the number equals N+3.
class Yes string Class name in App package name+App entry activity format. Example: com.example.hmstest.MainActivity
set_num Yes integer Badge number, which is an integer ranging from 0 to 99. For example, if this parameter is set to 10, the number displayed in the app badge is 10 no matter how many messages are received. If both set_num and add_num are set, the value of set_num will be used.

Pay attention to the following when setting these parameters:

  1. The value of class must be in the format App package name+App entry activity. Otherwise, the badge cannot be displayed.
  2. The add_num parameter requires that the EMUI version be 8.0.0 or later and the push service version be 8.0.0 or later.
  3. The set_num parameter requires that the EMUI version be 10.0.0 or later and the push service version be 10.1.0 or later.
  4. By default, the badge number will not be cleared when a user starts the app or taps and clears a notification message. To enable an app to clear the badge number, the app developer needs to perform development based on the relevant badge development guide.
  5. The class parameter is mandatory, and the add_num and set_num parameters are optional. If both of add_num and set_num are left empty, the badge number is incremented by 1 by default.

Top comments (0)