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 the **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). 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, callback: AsyncCallback\<void\>): 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). For details, see [Publishing a Basic 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 setBadgeNumberCallback = (err: BusinessError): void => { 48 if (err) { 49 hilog.error(DOMAIN_NUMBER, TAG, `Failed to set badge number. Code is ${err.code}, message is ${err.message}`); 50 return; 51 } 52 hilog.info(DOMAIN_NUMBER, TAG, `Succeeded in setting badge number.`); 53 } 54 55 let badgeNumber = 9; 56 notificationManager.setBadgeNumber(badgeNumber, setBadgeNumberCallback); 57 ``` 58 593. Decrease the count on the badge. 60 61 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. 62 63 ```ts 64 let setBadgeNumberCallback = (err: BusinessError): void => { 65 if (err) { 66 hilog.error(DOMAIN_NUMBER, TAG, `Failed to set badge number. Code is ${err.code}, message is ${err.message}`); 67 return; 68 } 69 hilog.info(DOMAIN_NUMBER, TAG, `Succeeded in setting badge number.`); 70 } 71 72 let badgeNumber = 8; 73 notificationManager.setBadgeNumber(badgeNumber, setBadgeNumberCallback); 74 ``` 75 76## FAQs 77 78**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. 79 80- Negative example 81 82 Independent API calling cannot guarantee a proper calling sequence during actual execution. 83 84 The sample code is as follows: 85 86 ```ts 87 let badgeNumber: number = 10; 88 notificationManager.setBadgeNumber(badgeNumber).then(() => { 89 hilog.info(DOMAIN_NUMBER, TAG, `setBadgeNumber 10 success.`); 90 }); 91 badgeNumber = 11; 92 notificationManager.setBadgeNumber(badgeNumber).then(() => { 93 hilog.info(DOMAIN_NUMBER, TAG, `setBadgeNumber 11 success.`); 94 }); 95 ``` 96 97- Positive example 98 99 Dependencies existing between multiple API callings ensures that the next setting can be performed only after the previous setting is complete. 100 101 The sample code is as follows: 102 103 ```ts 104 let badgeNumber: number = 10; 105 notificationManager.setBadgeNumber(badgeNumber).then(() => { 106 hilog.info(DOMAIN_NUMBER, TAG, `setBadgeNumber 10 success.`); 107 badgeNumber = 11; 108 notificationManager.setBadgeNumber(badgeNumber).then(() => { 109 hilog.info(DOMAIN_NUMBER, TAG, `setBadgeNumber 11 success.`); 110 }); 111 }); 112 ``` 113