Tuesday 23 December 2014

How to create Interstitial Ad for AdMob in Ionic Cordova app?

You might have encountered an error like, when requesting an ad from AdMob using Cordova AdMob Plugin: interstitialAd is null, call createInterstitialView first

Chances are you are doing it wrong. To create an Interstitial Ad you need to take of three things:

  1. Use interstitialAdId as the key to pass you ad unit id in your options object
  2. First create a Interstitial view using createInterstitialView
  3. In success callback from createInterstitialView use requestInterstitialAd to request an ad

Below is the complete code snippet:

if (window.plugins && window.plugins.AdMob) {
    var admob_key = device.platform == "Android" ? "XXXXX" : "YYYYYYYYY";
    var admob = window.plugins.AdMob;
    var options = {
        interstitialAdId: admob_key,
        autoShow: true
    };
    admob.createInterstitialView(options, function() {
            admob.requestInterstitialAd({
                    'isTesting': false
                },
                function() {
                    admob.showAd(true);
                },
                function(error) {
                    console.log('failed to request ad ' + error);
                }
            );
        },
        function() {
            console.log('failed to create Interstitial view');
        });
} else {
    console.log("Admob plugin not available");
}

3 comments:

  1. Thank you so much you save my work of 1 week....thank you again

    ReplyDelete
  2. Thank you so much .. your the best

    ReplyDelete
  3. Thanks much! This fixed the issue and saved me from running around in circles trying to find the cause.

    ReplyDelete