How to Implement App Icon Badges
Tags: java, programming, tutorial
Published: 2022-05-30 10:48:55

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ma11ittna6ayehml60ou.png)

[HMS Core Push Kit](https://developer.huawei.com/consumer/en/doc/development/HMSCore-Guides/service-introduction-0000001050040060?ha_source=hms1) 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 " / >
```
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 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;
    }
}
```
### 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.
2. 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.