Search code examples
javascriptxmladmobphonegap

alert messages impacting whether or not admob ads pop up?


I'm having some confusing experiences with admob on my first app build ever. The android app was built with phonegap and I'm trying to display an interstitial ad twice during game play. It's a Jeopardy style game, so an ad pops up when they hit the daily double and when they reach final jeopardy. When I first got it to work, I had a bunch of alert messages running, and those caused the ads to be blacked out, but they would pop up at least. When I commented the alerts out, everything worked perfectly. I later made some changes to the app, and I may have accidentally changed some of the code I was using for the ads by mistake. Since that change, the ad is only popping up once at the daily double mark. I put the alerts back in to see where it was getting hung up, but when I put the alerts in, it works for both double jeopardy and the final round. Except the ads are blacked out as they were before. After more testing, the alert that is affecting everything is "alert("we've got one sitting, should be shown");" as part of showInterstitialAd(). When commented out the ad only runs during the daily double, but when it's active a blacked out ad will play during the daily double and final round.

var isPendingInterstitial = false;
var isAutoshowInterstitial = false;

function prepareInterstitialAd() {
//alert("preparing");
    if (!isPendingInterstitial) {
        //alert("requesting interstitial");
        admob.requestInterstitialAd({
        autoShowInterstitial: isAutoshowInterstitial
        });
    }
}

function onAdLoadedEvent(e) {
    if (e.adType === admob.AD_TYPE.INTERSTITIAL && !isAutoshowInterstitial) {
        isPendingInterstitial = true;
        //alert("ad load event");
    }
}

function onDeviceReady() {
    document.removeEventListener('deviceready', onDeviceReady, false);

    //alert("device is ready");

    admob.setOptions({
        //alert("setting options");
        publisherId:          "pub-XXXXXXXXXXXXXXXX",
        interstitialAdId:     "ca-app-pub-XXXXXXXXXXXXXXXXXXXXXXXXXXX",
    });   

    document.addEventListener(admob.events.onAdLoaded, onAdLoadedEvent);
    prepareInterstitialAd();
}

document.addEventListener("deviceready", onDeviceReady, false);

function showInterstitialAd() {
    //alert("trying to show");
    if (isPendingInterstitial) {
        admob.showInterstitialAd(function () {
            //alert("we've got one sitting, should be shown");
            isPendingInterstitial = false;
            isAutoshowInterstitial = false;
            prepareInterstitialAd();
        });
    } 
    else {
        //alert("not ready yet, bring it in when ready");
        isAutoshowInterstitial = true;
        admob.requestInterstitialAd({
            autoShowInterstitial: isAutoshowInterstitial
        });
    }
}

The issue isn't calling showInterstitialAd(). It is getting called twice, I can tell from the alerts. There also shouldn't be any code running that would disrupt the ad during the final round. At the point the ads should display, the program is waiting on user input for their wager. As a test, I tried calling showInterstitialAd with setInterval at one minute intervals. That works and it shows the ad multiple times. I'm completely stumped. Unfortunately I'm not sure how to get any console logs out of this. I test on my browser for those, but I can't with the admob code active.

Why would calling showInterstitialAd work multiple times with setInterval but not the other way? It is a known fact that the other way is calling showInterstitialAd both times.

Why would the existence of an alert cause an ad to pop up, or why would it's lack of existence cause the ad to fail?

Any help would be awesome, thanks! For good measure, here is the code to call showInterstitialAd.

function runQuestion() {
    showInterstitialAd();
    $('.clickBlock').hide();
    $('.doublePageBody').hide();
    $('.questionPage').show();
    $('.answerInputs').show();
    if (finalRound) {
        document.getElementById('category').innerHTML = titleCase(finalCategory);
    }
    if (total <= 500) {

        document.getElementById('showQuestion').innerHTML = "Place your wager between $5 and $500.";
    }
    else {
        document.getElementById('showQuestion').innerHTML = "Place your wager between $5 and " + total;
    }
    document.getElementById('timer').innerHTML = '';
}

EDIT My original post had a mistake in the code that only existed here and wasn't in my app code. I've removed that now.


Solution

  • The problem was the prepareInterstitialAd() in this portion of the code.

    function showInterstitialAd() {
    //alert("trying to show");
    if (isPendingInterstitial) {
        admob.showInterstitialAd(function () {
            //alert("we've got one sitting, should be shown");
            isPendingInterstitial = false;
            isAutoshowInterstitial = false;
            prepareInterstitialAd();
        });
    } 
    else {
        //alert("not ready yet, bring it in when ready");
        isAutoshowInterstitial = true;
        admob.requestInterstitialAd({
            autoShowInterstitial: isAutoshowInterstitial
        });
    }
    

    The showing of the current interstitial must have impacted the preparing of the next one since they were happening at the same time for the most part. Now I'm preparing the next interstitial after the user enters their wager for the double jeopardy round. I had used this link as the example for my code in the original post, which was the first google response for "admob phonegap". On top of the issue described above, they also misspell the prepareInterstitial function in the device ready portion, but I had caught that part right away. Just a heads up for anyone else, though.