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在进行测试时控制台输出