notification-badge.md 2.8 KB
Newer Older
E
ester.zhou 已提交
1
# Managing the Notification Badge
E
ester.zhou 已提交
2 3 4 5 6 7 8 9 10 11 12 13

OpenHarmony 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

1. The notification service provides two methods to increase the count on the notification badge:

E
ester.zhou 已提交
14
   - When publishing a notification, pass the **badgeNumber** parameter in [NotificationRequest](../reference/apis/js-apis-inner-notification-notificationRequest.md#notificationrequest). After the notification is received, the count on the badge is incremented.
E
ester.zhou 已提交
15

E
ester.zhou 已提交
16
   - Call the [setBadgeNumber()](../reference/apis/js-apis-notificationManager.md#notificationmanagersetbadgenumber10) API to set the count on the badge.
E
ester.zhou 已提交
17

E
ester.zhou 已提交
18
2. To decrease the count on the badge, call the [setBadgeNumber()](../reference/apis/js-apis-notificationManager.md#notificationmanagersetbadgenumber10) API.
E
ester.zhou 已提交
19 20 21 22 23 24 25 26 27 28 29 30

| API| Description|
| -------- | -------- |
| setBadgeNumber(badgeNumber: number, callback: AsyncCallback\<void\>): void | Sets the count on the badge.|


## How to Develop

1. Import the **NotificationManager** module.

   ```ts
   import notificationManager from '@ohos.notificationManager';
X
XKK 已提交
31
   import Base from '@ohos.base';
E
ester.zhou 已提交
32 33 34 35
   ```

2. Increase the count on the badge.

E
ester.zhou 已提交
36
   When publishing a notification, pass the **badgeNumber** parameter in [NotificationRequest](../reference/apis/js-apis-inner-notification-notificationRequest.md#notificationrequest). For details, see [Publishing a Basic Notification](text-notification.md).
E
ester.zhou 已提交
37 38 39 40
   
   In this example, the **setBadgeNumber** API is called to add a badge. This API is called after a new notification is published.
   
   ```ts
X
XKK 已提交
41
   function setBadgeNumberCallback(err:Base.BusinessError) {
E
ester.zhou 已提交
42 43 44 45 46
     if (err) {
       console.error(`Failed to set badge number. Code is ${err.code}, message is ${err.message}`);
       return;
     }
     console.info(`Succeeded in seting badge number.`);
E
ester.zhou 已提交
47 48
   }
   
E
ester.zhou 已提交
49
   let badgeNumber = 10;
E
ester.zhou 已提交
50 51 52
   notificationManager.setBadgeNumber(badgeNumber, setBadgeNumberCallback);
   ```

E
ester.zhou 已提交
53
3. Decrease the count on the badge.
E
ester.zhou 已提交
54 55 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.

   ```ts
X
XKK 已提交
58
   function setBadgeNumberCallback(err:Base.BusinessError) {
E
ester.zhou 已提交
59 60 61 62 63
     if (err) {
       console.error(`Failed to set badge number. Code is ${err.code}, message is ${err.message}`);
       return;
     }
     console.info(`Succeeded in seting badge number.`);
E
ester.zhou 已提交
64 65
   }
   
E
ester.zhou 已提交
66
   let badgeNumber = 9;
E
ester.zhou 已提交
67 68 69 70
   notificationManager.setBadgeNumber(badgeNumber, setBadgeNumberCallback);
   ```