AndriousSolutions / ads

Other
58 stars 11 forks source link

Full Screen Ad is not loaded with setFullScreenAd #16

Closed tmrbaset closed 4 years ago

tmrbaset commented 4 years ago

First of all, thanks for this great library, it's really helpful.

I am currently facing an issue. Usually the proper procedure for interstitial Ads, is to load the Ad, make it ready, and when you need to show it, check if it's loaded then show it.

I assume that the setFullScreenAd method should load the Ad, and showFullScreenAd displays it if loaded.

currently the behavior is not like that with me, setFullScreenAd does not load the Ad, and only when I use showFullScreenAd the Ad is loaded then displayed, causing the Ad to be delayed, and also unwanted behavior in the app.

I am not sure if this a problem in my coding (I am new to dart), or a bug in the library, so I appreciate the assistance.

below are the important parts of my code:


// my static class
class AppAds {
  static Ads _ads;
  static Function _onScreenCloseHandler = (){};

//
....
//

static void setFullScreenAd({
    String adUnitId,
    MobileAdTargetingInfo targetInfo,
    List<String> keywords,
    String contentUrl,
    bool childDirected,
    List<String> testDevices,
    bool testing,
    MobileAdListener listener,
    double anchorOffset,
    AnchorType anchorType,
}) => _ads?.setFullScreenAd(
      adUnitId: adUnitId,
      targetInfo: targetInfo,
      keywords: keywords,
      contentUrl: contentUrl,
      childDirected: childDirected,
      testDevices: testDevices,
      testing: testing,
      anchorOffset: anchorOffset,
      anchorType: anchorType,
      listener: listener,
);

static void showFullScreen({
    String adUnitId,
    MobileAdTargetingInfo targetInfo,
    List<String> keywords,
    String contentUrl,
    bool childDirected,
    List<String> testDevices,
    bool testing,
    MobileAdListener listener,
    double anchorOffset,
    AnchorType anchorType,
    State state,
    Function onClose,
  }) {
  _ads?.showFullScreenAd(
    adUnitId: adUnitId,
      targetInfo: targetInfo,
      keywords: keywords,
      contentUrl: contentUrl,
      childDirected: childDirected,
      testDevices: testDevices,
      testing: testing,
      anchorOffset: anchorOffset,
      anchorType: anchorType,
      state: state,);

      _ads.screen.removeClosed(_onScreenCloseHandler);      
      _onScreenCloseHandler = onClose;
      _ads.screen.closedListener = (){
        _onScreenCloseHandler;
      };      
}
//
.....
//

static void init({bool testing: false, bool childDirected: false }) {
    _ads ??= Ads(
        _appId,
        bannerUnitId: _bannerUnitId,
        screenUnitId: _interstitialId,
        childDirected: childDirected,
        testDevices: _testDevices,
        testing: testing,
        listener: _eventListener,
      );

  }
}

//main.dart
  MobileAdListener _interstitialEventListener = (MobileAdEvent event) {
           print("=============== InterstitialAd event is $event");
          switch (event) {
            case MobileAdEvent.failedToLoad:
              Global.adMobInterstitialLoaded = false;    
              break;

            case MobileAdEvent.loaded:
              Global.adMobInterstitialLoaded = true;
              break;

            case MobileAdEvent.opened:
              break;

            case MobileAdEvent.closed:
              Global.adMobInterstitialLoaded = false;
              AppAds.setFullScreenAd();
              break;

            default:
              break;
          }
        };

    AppAds.init(testing: true, childDirected: false);
    AppAds.setFullScreenAd(listener: _interstitialEventListener);
Andrious commented 4 years ago

Hi there. Let's see what I can do. Is this code of yours being executed in an initState() function or something? Would you provide the repository to your code for me to look at?

    AppAds.init(testing: true, childDirected: false);
    AppAds.setFullScreenAd(listener: _interstitialEventListener);

Let's start from the beginning. Have you gotten the example to work? https://github.com/AndriousSolutions/ads/blob/master/example/main.dart

tmrbaset commented 4 years ago

Thanks Andrious for the response.

Yes I have that code in initState() in main.dart.

I also tried your example, it has the same behavior. the Interstitial is only loaded in memory after I click "Show Interstitial" button, not when the app loaded as expected

here is a log from your example, this log is just after I click the button:

I/flutter (28106): ====================An ad has loaded into memory. I/flutter (28106): The Ad is now open. I/flutter (28106): ===========The ad has opened.

The Ads are displayed, no problem in this, however my main problem is that they should be loaded with the setFullScreenAd method, not when I want to show them,

Andrious commented 4 years ago

Hi there again. In your first posting, you said, "I assume that the setFullScreenAd method should load the Ad, and showFullScreenAd displays it if loaded."

A fair assumption, but no. It doesn't load until you issue the 'show' command. However, after some hours of making changes, I found that 'loading' the Ad into memory with the setFullScreenAd method does reduce 'the delay' somewhat. Note, loading such Ads is a constraint on resources with such delays a constant.

Also in your first posting, you mentioned, "...Ad to be delayed, and also unwanted behavior in the app." What would be this other unwanted behaviour?

Regardless, I'm making revisions to the library package this evening. You will have your Ads loaded with the setFullScreenAd method. The next version that will be released in a few days. You can then be the judge as to the delay. It may not be that relevant, but again, I thought it a fair assumption. One likely shared by other users, and you know what they say, 'The customer is always right.'

Cheers.

Greg

tmrbaset commented 4 years ago

Thanks a lot Andrious for the support :)

Actually this assumption is based on best practice and even in the examples provided by AdMob team themselves in the original iOS and Android packages.

the scenario is you usually show interstitial Ad between App screens or if a game, between levels, so you need them to show instantly when requested, this is why there is a load method, and a show method, you load it first, and show it when you want (if loading didn't fail), and once closed you should call the load function again to prepare for the next Ad.

it will be really appreciated if you consider this implementation as it will improve your perfect library.

cheers.

Andrious commented 4 years ago

I can see the need for such an instant response. And so this latest version (1.1.0), will call the load function again once you've closed it. To prepare for the next Ad.

Let's see how immediate the Google Ads teams allows us to show such Ads again and again.

Cheers.

Greg

tmrbaset commented 4 years ago

issue should be fixed in rel 1.1.0