返回介绍

Ionic4 Admob插件-Admob

发布于 2019-11-23 07:40:56 字数 5619 浏览 1055 评论 0 收藏 0

Most complete Admob plugin with support for Tappx ads. Monetize your apps and games with AdMob ads, using latest Google AdMob SDK. With this plugin you can show AdMob ads easily!

Supports:

  • Banner ads (top and bottom)
  • Interstitial ads
  • Rewarded ads
  • Tappx ads
https://github.com/appfeel/admob-google-cordova

Ionic Admob插件的安装(Installation)

ionic cordova plugin add cordova-admob npm install @ionic-native/admob 
Ionic EE comes with fully supported and maintained plugins from the Ionic Team. Learn More or Contact Us
ionic enterprise register --key=YOURPRODUCTKEY npm install @ionic-enterprise/admob 

Ionic Admob插件支持的平台(Supported Platforms)

  • Android
  • iOS

Ionic Admob插件的用法(Usage)

import { Admob, AdmobOptions } from '@ionic-native/admob/ngx';


constructor(private admob: Admob) {
    // Admob options config
    const admobOptions: AdmobOptions = {
      publisherId: 'XXX-XXXX-XXXX',
      interstitialAdId: 'XXX-XXXX-XXXX',
      rewardedAdId: 'XXX-XXXX-XXXX',
      isTesting: true,
      autoShowBanner: false,
      autoShowInterstitial: false,
      autoShowRewarded: false,
      adSize: this.admob.AD_SIZE.BANNER
    };

    // Set admob options
    this.admob.setOptions(admobOptions)
      .then(() => console.log('Admob options have been successfully set'))
      .catch(err => console.error('Error setting admob options:', err));
}



// (Optionally) Load banner ad, in order to have it ready to show
this.admob.createBannerView()
  .then(() => console.log('Banner ad loaded'))
  .catch(err => console.error('Error loading banner ad:', err));


// Show banner ad (createBannerView must be called before and onAdLoaded() event raised)
this.admob.onAdLoaded().subscribe((ad) => {
  if (ad.adType === this.admob.AD_TYPE.BANNER) {
    this.admob.showBannerAd()
      .then(() => console.log('Banner ad shown'))
      .catch(err => console.error('Error showing banner ad:', err));
  }
});


// Hide banner ad, but do not destroy it, so it can be shown later on
// See destroyBannerView in order to hide and destroy banner ad
this.admob.showBannerAd(false)
  .then(() => console.log('Banner ad hidden'))
  .catch(err => console.error('Error hiding banner ad:', err));



// Request an interstitial ad, in order to be shown later on
// It is possible to autoshow it via options parameter, see docs
this.admob.requestInterstitialAd()
  .then(() => console.log('Interstitial ad loaded'))
  .catch(err => console.error('Error loading interstitial ad:', err));


// Show an interstitial ad (requestInterstitialAd must be called before)
this.admob.onAdLoaded().subscribe((ad) => {
  if (ad.adType === this.admob.AD_TYPE.INTERSTITIAL) {
    this.admob.showInterstitialAd()
      .then(() => console.log('Interstitial ad shown'))
      .catch(err => console.error('Error showing interstitial ad:', err));
  }
});


// Request a rewarded ad
this.admob.requestRewardedAd()
  .then(() => console.log('Rewarded ad loaded'))
  .catch(err => console.error('Error loading rewarded ad:', err));


// Show rewarded ad (requestRewardedAd must be called before)
this.admob.onAdLoaded().subscribe((ad) => {
  if (ad.adType === this.admob.AD_TYPE.REWARDED) {
    this.admob.showRewardedAd()
      .then(() => console.log('Rewarded ad shown'))
      .catch(err => console.error('Error showing rewarded ad:', err));
  }
});


// Hide and destroy banner or interstitial ad
this.admob.destroyBannerView()
  .then(() => console.log('Banner or interstitial ad destroyed'))
  .catch(err => console.error('Error destroying banner or interstitial ad:', err));



// On Ad loaded event
this.admob.onAdLoaded().subscribe((ad) => {
  if (ad.adType === this.admob.AD_TYPE.BANNER) {
    console.log('Banner ad is loaded');
    this.admob.showBannerAd();
  } else if (ad.adType === this.admob.AD_TYPE.INTERSTITIAL) {
    console.log('Interstitial ad is loaded');
    this.admob.showInterstitialAd();
  } else if (ad.adType === this.admob.AD_TYPE.REWARDED) {
    console.log('Rewarded ad is loaded');
    this.admob.showRewardedAd();
  }
});



// On ad failed to load
this.admob.onAdFailedToLoad().subscribe(err => console.log('Error loading ad:', err));



// On interstitial ad opened
this.admob.onAdOpened().subscribe(() => console.log('Interstitial ad opened'));



// On interstitial ad closed
this.admob.onAdClosed().subscribe(() => console.log('Interstitial ad closed'));



// On ad clicked and left application
this.admob.onAdLeftApplication().subscribe(() => console.log('Ad lefted application'));



// On user ad rewarded
this.admob.onRewardedAd().subscribe(() => console.log('The user has been rewarded'));



// On rewarded ad video started
this.admob.onRewardedAdVideoStarted().subscribe(() => console.log('Rewarded ad vieo started'));



// On rewarded ad video completed
this.admob.onRewardedAdVideoCompleted().subscribe(() => console.log('Rewarded ad video completed'));

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文