[Huawei Developers] Mike-mei:
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.

View attachment 5624581

HMS Core Push Kit provides an API for configuring app icon badges and allows developers to encapsulate the badge parameter in pushed messages.

It is a well-known fact that many users find such messages annoying, and a feeling like this can damage how users evaluate an app and make it impossible for the push message to play its role in boosting user engagement. This makes app icon badges a necessary complement to push messages, because unlike the latter, the former appears silently and thus won't bother users to check in-app events when it's inconvenient for them to do so.

So, how do we go about implementing app icon badges for an app? The detailed procedure is as follows.

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 " / >
"com.huawei.android.launcher.permission.CHANGE_BADGE " / >

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);

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 the badge number. */
public void setBadgeNum(int num) {
try {
Bundle bunlde = new Bundle();
// com.test.badge is the app package name.
bunlde.putString("package", "com.test.badge");
// com.test. badge.MainActivity is the main activity of an app.
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;
}
}

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.

Setting 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.

Conclusion​

App icon badges have become an integral part for mobile apps in different industries. Those little dots can serve as a quick reminder which urges users to check what is happening within an app, in a way that is imperceptible to users. In this sense, app icon badges can be used to boost app engagement, which can well explain why they are widely adopted by mobile app developers.

As proven by this post, the API from Push Kit is an effective way for app icon badge implementation. The API enables developers to equip push notifications with app icon badges whose parameters are customizable, for example, whether the badge is displayed for an app and the number inside a badge.

The whole implementation process is straightforward and has just a few requirements on hardware and software, as well as several parameter setting matters that need attention. Using the API, developers can easily implement the app icon badge feature for their apps.