harmonyOS SDK 原生广告的问题
各位大佬 我想请教下如何在点击按钮后触发在当前页面显示原生广告 这边只有跳转显示
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}`);
}
}