Flutter接入FirebaseAdmob

1、创建Firebase账号,并创建项目,在新创建的项目里添加Android、iOS工程,在iOS项目里下载GoogleService-info.plist

2、创建Admob账号,并分别创建Android、iOS对应工程,并分别创建所需广告,如bannerAD横幅广告,并分别记录Android、iOS对应的项目ID及广告ID

3、Flutter工程添加Firebase_admob依赖

dependencies:
  firebase_admob: ^0.9.0+10

4、Android工程在Application节点下添加meta-data

<meta-data
    android:name="com.google.android.gms.ads.APPLICATION_ID"
    android:value="[ADMOB_APP_ID]"/>

 5、iOS工程Info.plist添加键值对

<key>GADApplicationIdentifier</key>
<string>[ADMOB_APP_ID]</string>

6、初始化Admob

FirebaseAdMob.instance.initialize(appId: appId);

7、添加广告,banner广告为例

MobileAdTargetingInfo targetingInfo = MobileAdTargetingInfo(
  keywords: <String>['flutterio', 'beautiful apps'],
  contentUrl: 'https://flutter.io',
  birthday: DateTime.now(),
  childDirected: false,
  designedForFamilies: false,
  gender: MobileAdGender.male, // or MobileAdGender.female, MobileAdGender.unknown
  testDevices: <String>[], // Android emulators are considered test devices
);
BannerAd myBanner = BannerAd(
  // 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: BannerAd.testAdUnitId,
  size: AdSize.smartBanner,
  targetingInfo: targetingInfo,
  listener: (MobileAdEvent event) {
    print("BannerAd event is $event");
  },
);
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: 60.0,
    // Positions the banner ad 10 pixels from the center of the screen to the right
    horizontalCenterOffset: 10.0,
    // Banner Position
    anchorType: AnchorType.bottom,
  );

 注意:

1、Admob推送广告 需先设置付款信息 完成设置或添加广告后可能需要等待几小时 才能正常看到广告 不想等待的话 可使用测试广告ID测试

2、真机上可能需要在testDevices添加真机ID,否则可能获取广告失败,真机ID在进行测试时控制台输出

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值