Create Project in ionic 3 and implement Admob
Admob
ionic3
- By Code solution
- Jan 20th, 2021
- 0 comments
- 0
implement Ads in ionic 3 using AdMob , ionic native plugin and npm packages
AdMob
Link: https://www.google.com/admob/
go to the left menu and click - > Apps
then click -> Add Apps
then create the App id the banner, Interstitial, Rewarded
Then Create a project run this command:
ionic start <project name> blank --type ionic1
then move into the newly created directory:
cd <project name>
Install required js and CSS file
$ ionic cordova plugin add cordova-plugin-admobpro
$ npm install --save @ionic-native/admob
Open app.js in src ->app-> app.module.ts and add code
import { BrowserModule } from '@angular/platform-browser'; import { ErrorHandler, NgModule } from '@angular/core'; import { IonicApp, IonicErrorHandler, IonicModule } from 'ionic-angular'; import { SplashScreen } from '@ionic-native/splash-screen'; import { StatusBar } from '@ionic-native/status-bar'; import { MyApp } from './app.component'; import { HomePage } from '../pages/home/home'; import { AdMob } from '@ionic-native/admob'; @NgModule({ declarations: [ MyApp, HomePage ], imports: [ BrowserModule, IonicModule.forRoot(MyApp) ], bootstrap: [IonicApp], entryComponents: [ MyApp, HomePage ], providers: [ StatusBar, SplashScreen, {provide: ErrorHandler, useClass: IonicErrorHandler}, AdMob ] }) export class AppModule {}
Open app.js in src ->app->app.component.ts and add code
import { Component } from '@angular/core'; import { Platform } from 'ionic-angular'; import { StatusBar } from '@ionic-native/status-bar'; import { SplashScreen } from '@ionic-native/splash-screen'; import { HomePage } from '../pages/home/home'; import { AdMob } from '@ionic-native/admob'; @Component({ templateUrl: 'app.html' }) export class MyApp { rootPage:any = HomePage; options : any; constructor(platform: Platform, statusBar: StatusBar, splashScreen: SplashScreen, private admob: AdMob) { platform.ready().then(() => { var admobid: { banner: string, interstitial: string, Rewarded: string }; if (/(android)/i.test(navigator.userAgent)) { admobid = { // for Android banner: 'ca-app-pub-xxxxxxxxxxxxxxxxx/xxxxxxxxxxxx', interstitial: 'ca-app-pub-xxxxxxxxxxxxxxxxxx/xxxxxxxxxxxx', Rewarded: 'ca-app-pub-xxxxxxxxxxxxxxx/xxxxxxxxxx' }; } else if (/(ipod|iphone|ipad)/i.test(navigator.userAgent)){ admobid = { // for iOS banner: 'ca-app-pub-xxxxxxxxxxxxxxx/xxxxxxxxxx', interstitial: 'ca-app-pub-xxxxxxxxxxxxxxxx/xxxxxxxxxxx', Rewarded: 'ca-app-pub-xxxxxxxxxxxxxxxx/xxxxxxxxxxx' }; } // Banner Ads this.admob.createBanner({ adId: admobid.banner, isTesting: false,//comment this out before publishing the app autoShow: true }) // Full Screen Ads this.admob.prepareInterstitial({ adId: admobid.interstitial, isTesting: false, //comment this out before publishing the app autoShow: true }); // Video Ads this.admob.prepareRewardVideoAd({ adId: admobid.Rewarded, isTesting: false, //comment this out before publishing the app autoShow: true . }); statusBar.styleDefault(); splashScreen.hide();}); } }
Image loader like WhatsApp application code is done you add platform in your application code platform add command:
ionic cordova platform add android/ios/windows
Build and Run command
ionic cordova build android/ios/windows ionic cordova run android/ios/windows
Github link: https://github.com/Sudarshan101/ionicAdmob
thank you