Managing Notification Slots
The system supports a range of notification slots. Different notification slots are assigned different reminder modes. You can choose notification slots for your application and manage them as required, such as creating, querying, and deleting notification slots.
Notification Slots
The following table lists the notification slots and their reminder modes. Y indicates that the feature is supported, and N indicates that the feature is not supported.
| SlotType | Value | Category | Notification Panel | Banner | Lock Screen | Alert Tone/Vibration | Status Bar Icon | Automatic Screen-on |
|---|---|---|---|---|---|---|---|---|
| UNKNOWN_TYPE | 0 | Unknown | Y | N | N | N | N | N |
| SOCIAL_COMMUNICATION | 1 | Social communication | Y | Y | Y | Y | Y | Y |
| SERVICE_INFORMATION | 2 | Service notification | Y | Y | Y | Y | Y | Y |
| CONTENT_INFORMATION | 3 | Content and news | Y | N | N | N | N | N |
| CUSTOMER_SERVICE | 5 | Customer service | Y | N | N | Y | Y | N |
| OTHER_TYPES | 0xFFFF | Other | Y | N | N | N | N | N |
Available APIs
The main notification slot APIs are as follows. For details about other APIs, see @ohos.notificationManager (NotificationManager).
| API | Description |
|---|---|
| addSlot(type: SlotType): Promise<void> | Adds a notification slot. |
| getSlot(slotType: SlotType): Promise<NotificationSlot> | Obtains a notification slot. |
| removeSlot(slotType: SlotType): Promise<void> | Removes a notification slot for this application. |
In addition to using addSlot(), you can also create a notification slot by passing notificationSlotType in the NotificationRequest. If the specified notification slot does not exist, it is automatically created.
How to Develop
-
Import the notificationManager module.
import { notificationManager } from '@kit.NotificationKit'; import { BusinessError } from '@kit.BasicServicesKit'; import { hilog } from '@kit.PerformanceAnalysisKit'; const TAG: string = '[PublishOperation]'; const DOMAIN_NUMBER: number = 0xFF00; -
Add a notification slot.
// addSlot callback let addSlotCallBack = (err: BusinessError): void => { if (err) { hilog.error(DOMAIN_NUMBER, TAG, `addSlot failed, code is ${err.code}, message is ${err.message}`); } else { hilog.info(DOMAIN_NUMBER, TAG, `addSlot success`); } }; notificationManager.addSlot(notificationManager.SlotType.SOCIAL_COMMUNICATION, addSlotCallBack); -
Obtain a notification slot.
Retrieve the slot's creation status and supported notification modes—for example, whether there is an alert tone, vibration, and lock screen visibility.
// getSlot callback let getSlotCallback = (err: BusinessError, data: notificationManager.NotificationSlot): void => { if (err) { hilog.error(DOMAIN_NUMBER, TAG, `Failed to get slot. Code is ${err.code}, message is ${err.message}`); } else { hilog.info(DOMAIN_NUMBER, TAG, `Succeeded in getting slot.`); if (data != null) { hilog.info(DOMAIN_NUMBER, TAG, `slot enable status is ${JSON.stringify(data.enabled)}`); hilog.info(DOMAIN_NUMBER, TAG, `vibrationEnabled status is ${JSON.stringify(data.vibrationEnabled)}`); hilog.info(DOMAIN_NUMBER, TAG, `lightEnabled status is ${JSON.stringify(data.lightEnabled)}`); } } }; let slotType: notificationManager.SlotType = notificationManager.SlotType.SOCIAL_COMMUNICATION; notificationManager.getSlot(slotType, getSlotCallback); -
Remove a notification slot.
// removeSlot callback let removeSlotCallback = (err: BusinessError): void => { if (err) { hilog.error(DOMAIN_NUMBER, TAG, `removeSlot failed, code is ${JSON.stringify(err.code)}, message is ${JSON.stringify(err.message)}`); } else { hilog.info(DOMAIN_NUMBER, TAG, 'removeSlot success'); } }; let slotType: notificationManager.SlotType = notificationManager.SlotType.SOCIAL_COMMUNICATION; notificationManager.removeSlot(slotType, removeSlotCallback);