Ionic 2 Admob Plugin cordova-plugin-admob-free

时间:2017-04-16 17:18:49

标签: android cordova typescript ionic2

我试图在Ionic 2中使用此插件,如there 所示,并在src / app / app.components.ts内添加;

import { TabsPage } from '../pages/tabs/tabs';

import { AdMobFree, AdMobFreeBannerConfig } from '@ionic-native/admob-free';


@Component({
  templateUrl: 'app.html'
})
export class MyApp {
  rootPage:any = TabsPage;

  constructor(private admobFree: AdMobFree,platform: Platform, statusBar: StatusBar, splashScreen: SplashScreen) {
    platform.ready().then(() => {
      // Okay, so the platform is ready and our plugins are available.
      // Here you can do any higher level native things you might need.
      statusBar.styleDefault();
      splashScreen.hide();


      const bannerConfig: AdMobFreeBannerConfig = {
         id: 'ca-app-pub-MYCODE',
         isTesting: true,
         autoShow: true
        };
      this.admobFree.banner.config(bannerConfig);
      this.admobFree.banner.show();  


    });
  }
}

就像那段代码一样。

我也导入模块,将提供者添加到src / app / app.module.ts;

import { AdMobFree } from '@ionic-native/admob-free';

@NgModule({
  declarations: [
    MyApp,
    AboutPage,
    ContactPage,
    HomePage,
    TabsPage
  ],
  imports: [
    BrowserModule,
    IonicModule.forRoot(MyApp)
  ],
  bootstrap: [IonicApp],
  entryComponents: [
    MyApp,
    AboutPage,
    ContactPage,
    HomePage,
    TabsPage
  ],
  providers: [
    StatusBar,
    SplashScreen,
    AdMobFree,
    {provide: ErrorHandler, useClass: IonicErrorHandler}
  ]
})
export class AppModule {}

当我尝试在模拟器应用程序中工作正常但广告未来。我做错了吗?

1 个答案:

答案 0 :(得分:0)

试试这个!

constructor(private admobFree: AdMobFree) {
this.showBannerAd(); //calling banner ads
} 

async showBannerAd(){

try{
  const bannerConfig: AdMobFreeBannerConfig = {
    id : 'ca-app-pub-xxxx',
    isTesting : true,
    autoShow : true
  }
  this.admobFree.banner.config(bannerConfig);
  const result = await this.admobFree.banner.prepare();
  console.log('ad works fine', result);

}

catch(e){
  console.error(e);
}
}