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