首页 > 解决方案 > Flutter,Facebook Audience Network 广告横幅在进入另一个屏幕时不显示

问题描述

我用这个插件

facebook_audience_network 0.5.0

我通过这种方式实现了这个

class FacebookAd{

static void faceInit() {
 FacebookAudienceNetwork.init(
   testingId: "xxxxxxx-xxxxx-xxxx-xxxx-xxxxxxxxxx",
 );
}

static widget myBanner;

static void showBannerAd() {
  myBanner = FacebookBannerAd(
    placementId: "xxxxxxxxx_xxxxxxxxxxx",
    keepAlive: true,
    bannerSize: BannerSize.STANDARD,
    listener: (result, value) {
      print("Banner Ad: $result -->  $value eee");
      switch (result) {
        case BannerAdResult.ERROR:
          print("Error: $value");
          break;
        case BannerAdResult.LOADED:
          print("Loaded: $value");
          break;
        case BannerAdResult.CLICKED:
          print("Clicked: $value");
          break;
        case BannerAdResult.LOGGING_IMPRESSION:
          print("Logging Impression: $value");
          break;
      }
    },
  );
 }
}

}

和其他班级

 return Scaffold(
   bottomNavigationBar: FacebookAd.myBanner
 )

所以在应用程序中,所有屏幕都引用变量“myBanner”以在“bottomNavigationBar”内显示横幅

但问题是使用 pushNamed("/xxx") 更改屏幕时

有时屏幕显示横幅,有时不显示!(大部分不是)

我该如何解决这个问题?

谢谢你

标签: flutterflutter-layoutflutter-dependenciesfacebook-audience-network

解决方案


以下是我解决不同页面的 FAN 插页式广告的方法,与 AdMob 不同,您应该在单独的 dart 类中集中管理 Facebook FAN 广告。

class MyFacebookAdsManager {
  static bool isInterstitialAdLoaded = false;
  static String adsInterstitialUnitId = '';

  static void init(String id) {
    logger.i('$prefix init called');
    adsInterstitialUnitId = id;
    FacebookAudienceNetwork.init(
      testingId: '<test-id-if-needed>',
    );
    loadNewFANInterstitialAd();
  }

 static void loadNewFANInterstitialAd() {
    FacebookInterstitialAd.loadInterstitialAd(
      placementId: adsInterstitialUnitId,
      listener: (result, value) {
        logger.i("$prefix >> FAN > Interstitial Ad: $result --> $value");
        switch (result) {
          case InterstitialAdResult.LOADED:
            isInterstitialAdLoaded = true;
            break;

          case InterstitialAdResult.DISMISSED:
            isInterstitialAdLoaded = false;
            loadNewFANInterstitialAd();
            break;
          case InterstitialAdResult.ERROR:
            isInterstitialAdLoaded = false;
            logger.e('error loading facebook ads');
            loadNewFANInterstitialAd();
            break;
          default:
        }
      },
    );
  }

  static void showInterstitialAd() {
    if (isInterstitialAdLoaded)
      FacebookInterstitialAd.showInterstitialAd();
    else
      loadNewFANInterstitialAd();
  }
}

现在在主屏幕中,您将调用一次且仅一次的 init。

MyFacebookAdsManager.init('test-id')

最后,无论您想在哪里展示广告,都可以使用创建的类并展示广告。

MyFacebookAdsManager.showInterstitialAd();

以同样的方式,您应该创建单独的课程并管理所有横幅和奖励视频。


推荐阅读