HarmonyOS 鸿蒙Next打造开放、合规的广告生态 - 原生广告
HarmonyOS 鸿蒙Next打造开放、合规的广告生态 - 原生广告 场景介绍
原生广告是与应用内容融于一体的广告,通过“和谐”的内容呈现广告信息,在不破坏用户体验的前提下,为用户提供有价值的信息,展示形式包含图片和视频,支持您自由定制界面。
接口说明
接口名 | 描述 |
---|---|
loadAd(adParam: AdRequestParams, adOptions: AdOptions, listener: AdLoadListener): void | 请求单广告位广告,通过AdRequestParams、AdOptions进行广告请求参数设置,通过AdLoadListener监听广告请求回调。 |
loadAdWithMultiSlots(adParams: AdRequestParams[], adOptions: AdOptions, listener: MultiSlotsAdLoadListener): void | 请求多广告位广告,通过AdRequestParams[]、AdOptions进行广告请求参数设置,通过MultiSlotsAdLoadListener监听广告请求回调。 |
AdComponent(ads: advertising.Advertisement[], displayOptions: advertising.AdDisplayOptions, interactionListener: advertising.AdInteractionListener, @BuilderParam adRenderer?: () => void): void | 展示广告,通过AdDisplayOptions进行广告展示参数设置,通过AdInteractionListener监听广告状态回调。 |
开发步骤
-
获取OAID。
- 如果想要为用户更精准的推送广告,可以在请求参数AdRequestParams中添加oaid属性。
- 如何获取OAID参见获取OAID信息。
- 说明
- 使用以下示例中提供的测试广告位必须先获取OAID信息。
-
请求广告。
- 请求单广告位广告,需要创建一个AdLoader对象,通过AdLoader的loadAd方法请求广告,最后通过AdLoadListener,来监听广告的加载状态。
- 如果想要为用户更精准的推送广告,可以在请求参数AdRequestParams中添加oaid属性。
请求广告关键参数如下所示:
请求广告参数名 类型 必填 说明 adType number 是 请求广告类型,原生广告类型为3。 adId string 是 广告位ID。 oaid string 否 开放匿名设备标识符,用于精准推送广告。不填无法获取到个性化广告。 示例代码如下所示:
import { advertising, identifier } from '[@kit](/user/kit).AdsKit'; import { router } from '[@kit](/user/kit).ArkUI'; import { common } from '[@kit](/user/kit).AbilityKit'; import { hilog } from '[@kit](/user/kit).PerformanceAnalysisKit'; import { BusinessError } from '[@kit](/user/kit).BasicServicesKit'; [@Entry](/user/Entry) [@Component](/user/Component) export struct LoadAd { private context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext; private oaid: string = ''; aboutToAppear() { try { // 使用Promise回调方式获取OAID identifier.getOAID().then((data: string) => { this.oaid = data; hilog.info(0x0000, 'testTag', '%{public}s', 'Succeeded in getting adsIdentifierInfo by promise'); }).catch((error: BusinessError) => { hilog.error(0x0000, 'testTag', '%{public}s', `Failed to get adsIdentifierInfo, error code: ${error.code}, message: ${error.message}`); }) } catch (error) { hilog.error(0x0000, 'testTag', '%{public}s', `Catch err, code: ${error.code}, message: ${error.message}`); } } build() { Column() { Column() { // 跳转到原生广告展示页面 Button("请求原生广告", { type: ButtonType.Normal, stateEffect: true }).onClick(() => { this.requestAd(); }) }.width('100%').height('80%').justifyContent(FlexAlign.Center) } .width('100%') .height('100%') } private requestAd(): void { // 广告展示参数 const adDisplayOptions: advertising.AdDisplayOptions = { // 是否静音,默认不静音 mute: false } // 原生广告配置 const adOptions: advertising.AdOptions = { // 设置是否请求非个性化广告 nonPersonalizedAd: 1, // 是否允许流量下载0:不允许,1:允许,不设置以广告主设置为准 allowMobileTraffic: 0, // 是否希望根据 COPPA 的规定将您的内容视为面向儿童的内容: -1默认值,不确定 0不希望 1希望 tagForChildProtection: -1, // 是否希望按适合未达到法定承诺年龄的欧洲经济区 (EEA) 用户的方式处理该广告请求: -1默认值,不确定 0不希望 1希望 tagForUnderAgeOfPromise: -1, // 设置广告内容分级上限: W: 3+,所有受众 PI: 7+,家长指导 J:12+,青少年 A: 16+/18+,成人受众 adContentClassification: 'A' } // 原生广告请求参数 const nativeVideoAdReqParams: advertising.AdRequestParams = { // 'testu7m3hc4gvm'为测试专用的广告位ID,App正式发布时需要改为正式的广告位ID adId: 'testu7m3hc4gvm', adType: 3, adCount: 1, // 原生广告自定义扩展参数。等所有广告素材下载完后再回调 enableDirectReturnVideoAd: true, oaid: this.oaid } // 广告请求回调监听 const adLoaderListener: advertising.AdLoadListener = { // 广告请求失败回调 onAdLoadFailure: (errorCode: number, errorMsg: string) => { hilog.error(0x0000, 'testTag', '%{public}s', `Failed to request ad, message: ${errorMsg}, error code: ${errorCode}`); }, // 广告请求成功回调 onAdLoadSuccess: (ads: Array<advertising.Advertisement>) => { hilog.info(0x0000, 'testTag', '%{public}s', 'Succeeded in requesting ad'); // 调用原生广告展示页面 routePage('pages/NativeAdPage', ads, adDisplayOptions); } }; // 创建AdLoader广告对象 const load: advertising.AdLoader = new advertising.AdLoader(this.context); // 调用广告请求接口 load.loadAd(nativeVideoAdReqParams, adOptions, adLoaderListener); } } async function routePage(pageUri: string, ads: Array<advertising.Advertisement | null>, displayOptions: advertising.AdDisplayOptions) { let options: router.RouterOptions = { url: pageUri, params: { ads: ads, displayOptions: displayOptions } } try { router.pushUrl(options); } catch (error) { hilog.error(0x0000, 'testTag', '%{public}s', `Failed to routePage callback, code: ${error.code}, msg: ${error.message}`); } }
-
请求多广告与请求单广告类似,需要创建一个AdLoader对象,通过AdLoader的loadAdWithMultiSlots方法请求广告,最后通过MultiSlotsAdLoadListener,来监听广告的加载状态。示例代码如下:
import { advertising, identifier } from '[@kit](/user/kit).AdsKit'; import { router } from '[@kit](/user/kit).ArkUI'; import { common } from '[@kit](/user/kit).AbilityKit'; import { hilog } from '[@kit](/user/kit).PerformanceAnalysisKit'; import { BusinessError } from '[@kit](/user/kit).BasicServicesKit'; [@Entry](/user/Entry) [@Component](/user/Component) export struct LoadAd { private ads: Array<advertising.Advertisement> = []; private context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext; private oaid: string = ''; aboutToAppear() { try { // 使用Promise回调方式获取OAID identifier.getOAID().then((data: string) => { this.oaid = data; hilog.info(0x0000, 'testTag', '%{public}s', 'Succeeded in getting adsIdentifierInfo by promise'); }).catch((error: BusinessError) => { hilog.error(0x0000, 'testTag', '%{public}s', `Failed to get adsIdentifierInfo, code: ${error.code}, message: ${error.message}`); }) } catch (error) { hilog.error(0x0000, 'testTag', '%{public}s', `Catch err, code: ${error.code}, message: ${error.message}`); } } build() { Column() { Column() { // 跳转到原生广告展示页面 Button("请求原生广告", { type: ButtonType.Normal, stateEffect: true }).onClick(() => { this.requestAd(); }) }.width('100%').height('80%').justifyContent(FlexAlign.Center) } .width('100%') .height('100%') } private requestAd(): void { // 广告展示参数 const adDisplayOptions: advertising.AdDisplayOptions = { // 是否静音,默认不静音 mute: false } // 原生广告配置 const adOptions: advertising.AdOptions = { // 是否允许流量下载 0不允许 1允许,不设置以广告主设置为准 allowMobileTraffic: 0, // 是否希望根据 COPPA 的规定将您的内容视为面向儿童的内容: -1默认值,不确定 0不希望 1希望 tagForChildProtection: -1, // 是否希望按适合未达到法定承诺年龄的欧洲经济区 (EEA) 用户的方式处理该广告请求: -1默认值,不确定 0不希望 1希望 tagForUnderAgeOfPromise: -1, // 设置广告内容分级上限: W: 3+,所有受众 PI: 7+,家长指导 J:12+,青少年 A: 16+/18+,成人受众 adContentClassification: 'A' }; // 原生广告请求参数 const nativeVideoAdReqParams: advertising.AdRequestParams[] = [{ // 'testy63txaom86'为测试专用的广告位ID,App正式发布时需要改为正式的广告位ID adId: 'testy63txaom86', adType: 3, adCount: 1, // 原生广告自定义扩展参数。等所有广告素材下载完后再回调 enableDirectReturnVideoAd: true, oaid: this.oaid }, { // 'testu7m3hc4gvm'为测试专用的广告位ID,App正式发布时需要改为正式的广告位ID adId: 'testu7m3hc4gvm', adType: 3, adCount: 1, // 原生广告自定义扩展参数。等所有广告素材下载完后再回调 enableDirectReturnVideoAd: true, oaid: this.oaid }] // 广告请求回调监听 const adLoaderListener: advertising.MultiSlotsAdLoadListener = { // 广告请求失败回调 onAdLoadFailure: (errorCode: number, errorMsg: string) => { hilog.error(0x0000, 'testTag', '%{public}s', `Failed to request ad errorCode is: ${errorCode}, errorMsg is: ${errorMsg}`); }, // 广告请求成功回调 onAdLoadSuccess: (ads: Map<string, Array<advertising.Advertisement>>) => { hilog.info(0x0000, 'testTag', '%{public}s', 'Succeeded in requesting ad!'); ads.forEach((adsArray) => this.ads.push(...adsArray)); // 调用原生广告展示页面 routePage('pages/NativeAdPage', this.ads, adDisplayOptions); } }; // 创建AdLoader广告对象 const load: advertising.AdLoader = new advertising.AdLoader(this.context); // 调用广告请求接口 load.loadAdWithMultiSlots(nativeVideoAdReqParams, adOptions, adLoaderListener); } } async function routePage(pageUri: string, ads: Array<advertising.Advertisement | null>, displayOptions: advertising.AdDisplayOptions) { let options: router.RouterOptions = { url: pageUri, params: { ads: ads, displayOptions: displayOptions } } try { router.pushUrl(options); } catch (error) { hilog.error(0x0000, 'testTag', '%{public}s', `Failed to routePage callback, code: ${error.code}, msg: ${error.message}`); } }
更多关于HarmonyOS 鸿蒙Next打造开放、合规的广告生态 - 原生广告的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
更多关于HarmonyOS 鸿蒙Next打造开放、合规的广告生态 - 原生广告的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
针对帖子标题“HarmonyOS 鸿蒙Next打造开放、合规的广告生态 - 原生广告”的问题,以下回答专注于鸿蒙系统本身及其广告生态的方面:
鸿蒙系统(HarmonyOS)在Next版本中致力于构建一个开放且合规的广告生态。其中,原生广告作为这一生态的重要组成部分,旨在提供更为流畅、自然的用户体验。原生广告通常与应用程序的内容、设计和功能高度融合,使得用户在浏览或使用过程中几乎察觉不到广告的存在,从而减少了传统广告可能带来的干扰和反感。
在鸿蒙的广告生态中,原生广告不仅遵循严格的合规标准,确保广告内容的真实性和合法性,还通过智能化的投放策略,实现广告与用户需求的精准匹配。这有助于提升广告的转化效率,同时也为用户提供了更加个性化的服务体验。
鸿蒙系统通过其强大的技术实力和开放的平台策略,为广告主和开发者提供了丰富的广告形式和灵活的投放选项。这有助于推动广告行业的创新发展,促进鸿蒙生态的繁荣与壮大。
如果对于鸿蒙系统中原生广告的具体实现方式、技术细节或合规标准等方面有更多疑问,且上述回答未能满足需求,请联系官网客服。官网地址是:https://www.itying.com/category-93-b0.html,