1# 管理通知渠道 2系统支持多种通知渠道,不同通知渠道对应的通知提醒方式不同,可以根据应用的实际场景选择适合的通知渠道,并对通知渠道进行管理(支持创建、查询、删除等操作)。 3 4## 通知渠道类型说明 5 6不同类型的通知渠道对应的通知提醒方式不同,详见下表。其中,Y代表支持,N代表不支持。 7 8| SlotType | 取值 | 分类 | 通知中心 | 横幅 | 锁屏 | 铃声/振动 | 状态栏图标 | 自动亮屏 | 9| -------------------- | ------ | --------| ------- |------|------|----------|-----------|---------| 10| UNKNOWN_TYPE | 0 | 未知类型 | Y | N | N | N | N | N | 11| SOCIAL_COMMUNICATION | 1 | 社交通信 | Y | Y | Y | Y | Y | Y | 12| SERVICE_INFORMATION | 2 | 服务提醒 | Y | Y | Y | Y | Y | Y | 13| CONTENT_INFORMATION | 3 | 内容资讯 | Y | N | N | N | N | N | 14| CUSTOMER_SERVICE | 5 | 客服消息 | Y | N | N | Y | Y | N | 15| OTHER_TYPES | 0xFFFF | 其他 | Y | N | N | N | N | N | 16 17 18## 接口说明 19 20通知渠道主要接口如下。其他接口介绍详情参见[API参考](../reference/apis-notification-kit/js-apis-notificationManager.md)。 21 22| **接口名** | **描述** | 23| ---------- | -------- | 24| addSlot(type: SlotType): Promise\<void\> | 创建指定类型的通知渠道。 | 25| getSlot(slotType: SlotType): Promise\<NotificationSlot\> | 获取一个指定类型的通知渠道。 | 26| removeSlot(slotType: SlotType): Promise\<void\> | 删除此应用程序指定类型的通知渠道。 | 27 28除了可以使用`addslot()`创建通知渠道,还可以在发布通知的[NotificationRequest](../reference/apis-notification-kit/js-apis-inner-notification-notificationRequest.md#notificationrequest)中携带notificationSlotType字段,如果对应渠道不存在,会自动创建。 29 30## 开发步骤 31 321. 导入notificationManager模块。 33 34 ```ts 35 import { notificationManager } from '@kit.NotificationKit'; 36 import { BusinessError } from '@kit.BasicServicesKit'; 37 import { hilog } from '@kit.PerformanceAnalysisKit'; 38 39 const TAG: string = '[PublishOperation]'; 40 const DOMAIN_NUMBER: number = 0xFF00; 41 ``` 42 432. 创建指定类型的通知渠道。 44 45 ```ts 46 // addslot回调 47 let addSlotCallBack = (err: BusinessError): void => { 48 if (err) { 49 hilog.info(DOMAIN_NUMBER, TAG, `addSlot failed, code is ${err.code}, message is ${err.message}`); 50 } else { 51 hilog.info(DOMAIN_NUMBER, TAG, `addSlot success`); 52 } 53 } 54 notificationManager.addSlot(notificationManager.SlotType.SOCIAL_COMMUNICATION, addSlotCallBack); 55 ``` 56 573. 查询指定类型的通知渠道。 58 59 获取对应渠道是否创建以及该渠道支持的通知提醒方式,比如是否有声音提示,是否有震动,锁屏是否可见等。 60 ```ts 61 // getSlot回调 62 let getSlotCallback = (err: BusinessError, data: notificationManager.NotificationSlot): void => { 63 if (err) { 64 hilog.error(DOMAIN_NUMBER, TAG, `getSlot failed, code is ${JSON.stringify(err.code)}, message is ${JSON.stringify(err.message)}`); 65 } else { 66 hilog.info(DOMAIN_NUMBER, TAG, `getSlot success. `); 67 if (data != null) { 68 hilog.info(DOMAIN_NUMBER, TAG, `slot enable status is ${JSON.stringify(data.enabled)}`); 69 hilog.info(DOMAIN_NUMBER, TAG, `slot level is ${JSON.stringify(data.level)}`); 70 hilog.info(DOMAIN_NUMBER, TAG, `vibrationEnabled status is ${JSON.stringify(data.vibrationEnabled)}`); 71 hilog.info(DOMAIN_NUMBER, TAG, `lightEnabled status is ${JSON.stringify(data.lightEnabled)}`); 72 } 73 } 74 } 75 let slotType: notificationManager.SlotType = notificationManager.SlotType.SOCIAL_COMMUNICATION; 76 notificationManager.getSlot(slotType, getSlotCallback); 77 ``` 78 794. 删除指定类型的通知渠道。 80 81 ```ts 82 // removeSlot回调 83 let removeSlotCallback = (err: BusinessError): void => { 84 if (err) { 85 hilog.error(DOMAIN_NUMBER, TAG, `removeSlot failed, code is ${JSON.stringify(err.code)}, message is ${JSON.stringify(err.message)}`); 86 } else { 87 hilog.info(DOMAIN_NUMBER, TAG, "removeSlot success"); 88 } 89 } 90 let slotType: notificationManager.SlotType = notificationManager.SlotType.SOCIAL_COMMUNICATION; 91 notificationManager.removeSlot(slotType, removeSlotCallback); 92 ``` 93