管理通知渠道

系统支持多种通知渠道,不同通知渠道对应的通知提醒方式不同,可以根据应用的实际场景选择适合的通知渠道,并对通知渠道进行管理(支持创建、查询、删除等操作)。

通知渠道类型说明

不同类型的通知渠道对应的通知提醒方式不同,详见下表。其中,Y代表支持,N代表不支持。

SlotType 取值 分类 通知中心 横幅 锁屏 铃声/振动 状态栏图标 自动亮屏
SOCIAL_COMMUNICATION 1 社交通信 Y Y Y Y Y Y
SERVICE_INFORMATION 2 服务提醒 Y N Y Y Y Y
CONTENT_INFORMATION 3 内容资讯 Y N N N Y N
CUSTOMER_SERVICE 5 客服消息 Y N N Y Y N
OTHER_TYPES 0xFFFF 其他 Y N N N N N

接口说明

通知渠道主要接口如下。其他接口介绍详情参见API参考

接口名 描述
addSlot(type: SlotType): Promise<void> 创建指定类型的通知渠道。
getSlot(slotType: SlotType): Promise<NotificationSlot> 获取一个指定类型的通知渠道。
removeSlot(slotType: SlotType): Promise<void> 删除此应用程序指定类型的通知渠道。

除了可以使用addslot()创建通知渠道,还可以在发布通知的NotificationRequest中携带notificationSlotType字段,如果对应渠道不存在,会自动创建。

开发步骤

  1. 导入notificationManager模块。

    import notificationManager from '@ohos.notificationManager';
    import Base from '@ohos.base';
    
  2. 创建指定类型的通知渠道。

    // addslot回调
    let addSlotCallBack = (err: Base.BusinessError): void => {
        if (err) {
            console.error(`addSlot failed, code is ${err.code}, message is ${err.message}`);
        } else {
            console.info("addSlot success");
        }
    }
    notificationManager.addSlot(notificationManager.SlotType.SOCIAL_COMMUNICATION, addSlotCallBack);
    
  3. 查询指定类型的通知渠道。

    获取对应渠道是否创建以及该渠道支持的通知提醒方式,比如是否有声音提示,是否有震动,锁屏是否可见等。

    // getSlot回调
    let getSlotCallback = (err: Base.BusinessError, data: notificationManager.NotificationSlot): void => {
        if (err) {
            console.error(`getSlot failed, code is ${err.code}, message is ${err.message}`);
        } else {
            console.info(`getSlot success. `);
            if (data != null) {
                console.info(`slot enable status is ${JSON.stringify(data.enabled)}`);
                console.info(`slot level is ${JSON.stringify(data.level)}`);
                console.info(`vibrationEnabled status is ${JSON.stringify(data.vibrationEnabled)}`);
                console.info(`lightEnabled status is ${JSON.stringify(data.lightEnabled)}`);
            }
        }
    }
    let slotType: notificationManager.SlotType = notificationManager.SlotType.SOCIAL_COMMUNICATION;
    notificationManager.getSlot(slotType, getSlotCallback);
    
  4. 删除指定类型的通知渠道。

    // removeSlot回调
    let removeSlotCallback = (err: Base.BusinessError): void => {
    if (err) {
        console.error(`removeSlot failed, code is ${err.code}, message is ${err.message}`);
    } else {
        console.info("removeSlot success");
    }
    }
    let slotType = notificationManager.SlotType.SOCIAL_COMMUNICATION;
    notificationManager.removeSlot(slotType, removeSlotCallback);