This topic describes how to set badges when you push notifications to HarmonyOS.
Feature description
Alibaba Cloud Mobile Push provides badge parameters for HarmonyOS applications. You can use these parameters to set a badge number or an incremental badge number for push notifications. After a notification is pushed, the badge number is displayed on the application icon on the HarmonyOS home screen. This feature is supported by both the Alibaba Cloud channel and the HarmonyOS channel.
Badge settings
OpenAPI provides two parameters to set badges:
HarmonyBadgeSetNum: The badge number. This parameter specifies the value to be displayed on the badge.
HarmonyBadgeAddNum: The incremental badge number. The final badge number is the sum of the original badge number and this value. If you set both HarmonyBadgeSetNum and HarmonyBadgeAddNum, only HarmonyBadgeSetNum takes effect.
You can set these parameters as needed when you push a notification to display the badge.
Handle badges for custom notifications
When a notification is sent through the Alibaba Cloud channel, you can customize the notification. For custom notifications, the client application is responsible for how the notification is displayed. Therefore, the client must also handle the badge display.
Badge data retrieval
Badge fields have been added to the PushNotification data. The definitions are as follows:
/**
* Push data
*/
export interface PushNotification extends BasePushData {
// Other fields are omitted.
/**
* The badge number to set.
*/
badgeSetNum?: number;
/**
* The incremental badge number.
*/
badgeAddNum?: number;
}badgeSetNum: The badge number. This corresponds to the HarmonyBadgeSetNum parameter of the push.
badgeAddNum: The incremental badge number. This corresponds to the HarmonyBadgeAddNum parameter of the push. If you set both the HarmonyBadgeSetNum and HarmonyBadgeAddNum parameters in a push, only the badgeSetNum parameter is sent. The badgeAddNum parameter is ignored.
Set the incremental badge number
In the SDK, the incremental badge number is implemented using the NotificationRequest badgeNumber field. Therefore, when you create a NotificationRequest, you must set the badgeNumber field based on badgeAddNum.
The SDK provides the helper method createNotificationRequest to create a NotificationRequest . This method internally handles the setting of the badgeNumber field. The following code is an example:
import {
aliyunPush,
ExtensionNotification,
IPushListener,
PushError,
PushMessage,
PushNotification
} from '@aliyun/push';
import { notificationManager } from '@kit.NotificationKit';
import { BusinessError } from '@kit.BasicServicesKit';
/**
* Implementation of the push callback interface to accept push data.
*/
class MyPushListener implements IPushListener {
onReceiveNotification(data: PushNotification | ExtensionNotification): boolean {
// ************* Custom notification begin *************
aliyunPush.getPushHelper().createNotificationRequest(data).then((request) => {
// The createNotificationRequest method has already set the badgeNumber field based on data.badgeAddNum. No separate processing is required.
// The application can adjust the fields of the request as needed to customize the effect.
// Publish the notification.
notificationManager.publish(request).then(() => {
console.debug(`Successfully published the custom notification`)
if (data.badgeSetNum !== undefined && data.badgeSetNum >= 0) {
// For custom notifications, you must handle the badge yourself. For data.badgeSetNum, you must set it here.
notificationManager.setBadgeNumber(data.badgeSetNum);
}
// To unify the logic, you can also actively call the onShowNotification method here.
this.onShowNotification(data);
}).catch((e: BusinessError) => {
console.error(`Failed to publish the custom notification: ${e.code} ${e.message}`)
})
}).catch((e: PushError) => {
console.error(`Failed to create the notification request: ${e.code} ${e.message}`)
})
// ************* Custom notification end *************
// Return true. The application handles the notification.
return true;
}
onShowNotification(data: PushNotification | ExtensionNotification): void {
// Handle the notification display event.
}
onReceiveMessage(data: PushMessage): void {
// Handle the push message.
}
}
// Other code is omitted.Set the badge number
In the SDK, you can set the badge number using the notificationManager.setBadgeNumber method when you customize a notification. The following code is an example:
import {
aliyunPush,
ExtensionNotification,
IPushListener,
PushError,
PushMessage,
PushNotification
} from '@aliyun/push';
import { notificationManager } from '@kit.NotificationKit';
import { BusinessError } from '@kit.BasicServicesKit';
/**
* Implementation of the push callback interface to accept push data.
*/
class MyPushListener implements IPushListener {
onReceiveNotification(data: PushNotification | ExtensionNotification): boolean {
// ************* Custom notification begin *************
aliyunPush.getPushHelper().createNotificationRequest(data).then((request) => {
// The createNotificationRequest method has already set the badgeNumber field based on data.badgeAddNum. No separate processing is required.
// The application can adjust the fields of the request as needed to customize the effect.
// Publish the notification.
notificationManager.publish(request).then(() => {
console.debug(`Successfully published the custom notification`)
if (data.badgeSetNum !== undefined && data.badgeSetNum >= 0) {
// For custom notifications, you must handle the badge yourself. For data.badgeSetNum, you must set it here.
notificationManager.setBadgeNumber(data.badgeSetNum);
}
// To unify the logic, you can also actively call the onShowNotification method here.
this.onShowNotification(data);
}).catch((e: BusinessError) => {
console.error(`Failed to publish the custom notification: ${e.code} ${e.message}`)
})
}).catch((e: PushError) => {
console.error(`Failed to create the notification request: ${e.code} ${e.message}`)
})
// ************* Custom notification end *************
// Return true. The application handles the notification.
return true;
}
onShowNotification(data: PushNotification | ExtensionNotification): void {
// Handle the notification display event.
}
onReceiveMessage(data: PushMessage): void {
// Handle the push message.
}
}
// Other code is omitted.