请求单广告位广告,需要创建一个AdLoader对象,通过AdLoader的loadAd方法请求广告,最后通过AdLoadListener,来监听广告的加载状态。
如果想要为用户更精准的推送广告,可以在请求参数AdRequestParams中添加oaid属性。
请求广告关键参数如下所示: 请求广告参数名 |
类型 |
必填 |
说明 |
---|---|---|---|
adType |
number |
是 |
请求广告类型,原生广告类型为3。 |
adId |
string |
是 |
广告位ID。
|
oaid |
string |
否 |
开放匿名设备标识符,用于精准推送广告。不填无法获取到个性化广告。 |
- import { advertising, identifier } from '@kit.AdsKit';
- import { router } from '@kit.ArkUI';
- import { common } from '@kit.AbilityKit';
- import { hilog } from '@kit.PerformanceAnalysisKit';
- import { BusinessError } from '@kit.BasicServicesKit';
- @Entry
- @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}`);
- }
- }
- import { advertising, identifier } from '@kit.AdsKit';
- import { router } from '@kit.ArkUI';
- import { common } from '@kit.AbilityKit';
- import { hilog } from '@kit.PerformanceAnalysisKit';
- import { BusinessError } from '@kit.BasicServicesKit';
- @Entry
- @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}`);
- }
- }