2017-04-16 98 views
0

我试图在Ionic 2中使用这个插件,如there所示,并添加到src/app/app.components.ts中;Ionic 2 Admob插件cordova-plugin-admob-free

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 {} 

而当我尝试在模拟器应用程序工作正常,但广告不来。我做错了什么?

回答

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); 
} 
}