跳转到内容

开始使用

  1. 安装插件

    Terminal window
    npm i @capgo/capacitor-admob
  2. 同步原生项目

    Terminal window
    npx cap sync
import { AdMob, MaxAdContentRating } from '@capgo/capacitor-admob';
// 在应用程序引导中启动 Mobile Ads SDK 一次
await AdMob.start();
// 可选:配置全局请求设置
await AdMob.configure({
appMuted: false,
appVolume: 1,
});
await AdMob.configRequest({
maxAdContentRating: MaxAdContentRating.T,
tagForChildDirectedTreatment: false,
tagForUnderAgeOfConsent: false,
});
import { BannerAd } from '@capgo/capacitor-admob';
const banner = new BannerAd({
adUnitId: 'ca-app-pub-xxxxxxxxxxxxxxxx/banner',
position: 'bottom',
});
await banner.show();
import { InterstitialAd, RewardedAd } from '@capgo/capacitor-admob';
const interstitial = new InterstitialAd({
adUnitId: 'ca-app-pub-xxxxxxxxxxxxxxxx/interstitial',
});
await interstitial.load();
await interstitial.show();
const rewarded = new RewardedAd({
adUnitId: 'ca-app-pub-xxxxxxxxxxxxxxxx/rewarded',
});
await rewarded.load();
await rewarded.show();
import { AdMob } from '@capgo/capacitor-admob';
const handle = await AdMob.addListener('adImpression', (event) => {
console.log('广告展示', event);
});
// 稍后清理时
await handle.remove();
  • iOS:在 Info.plist 中的 GADApplicationIdentifier 键下添加您的 AdMob 应用 ID,并包含您依赖的任何 SKAdNetwork ID。
  • Android:通过在 <application> 标签内添加 com.google.android.gms.ads.APPLICATION_IDAndroidManifest.xml 中声明您的 AdMob 应用 ID。
  • 同意和隐私:在 iOS 14+ 上使用 requestTrackingAuthorization(),并使用 configRequest() 儿童导向标志以符合区域隐私规则。