Badge API (4.x)
This document explains how to use the Mobile Push Android SDK 4.x to set and clear the badge on your app's desktop icon.
This document applies to Mobile Push Android SDK 4.x. The API and examples described are based on version 4.0.0. For applications that use version 3.x, refer to the corresponding 3.x API documentation.
Scope
As of the 4.0.0 release, this API is compatible with Huawei, HONOR, and vivo devices. Badge display depends on the device's operating system, default desktop, and the app's notification and badge settings. Always test this feature on your target devices.
Before you begin, initialize the SDK and obtain a CloudPushService instance from com.alibaba.sdk.android.push.noonesdk.PushServiceFactory. For Huawei devices, the desktop must support badge display. HONOR devices support numeric badges starting from Magic UI 6.0. Configure vendor-required permissions and settings as described in the badge usage instructions.
Set and clear badge
Sets the number to display on your app's desktop badge. The num parameter specifies an absolute value, not an increment to the current count. Passing 0 clears the badge.
Class: com.alibaba.sdk.android.push.CloudPushService.
API definition
void setBadgeNum(Context context, int num);
Parameters
|
Parameter |
Type |
Required |
Description |
|
context |
Context |
Yes |
The application context. Using |
|
num |
int |
Yes |
The number to display on the badge. This value is absolute, not incremental. A positive number sets the count, 0 clears the badge, and any negative number is treated as 0. |
Return value and callback: This is a void method with no result callback. To verify the outcome, check the badge display on a target device's desktop.
Example: Set the badge
Run the following code after the SDK is initialized. The context parameter must be a valid Android Context for your app. Each call sets an absolute value. This example sets the badge count to 1.
CloudPushService pushService = PushServiceFactory.getCloudPushService();
// Set the app's badge count to 1.
pushService.setBadgeNum(context.getApplicationContext(), 1);val pushService = PushServiceFactory.getCloudPushService()
// Set the app's badge count to 1.
pushService.setBadgeNum(context.applicationContext, 1)Example: Clear the badge
After a user acknowledges the related notification or event, call the following code to clear the badge.
PushServiceFactory.getCloudPushService()
.setBadgeNum(context.getApplicationContext(), 0);PushServiceFactory.getCloudPushService()
.setBadgeNum(context.applicationContext, 0)Troubleshooting
Ensure that your app uses a supported system desktop and is permitted to display badges. Verify your startup Activity and all vendor-required permission settings. If you use both this local API and server-side badge parameters in push notifications, ensure that you manage the badge count consistently.
Related documents
For integration steps, see Integrate Android SDK (4.x).
For vendor permissions, desktop settings, and server-side badge parameters, see App badges for Android.
For apps that use version 3.x, see Badge API (3.x).