如何仅在使用 ionic 完成关卡后才显示 AdMob 插页式广告

How to show an AdMob interstitial ad only after a complete level with ionic

我已经使用我的 Ionic 应用程序实现了 cordova-admob-pro 插件。

我希望插页式广告在完成特定关卡后显示,在应用加载时显示。

这是我的代码:

app.js

// AdMob

    if (window.plugins && window.plugins.AdMob && showAds) {
        DebugService.add('admob OK');
        if (window.plugins && window.plugins.AdMob) {
            var admob_banner_key = device.platform == "Android" ? "ca-app-pub-7...6" : "ca-app-pub-7...0";
            var admob_interstitial_key = device.platform == "Android" ? "ca-app-pub-7...5 " : "ca-app-pub-7...6";
            var admob = window.plugins.AdMob;
            admob.createBannerView(
                    {
                        'publisherId': admob_banner_key,
                        'adSize': admob.AD_SIZE.BANNER,
                        'bannerAtTop': false
                    },
                    function () {
                        admob.requestAd(
                            {'isTesting': true},
                            function () {
                                admob.showAd(true);
                            },
                            function () {
                                DebugService.add('failed to request ad');
                            }
                        );
                    },
                    function () {
                        DebugService.add('failed to create banner view');
                    }
            );
            admob.createInterstitialView(
                    {
                        'publisherId': admob_interstitial_key,
                        'autoShow': false
                    },
                    function() {
                        DebugService.add('interstitial successfully craeted');
                    },
                    function() {
                        DebugService.add('failed to add interstitial :-(');
                    }
            );
        }
    }

controller.js:

            if ($scope.levelId % 3 === 0) {
                // after every second level and every third level after that
                DebugService.add("Show interstitial ad :-)");
                if ($localStorage.noAdsPurchased !== true &&
                        window.plugins && window.plugins.AdMob) {
                    DebugService.add('Ready to show interstitial');
                    window.plugins.AdMob.requestInterstitialAd(
                        {'isTesting': true},
                        function() {
                            window.plugins.AdMob.showInterstitialAd(true, 
                                function(){
                                    DebugService.add('interstitial successfully shown');
                                }, 
                                function(){
                                    DebugService.add('interstitial show failed');
                                });
                            DebugService.add('interstitial successfully requested');
                        },
                        function() {
                            DebugService.add('failed to request interstitial');
                        }
                    );
                    DebugService.add('After showing interstitial');
                }
            }

插页式广告也在开始时显示,这是我不想要的。如果我删除代码或尝试将 app.js 中的代码仅用于控制器中的插页式广告,则插页式广告根本不会显示。我在想,因为他们还没有启动。

如何启动插页式广告而不让它们一开始就展示?

感谢您的投入。这就是我最终解决问题的方法。

似乎只要调用 createInterstitialView 方法就会显示插页式广告。这就是它从一开始就显示的原因。

为了解决这个问题,我将所有 AdMob 代码移到了它自己的服务中,并使用方法来初始化、创建和展示广告。这样 createInterstitialView 在广告显示之前不会被调用。 createIntersititalView 从控制器调用。

wwdWordRecognition.service('AdmobService', ['DebugService', function (DebugService) {

    var _bannerId;
    var _interstitialId;

    var _isAdmobMissing = function() {
        if(window.plugins === undefined || window.plugins.AdMob === undefined) {
            return true;
        }
        return false;
    };

    this.init = function(bannerId, interstitialId) {
        _bannerId = bannerId;
        _interstitialId = interstitialId;
    };

    this.createAdmobBanner = function () {
        if(_isAdmobMissing()) {
            return false;
        }
        var _admob = window.plugins.AdMob;
        _admob.createBannerView(
                {
                    'publisherId': _bannerId,
                    'adSize': _admob.AD_SIZE.BANNER,
                    'bannerAtTop': false
                },
                function () {
                    _admob.requestAd(
                        {'isTesting': true},
                        function () {
                            _admob.showAd(true);
                        },
                        function () { DebugService.add('failed to request ad'); }
                    );
                },
                function () { DebugService.add('failed to create banner view');}
        );
    };

    this.showInterstitial = function () {
        if(_isAdmobMissing()) {
            return false;
        }
        DebugService.add('Admob plugin found.');
        var _admob = window.plugins.AdMob;
        _admob.createInterstitialView(
            {
                'publisherId': _interstitialId
            },
            function () {
                DebugService.add('interstitial successfully craeted');
                _admob.requestInterstitialAd(
                    {'isTesting': true},
                    function() {
                        _admob.showInterstitialAd(true, 
                            function(){ DebugService.add('interstitial successfully shown'); }, 
                            function(){ DebugService.add('interstitial show failed'); });
                        DebugService.add('interstitial successfully requested');
                    },
                    function() { DebugService.add('failed to request interstitial'); }
                );
            },
            function () { DebugService.add('failed to add interstitial :-('); }
        );
    };
}]);

app.js 现在看起来像这样:

// AdMob

DebugService.add('Start ad setup...');
if (showAds) {
    if (window.plugins && window.plugins.AdMob) {
        var _admob_banner_key = device.platform == "Android" ? "ca-app-pub-7...6" : "ca-app-pub-7...0";
        var _admob_interstitial_key = device.platform == "Android" ? "ca-app-pub-7...5 " : "ca-app-pub-7...6";
        AdmobService.init(_admob_banner_key,_admob_interstitial_key);
        AdmobService.createAdmobBanner();
    } else {
        DebugService.add('Admob plugin not found');
    }
}

最后 controller.js:

if ($scope.listId % 3 === 0) {
    // after every third list
    DebugService.add("Show interstitial ad.");
    if ($localStorage.noAdsPurchased !== true &&
             window.plugins && window.plugins.AdMob) {
        AdmobService.showInterstitial();
    }
}