「Flutter/admob/firebase admob」の版間の差分
提供: 初心者エンジニアの簡易メモ
(→サンプル) |
(→サンプル) |
||
| 行95: | 行95: | ||
myBanner | myBanner | ||
..load() | ..load() | ||
| − | ..show(); | + | ..show( |
| + | anchorOffset: 0.0, | ||
| + | anchorType: AnchorType.bottom, | ||
| + | ); | ||
</pre> | </pre> | ||
2019年12月16日 (月) 12:53時点における版
目次
インストール
pubspec.yaml
dependencies: firebase_admob: ^0.9.0+10
準備
android/app/src/main/AndroidManifest.xml
<meta-data android:name="com.google.android.gms.ads.APPLICATION_ID" android:value="ca-app-pub-xxxxxxx-appid"/>
ios/Runner/Info.Plist
<key>GADApplicationIdentifier</key> <string>ca-app-pub-xxxxxxx-appid</string> <key>io.flutter.embedded_views_preview</key> <true/>
appIdを入れる。
firebase設定を追加
andoridの準備
google-services.jsonをandorid/appの下へ
android/build.gradle
buildscript {
repositories {
google() // Google's Maven repository
}
dependencies {
classpath 'com.google.gms:google-services:3.2.1'
}
}
allprojects {
repositories {
google() // Google's Maven repository
}
}
android/app/build.gradle のファイルの末尾へapply~を追加
dependencies {
}
apply plugin: 'com.google.gms.google-services' // Google Play services Gradle plugin
iosの準備
xcode でios/Runner.xcworkspaceを開きRunner/RunnerへGoogleService-Info.plistファイルをドラッグする。
サンプル
初回起動時
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
if (Platform.isAndroid) {
FirebaseAdMob.instance.initialize(appId: 'ca-app-pub-067924yyyyyyyyyyy~xxxxxxxxx');
} else if (Platform.isIOS) {
FirebaseAdMob.instance.initialize(appId: 'ca-app-pub-067924yyyyyyyyyyy~xxxxxxxxx');
}
広告表示
void initState() {
MobileAdTargetingInfo targetingInfo = MobileAdTargetingInfo(
keywords: <String>['flutterio', 'beautiful apps'],
contentUrl: 'https://flutter.io',
birthday: DateTime.now(),
childDirected: false,
designedForFamilies: false,
gender: MobileAdGender.male, // or female, unknown
//testDevices: <String>["E36xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"],
);
String adUnitId = "";
if (Platform.isAndroid) {
adUnitId = "ca-app-pub-06792yyyyyyyyyyyyy/37xxxxxxx";
} else if (Platform.isIOS){
adUnitId = "ca-app-pub-06792yyyyyyyyyyyyy/62xxxxxxx";
}
BannerAd myBanner = BannerAd(
adUnitId: adUnitId,
size: AdSize.banner,
targetingInfo: targetingInfo,
listener: (MobileAdEvent event) {
print("BannerAd event is $event");
},
);
myBanner
..load()
..show(
anchorOffset: 0.0,
anchorType: AnchorType.bottom,
);
"BannerAd event is MobileAdEvent.failedToLoad"エラーが出たら
androidの場合は以下でテストデバイスidが上記testDeviceに設定されてるか確認する。
$ adb logcat | grep Test
