Interstitial广告在Flutter中未显示。

huangapple go评论73阅读模式
英文:

Interstitial ads not showing in Flutter

问题

我正在使用这个包: https://pub.dev/packages/firebase_admob

一切都设置正确,正常的横幅广告显示正常。我的问题仅出现在插页式广告上:

import 'package:firebase_admob/firebase_admob.dart';

BannerAd myBanner = BannerAd(
  adUnitId: BannerAd.testAdUnitId,
  size: AdSize.smartBanner,
  listener: (MobileAdEvent event) {
    print("BannerAd event is $event");
  },
);

InterstitialAd myInterstitial = InterstitialAd(
  adUnitId: 'ca-app-pub-MYID',
  listener: (MobileAdEvent event) {
    print("InterstitialAd event is $event");
  },
);

class Ads {
  static showBanner() {
    myBanner
      // 通常在广告显示之前会发生这种情况
      ..load()
      ..show(
        // 将横幅广告定位在屏幕底部60像素处
        anchorOffset: 0.0,
        // 将横幅广告定位在屏幕中心右侧10像素处
        horizontalCenterOffset: 0.0,
        // 横幅广告位置
        anchorType: AnchorType.bottom,
      );
  }

  static showInterstitial() {
    myInterstitial
      ..load()
      ..show(
        anchorType: AnchorType.bottom,
        anchorOffset: 0.0,
        horizontalCenterOffset: 0.0,
      );
  }
}

final Ads ads = Ads();

要显示它,我执行: Ads.showInterstitial(); 但它从未显示。

如果我尝试再次调用它,错误会导致应用程序崩溃。

我在我的应用程序中不使用Statefull小部件。

英文:

I'm using this package: https://pub.dev/packages/firebase_admob

Everything is setup correctly and the normal banner is showing. My problem comes only with the interstitial one:

import 'package:firebase_admob/firebase_admob.dart';

BannerAd myBanner = BannerAd(
  adUnitId: BannerAd.testAdUnitId,
  size: AdSize.smartBanner,
  listener: (MobileAdEvent event) {
    print("BannerAd event is $event");
  },
);

InterstitialAd myInterstitial = InterstitialAd(
  adUnitId: 'ca-app-pub-MYID',
  listener: (MobileAdEvent event) {
    print("InterstitialAd event is $event");
  },
);

class Ads {
  static showBanner() {
    myBanner
      // typically this happens well before the ad is shown
      ..load()
      ..show(
        // Positions the banner ad 60 pixels from the bottom of the screen
        anchorOffset: 0.0,
        // Positions the banner ad 10 pixels from the center of the screen to the right
        horizontalCenterOffset: 0.0,
        // Banner Position
        anchorType: AnchorType.bottom,
      );
  }

  static showInterstitial() {
    myInterstitial
      ..load()
      ..show(
        anchorType: AnchorType.bottom,
        anchorOffset: 0.0,
        horizontalCenterOffset: 0.0,
      );
  }
}

final Ads ads = Ads();

To show it I do: Ads.showInterstitial(); but it's never shown.

If I try to call it again the error breaks the app.

I do not use Statefull widgets on my app

答案1

得分: 1

我认为您需要将您设备的ID添加为testDevice,以便能够在那里看到广告。

添加一个targetingInfo属性,其中包含您设备的ID。

MobileAdTargetingInfo targetingInfo = MobileAdTargetingInfo(
testDevices: ["在此处添加您设备的ID"], // Android模拟器被视为测试设备
);

并且在创建您的Interstitial字段时,添加targetingInfo属性,如插件页面的Readme中所述。

InterstitialAd myInterstitial = InterstitialAd(
// 用AdMob仪表板中的广告单元ID替换testAdUnitId。
// https://developers.google.com/admob/android/test-ads
// https://developers.google.com/admob/ios/test-ads
adUnitId: InterstitialAd.testAdUnitId,
targetingInfo: targetingInfo, // <--- 在这里
listener: (MobileAdEvent event) {
print("InterstitialAd事件为$event");
},
);

英文:

i think you have to add the ID of your device, as testDevice to be able to see the ads there.

add a targetingInfo property, where you add the ID of your device.

MobileAdTargetingInfo targetingInfo = MobileAdTargetingInfo(
  testDevices: &lt;String&gt;[&quot;*add id of your device here*&quot;], // Android emulators are considered test devices
);

and when creating your Interstitial field, add the targetingInfo property, like mentioned in the Readme of the plugin page.

InterstitialAd myInterstitial = InterstitialAd(
  // Replace the testAdUnitId with an ad unit id from the AdMob dash.
  // https://developers.google.com/admob/android/test-ads
  // https://developers.google.com/admob/ios/test-ads
  adUnitId: InterstitialAd.testAdUnitId,
  targetingInfo: targetingInfo, // &lt;--- here
  listener: (MobileAdEvent event) {
    print(&quot;InterstitialAd event is $event&quot;);
  },
);

huangapple
  • 本文由 发表于 2020年1月6日 20:13:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/59611921.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定