EddyVerbruggen / nativescript-admob

NativeScript plugin to earn some precious :moneybag::moneybag: with ads by Google AdMob
MIT License
69 stars 26 forks source link

Suggestion: preload Interstitial #3

Closed calebeaires closed 6 years ago

calebeaires commented 8 years ago

Admob team contact me about policy from interstital implementation. They suggest to preload the ad first (ex: when app starts), then show on needed.

This is useful to "Rewarded mediation": https://support.google.com/admob/answer/3063564?hl=en

I have did some modification. It is not the final implementation, but It is a start.

admob.preloadInterstitial = function(arg) {
  return new Promise(function (resolve, reject) {
    try {
      var settings = admob.merge(arg, admob.defaults);
      var activity = application.android.foregroundActivity;
      admob.interstitialView = new com.google.android.gms.ads.InterstitialAd(activity);
      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 () {
          var a = admob.interstitialView.isLoaded();
          resolve(a);
        },
        onAdFailedToLoad: function (errorCode) {
          reject(errorCode);
        }
      });
      admob.interstitialView.setAdListener(new InterstitialAdListener());

      var ad = admob._buildAdRequest(settings);
      admob.interstitialView.loadAd(ad);

    } catch (ex) {
      console.log("Error in admob.createBanner: " + ex);
      reject(ex);
    }
  });
};
SteveOliverC commented 7 years ago

This should be more than a suggestion, because using the plugin this way is a violation of Admob's policies and as the OP says they will contact you. It is within Admob's rights to withhold your ad money and terminate your account. The issue is that they specifically say you cannot show an interstitial an indeterminate amount of time after the action that triggered it. It will almost never be the case that the interstitial is loaded immediately after it is requested. The way this plugin works at the moment, it will nearly always present the interstitial a few seconds after you call it, and that is exactly what Admob does not allow.

EddyVerbruggen commented 7 years ago

That's clear. Anyone wanting to contribute a solution?

skopekreep commented 6 years ago

In the README, it says: "Note that Interstitial banners need to be loaded before they can be shown, but don't worry: this plugin will manage that transparently for you", which seems in contradiction to this (open) issue. Can you please clarify if preloading has been added for both platforms and this issue should be closed, or if the above statement is incomplete or inaccurate? Thanks!

EddyVerbruggen commented 6 years ago

It’s accurate AND preloading hasn’t been added but it’s still up for grabs.

What the readme says is you don’t have to call 2 functions (load, then show when loaded) as it’s all integrated into 1 function.

sayeed360 commented 6 years ago

It’s accurate AND preloading hasn’t been added but it’s still up for grabs.

What the readme says is you don’t have to call 2 functions (load, then show when loaded) as it’s all integrated into 1 function.

Can you please provide an example. All I can see is when I use your example on the main readme, it shows the ad as soon as it is loaded. How do I preload the ad and show when needed?

SteveOliverC commented 6 years ago

You are using a test ad, so that displays very quickly. But a normal ad will take at least a few seconds to load, and maybe longer depending on the ad content and the device connection. The way to test this properly is to log to the console when the ad is successfully loaded (or not), and then trigger the ad to display based on some action by the user. And this cannot be done with the plugin as it currently is.

sayeed360 commented 6 years ago

So any other alternatives? maybe another plugin? How do you guys solve this problem in your own apps?

EddyVerbruggen commented 6 years ago

Exactly as @SteveOliverC states.

Btw, it's not too hard to add to this plugin, it's just a matter of priority/time.

SteveOliverC commented 6 years ago

It's been a while since I solved this so let me see if I can remember exactly .... I hacked each of the iOS and Android codes so that would return a status code rather than display the interstitial. That way my app knew the interstitial was loaded. Then I added a method to show the loaded interstitial. But I could not get this to work again after the first time it was loaded and displayed -- it would never return another status code when requesting another ad load. It was too hard for me to figure out :)

mimamo10 commented 6 years ago

Thanks for the plugin, really appreciate it. But as it stands we shouldn't use the plugin for interstitial ads without first hacking the plugin code?

"Note that Interstitial banners need to be loaded before they can be shown, but don't worry: this plugin will manage that transparently for you." gives an entirely different impression!

SteveOliverC says he couldn't figure it out, so fat chance for those of us with lesser skills.

mimamo10 commented 6 years ago

Okay, I see. We can't preload and show when needed, but it effectively preloads. Hacking only needed if we want to show the ad at a later point from when it's loaded.

EddyVerbruggen commented 6 years ago

Yep, that’s what that quote tries to say.

SteveOliverC commented 6 years ago

So then this issue is fixed? or was never an issue to begin with? Or I am not understanding what is going on here? Because when I use this plugin on a production account (IE, not using test interstitials), the delay between calling this method and the when the interstitial appears can be up to several seconds. So a user taps a button and I want the interstitial to appear at that action. But if the interstitial appears 3 seconds later, that is a clear violation of Admob terms. I have been flagged by Admob before and believe me, it ain't pretty. Firstly, they simply turn off your revenue. Then you notice you don't get any money and you contact them. Then if you are lucky they respond and explain why they turned off your money. Otherwise you are left trying to figure it out. Finally, if too many of your apps misbehave, they simply turn off your account (luckily I fixed my app before that happened).

mimamo10 commented 6 years ago

It is an issue as outlined on this google page. The delay we get means that the next action has started before the interstitial ad is shown. We definitely need to separate out the loading and the showing into two functions.

I've just tried using calebeaires solution to load and another function to show the ad. It works on the emulator but won't load on an actual device, onAdFailedToLoad returns an error code 0. I'm currently stumped.

EddyVerbruggen commented 6 years ago

Let me try to be clear:

EddyVerbruggen commented 6 years ago

FYI I'm spending my weekend fixing this. iOS is already done. Android tomorrow. There will be 2 new methods: preloadInterstitial (with the same parameters as the current createInterstitial), and showInterstitial which should not be called before the promise of preloadInterstitial resolves, but can be called a long time after it has been preloaded.

UPDATE: Android is done as well. And the demo has been updated (you can already take a look). Need to document it and release it tomorrow.

umair-ameen commented 6 years ago

Is it fixed in nativescript-plugin-firebase as well?

EddyVerbruggen commented 6 years ago

@umair-ameen Not by magic 💫😉. I'll spend some time there as well today.

EddyVerbruggen commented 6 years ago

2.0.0 released, see the updated docs.

EddyVerbruggen commented 6 years ago

@umair-ameen Check https://github.com/EddyVerbruggen/nativescript-plugin-firebase/issues/934 - now available from npm in nativescript-plugin-firebase version 7.1.1.

umair-ameen commented 6 years ago

@EddyVerbruggen Thank you so much Eddy. You are awesome.

SteveOliverC commented 6 years ago

@EddyVerbruggen NICE work!!!

mimamo10 commented 6 years ago

Brilliant, thanks!

LeviWadd commented 5 years ago

Thanks @EddyVerbruggen!