1# Managing the Notification Badge 2 3The 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. 4 5When a new notification arrives, the count on the badge is incremented by 1. 6 7After a notification is read, the count on the badge is decremented by 1. If there is no unread notification, the badge is not displayed. 8 9 10## Available APIs 11 12If **badgeNumber** is set to **0**, badges are cleared; if the value is greater than **99**, **99+** is displayed on the badge. 13 14- You can use either of the following methods to increase the count on the badge: 15 16 - When publishing a notification, pass the **badgeNumber** parameter in [NotificationRequest](../reference/apis-notification-kit/js-apis-inner-notification-notificationRequest.md#notificationrequest-1). After the notification is received, the count on the badge is incremented. 17 18 - Call the [setBadgeNumber()](../reference/apis-notification-kit/js-apis-notificationManager.md#notificationmanagersetbadgenumber10) API to set the count on the badge. 19 20- To decrease the count on the badge, call the [setBadgeNumber()](../reference/apis-notification-kit/js-apis-notificationManager.md#notificationmanagersetbadgenumber10) API. 21 22 | **API**| **Description**| 23 | -------- | -------- | 24 | setBadgeNumber(badgeNumber: number): Promise\<void\> | Sets the number count on the badge.| 25 26 27## How to Develop 28 291. Import the **NotificationManager** module. 30 31 ```ts 32 import { notificationManager } from '@kit.NotificationKit'; 33 import { hilog } from '@kit.PerformanceAnalysisKit'; 34 import { BusinessError } from '@kit.BasicServicesKit'; 35 36 const TAG: string = '[PublishOperation]'; 37 const DOMAIN_NUMBER: number = 0xFF00; 38 ``` 39 402. Increase the count on the badge. 41 42 When publishing a notification, pass the **badgeNumber** parameter in [NotificationRequest](../reference/apis-notification-kit/js-apis-inner-notification-notificationRequest.md#notificationrequest-1). For details, see [Publishing a Text Notification](text-notification.md). 43 44 In this example, the **setBadgeNumber** API is called to add a badge. This API is called after a new notification is published. 45 46 ```ts 47 let badgeNumber: number = 9; 48 notificationManager.setBadgeNumber(badgeNumber).then(() => { 49 hilog.info(DOMAIN_NUMBER, TAG, `Succeeded in setting badge number.`); 50 }).catch((err: BusinessError) => { 51 hilog.error(DOMAIN_NUMBER, TAG, `Failed to set badge number. Code is ${err.code}, message is ${err.message}`); 52 }); 53 ``` 54 553. Decrease the count on the badge. 56 57 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. 58 59 ```ts 60 let badgeNumber: number = 8; 61 notificationManager.setBadgeNumber(badgeNumber).then(() => { 62 hilog.info(DOMAIN_NUMBER, TAG, `Succeeded in setting badge number.`); 63 }).catch((err: BusinessError) => { 64 hilog.error(DOMAIN_NUMBER, TAG, `Failed to set badge number. Code is ${err.code}, message is ${err.message}`); 65 }); 66 ``` 67 68## FAQs 69 70**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. 71 72- Negative example 73 74 Independent API calling cannot guarantee a proper calling sequence during actual execution. 75 76 The sample code is as follows: 77 78 ```ts 79 let badgeNumber: number = 10; 80 notificationManager.setBadgeNumber(badgeNumber).then(() => { 81 hilog.info(DOMAIN_NUMBER, TAG, `setBadgeNumber 10 success.`); 82 }); 83 badgeNumber = 11; 84 notificationManager.setBadgeNumber(badgeNumber).then(() => { 85 hilog.info(DOMAIN_NUMBER, TAG, `setBadgeNumber 11 success.`); 86 }); 87 ``` 88 89- Positive example 90 91 Dependencies existing between multiple API callings ensures that the next setting can be performed only after the previous setting is complete. 92 93 The sample code is as follows: 94 95 ```ts 96 let badgeNumber: number = 10; 97 notificationManager.setBadgeNumber(badgeNumber).then(() => { 98 hilog.info(DOMAIN_NUMBER, TAG, `setBadgeNumber 10 success.`); 99 badgeNumber = 11; 100 notificationManager.setBadgeNumber(badgeNumber).then(() => { 101 hilog.info(DOMAIN_NUMBER, TAG, `setBadgeNumber 11 success.`); 102 }); 103 }); 104 ``` 105