EddyVerbruggen / nativescript-plugin-firebase

:fire: NativeScript plugin for Firebase
https://firebase.google.com
MIT License
1.01k stars 448 forks source link

Issue with admob showInterstitial #1034

Open cryptodude2000 opened 5 years ago

cryptodude2000 commented 5 years ago

I have been trying to understand how the preloadInterstitial and showInterstitial work.

Based on the documentation - preload should be called first, then show. What we where doing is passing the ad arguments to both the preload and show methods.

Turns out based on the code in the plugin, the showIntersitital has a check to see if an ad is pre-loaded IF YOU PASS AD ARGUMENTS to it, if not it will preload an ad for you and then show it automatically. What this does is cause the onAdClosed callback never to fire for us for some reason.

To resolve this we now do preloadInterstitial with ad arguments. Then we showInterstitial without any arguments and everything works perfectly.

While I appreciate the fallback to automatically load and show an in showInterstitial (this might be for backward compatibility) this should not be the case. Maybe you should create a function called preLoadandShowInterstial for that, to be more accurate in the function call. Another option is update your readme.md with the documentation to reflect current operation.

Posted this to help anyone else experiencing problems with the onAdClosed call back issue.

One more comment, it might be a good idea to add a "clearAdInPreload" method or something of the sort to be able to flush any preloaded ad if needed.

Plugin works great otherwise.

This is the method I am referring to:

exports.preloadInterstitial = preloadInterstitial; function showInterstitial(arg) { return new Promise(function (resolve, reject) { try { // if no arg is passed, the interstitial has probably been preloaded if (!arg) { if (firebase_common_1.firebase.admob.interstitialView) { firebase_common_1.firebase.admob.interstitialView.show(); resolve(); } else { reject("Please call 'preloadInterstitial' first"); } return; }

        var settings = firebase_common_1.firebase.merge(arg, admob_common_1.BANNER_DEFAULTS);
        var activity = appModule.android.foregroundActivity || appModule.android.startActivity;
        firebase_common_1.firebase.admob.interstitialView = new com.google.android.gms.ads.InterstitialAd(activity);
        firebase_common_1.firebase.admob.interstitialView.setAdUnitId(settings.androidInterstitialId);
        // Interstitial ads must be loaded before they can be shown, so adding a listener
        var InterstitialAdListener = com.google.android.gms.ads.AdListener.extend({
            onAdLoaded: function () {
                if (firebase_common_1.firebase.admob.interstitialView) {
                    firebase_common_1.firebase.admob.interstitialView.show();
                }
                resolve();
            },
            onAdFailedToLoad: function (errorCode) {
                reject(errorCode);
            },
            onAdClosed: function () {
                if (firebase_common_1.firebase.admob.interstitialView) {
                    firebase_common_1.firebase.admob.interstitialView.setAdListener(null);
                    firebase_common_1.firebase.admob.interstitialView = null;
                }
                arg.onAdClosed && arg.onAdClosed();
            }
        });
        firebase_common_1.firebase.admob.interstitialView.setAdListener(new InterstitialAdListener());
        var ad = _buildAdRequest(settings);
        firebase_common_1.firebase.admob.interstitialView.loadAd(ad);
    }
    catch (ex) {
        console.log("Error in firebase.admob.showInterstitial: " + ex);
        reject(ex);
    }
});

}

marcelomiranda90 commented 5 years ago

Would it be correct to call the preloadInsterstial in onAdClosed to have an Interstitial always available?

Would a 30 minute time between preloadInsterstial and showInterstitial be a problem?