Skip to main content

Shiply Harmony 远程配置 SDK 集成

一、Harmony SDK 简介

Shiply Harmony 版本 SDK,支持在鸿蒙系统上进行远程配置下发、远程资源下发。

  • 支持丰富的下发条件(允许自定义条件)

  • 支持灰度发布、全量发布、定时发布

  • 支持测试、验证、发布审批等流程

  • 差量省流技术(iOS/Android 支持,鸿蒙版本后续支持)

二、使用配置下发 SDK

请先参考:集成 ShiplyPro Harmony SDK 完成 SDK 集成

初始化 SDK

import { RDelivery, RDeliveryConfig, RDeliveryData } from '@ohos/shiply'

let config: RDeliveryConfig = new RDeliveryConfig();
config.logicEnvironment = "";
// 请替换成你在 Shiply 平台注册的产品 app id 和 app key
config.appId = "xxx";
config.appKey = "xxxxxx-xxxxxx-xxxxxx-xxxxxx-xxxxxx";

// 用户账号
config.userId = "your_app_user_id";
config.deviceId = "your_app_device_id";
config.language = "en";
config.appVersion = "1.0.0";
config.osVersion = "16.2";
config.bundleId = "com.tencent.rdelivery.cpp";

let logOutput = (level: number, log: string) => {
hilog.info(0x0000, 'ShiplyLog', '%{public}s', log);
};
RDelivery.ExpectOnLocalDataInitComplete((error_code: number) => {
hilog.info(0x0000, 'ShiplyLog', 'ExpectOnLocalDataInitComplete error_code: %{public}s', error_code);
});
let context = getContext(this) as common.UIAbilityContext;
let filesDir = context.filesDir;
RDelivery.SdkStart(filesDir, config, logOutput);

拉取全量配置

/// 请求远端全量配置
/// - Parameters:
///   - custom_properties: 自定义属性
///   - callback: 回调
RDelivery.RequestFullRemoteData(custom_properties, (error_code: number, configList: Array<RDeliveryData>) => {

})

拉取单个场景下配置

/// 按场景ID请求远端全量配置
/// - Parameters:
///   - scene_id: 场景ID(Shiply网页端创建获取)
///   - custom_properties: 自定义属性
///   - callback: 回调
RDelivery.RequestBatchRemoteDataByScene(scene_id, custom_properties, (error_code: number, configList: Array<RDeliveryData>) => {

})

拉取多个场景下配置

/// 按多个场景ID请求远端全量配置
/// - Parameters:
///   - scene_ids: 场景ID列表(Shiply网页端创建获取)
///   - custom_properties: 自定义属性
///   - callback: 回调
let scene_ids = [100780, 100782, 100699]
RDelivery.RequestBatchRemoteDataByScenes(scene_ids, custom_properties, (error_code: number, configList: Array<RDeliveryData>) => {

})

拉取单个配置

/// 按配置Key请求远程配置信息
/// - Parameters:
///   - key: 配置Key
///   - custom_properties: 自定义属性
///   - callback: 回调
RDelivery.RequestSingleRemoteDataByKey(key, custom_properties, (error_code: number, configList: Array<RDeliveryData>) => {

})

拉取多个配置

/// 按多个配置Key请求远程配置信息
/// - Parameters:
///   - keys: 配置Key列表
///   - custom_properties: 自定义属性
///   - callback: 回调
let keys = ["test_active_user_report", "test_cpp_report"];
RDelivery.RequestRemoteDataByKeys(keys, custom_properties, (error_code: number) => {

})

同步读取单个配置

/// 同步读取单个配置
/// - Parameter key: 配置Key
const data: RDeliveryData = RDelivery.SyncGetRDeliveryDataByKey("mellow_test");

异步读取单个配置

/// 异步读取单个配置
/// - Parameters:
///   - key: 配置Key
///   - callback: 回调
let callback = (errorCode: number, data: RDeliveryData) => {

};
RDelivery.GetRDeliveryDataByKey("mellow_test", callback);

同步读取所有配置

/// 同步读取本地所有配置信息
const configMap: Map<string, RDeliveryData> = RDelivery.SyncGetRDeliveryAllDataMap();
configMap.forEach((item, key) => {
if (item.value) {
hilog.info(0x0000, 'ShiplyLog', 'sync get key: %{public}s value: %{public}s', key, item.value);
}
});

异步读取所有配置

/// 异步读取所有配置信息
/// - Parameter callback: 回调
let callback = (errorCode: number, configMap: Map<string, aki.RDeliveryData>) => {
 for (let [key, data] of Object.entries(configMap)) {
   hilog.info(0x0000, 'ShiplyLog', 'async get key: %{public}s values: %{public}s', key, data.value);
}
};
this.sdk.GetRDeliveryAllDataMap(callback);

同步读取所有开关

/// 同步读取本地所有开关信息
const configMap: Map<string, RDeliveryData> = RDelivery.SyncGetRDeliveryAllDataMap();
configMap.forEach((item, key) => {
if (item.value) {
hilog.info(0x0000, 'ShiplyLog', 'sync get key: %{public}s value: %{public}s', key, item.value);
}
});

异步读取所有开关

/// 异步读取所有配置信息
/// - Parameter callback: 回调
let callback = (errorCode: number, configMap: Map<string, RDeliveryData>) => {
configMap.forEach((item, key) => {
hilog.info(0x0000, 'ShiplyLog', 'async get key: %{public}s value: %{public}s', key, item.value);
});
};
RDelivery.GetRDeliveryAllDataMap(callback);

切换账号

注意

切换账号后,使用方需要主动调用拉取配置!!!

/// 切换用户
/// - Parameters:
///   - user_id: 用户Id
///   - callback: 回调
let userId = "another_user_2"
RDelivery.SwitchUserId(userId, (error_code: number) => {

})
let custom_properties: CustomProperties = {age: 100};
RDelivery.RequestFullRemoteData(custom_properties, (error_code: number, configList: Array<RDeliveryData>) => {

})

切换环境

注意

切换环境后,使用方需要主动调用拉取配置!!!

/// 切换环境
/// - Parameters:
///   - env: 环境ID("" 空字符串为正式环境,"1" 为默认测试环境,其他自定义环境请前往Shiply网页端获取ID)
///   - callback: 回调

let env = "1"
let isError: Boolean = RDelivery.SwitchEnvironment(env, (error_code: number) => {

})
let custom_properties: CustomProperties = {age: 100};
RDelivery.RequestFullRemoteData(custom_properties, (error_code: number) => {

})