harmony 鸿蒙Managing the Notification Badge
Managing the Notification Badge
The system provides APIs for setting the notification badge, which is displayed in the upper right corner of the application icon on the home screen to notify the user of the count of unread notifications.
When a new notification arrives, the count on the badge is incremented by 1.
After a notification is read, the count on the badge is decremented by 1. If there is no unread notification, the badge is not displayed.
Available APIs
If badgeNumber is set to 0, badges are cleared; if the value is greater than 99, 99+ is displayed on the badge.
You can use either of the following methods to increase the count on the badge:
When publishing a notification, pass the badgeNumber parameter in NotificationRequest. After the notification is received, the count on the badge is incremented.
Call the setBadgeNumber() API to set the count on the badge.
To decrease the count on the badge, call the setBadgeNumber() API.
API | Description |
---|---|
setBadgeNumber(badgeNumber: number, callback: AsyncCallback<void>): void | Sets the number count on the badge. |
How to Develop
- Import the NotificationManager module.
import { notificationManager } from '@kit.NotificationKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
const TAG: string = '[PublishOperation]';
const DOMAIN_NUMBER: number = 0xFF00;
- Increase the count on the badge.
When publishing a notification, pass the badgeNumber parameter in NotificationRequest. For details, see Publishing a Basic Notification.
In this example, the setBadgeNumber API is called to add a badge. This API is called after a new notification is published.
```ts
let setBadgeNumberCallback = (err: BusinessError): void => {
if (err) {
hilog.error(DOMAIN_NUMBER, TAG, `Failed to set badge number. Code is ${err.code}, message is ${err.message}`);
return;
}
hilog.info(DOMAIN_NUMBER, TAG, `Succeeded in setting badge number.`);
}
let badgeNumber = 9;
notificationManager.setBadgeNumber(badgeNumber, setBadgeNumberCallback);
```
- Decrease the count on the badge.
After a notification is read, the application needs to call the API to set the number of remaining unread notifications. The badge is then updated.
```ts
let setBadgeNumberCallback = (err: BusinessError): void => {
if (err) {
hilog.error(DOMAIN_NUMBER, TAG, `Failed to set badge number. Code is ${err.code}, message is ${err.message}`);
return;
}
hilog.info(DOMAIN_NUMBER, TAG, `Succeeded in setting badge number.`);
}
let badgeNumber = 8;
notificationManager.setBadgeNumber(badgeNumber, setBadgeNumberCallback);
```
FAQs
setBadgeNumber is an asynchronous API. When setBadgeNumber is used to set badges consecutively, you should not set the value until the previous setting is complete, ensuring that the execution sequence meets the expectation.
Negative example
Independent API calling cannot guarantee a proper calling sequence during actual execution.
The sample code is as follows:
let badgeNumber: number = 10; notificationManager.setBadgeNumber(badgeNumber).then(() => { hilog.info(DOMAIN_NUMBER, TAG, `setBadgeNumber 10 success.`); }); badgeNumber = 11; notificationManager.setBadgeNumber(badgeNumber).then(() => { hilog.info(DOMAIN_NUMBER, TAG, `setBadgeNumber 11 success.`); });
Positive example
Dependencies existing between multiple API callings ensures that the next setting can be performed only after the previous setting is complete.
The sample code is as follows:
let badgeNumber: number = 10; notificationManager.setBadgeNumber(badgeNumber).then(() => { hilog.info(DOMAIN_NUMBER, TAG, `setBadgeNumber 10 success.`); badgeNumber = 11; notificationManager.setBadgeNumber(badgeNumber).then(() => { hilog.info(DOMAIN_NUMBER, TAG, `setBadgeNumber 11 success.`); }); });
你可能感兴趣的鸿蒙文章
harmony 鸿蒙Notification Kit (User Notification Service)
harmony 鸿蒙Publishing a Live View Notification (for System Applications Only)
harmony 鸿蒙Canceling a Notification
harmony 鸿蒙Clearing Repeated Notifications Across Devices
harmony 鸿蒙Cross-Device Notification Management (for System Applications Only)
harmony 鸿蒙Cross-Device Notification Overview
harmony 鸿蒙Requesting Notification Authorization
harmony 鸿蒙Introduction to Notification Kit
harmony 鸿蒙Enabling Quick Reply for Cross-device Notifications
- 所属分类: 后端技术
- 本文标签:
热门推荐
-
2、 - 优质文章
-
3、 gate.io
-
8、 golang
-
9、 openharmony
-
10、 Vue中input框自动聚焦