「Flutter/admob/firebase admob」の版間の差分
提供: 初心者エンジニアの簡易メモ
(→サンプル) |
|||
| (同じ利用者による、間の4版が非表示) | |||
| 行24: | 行24: | ||
==firebase設定を追加== | ==firebase設定を追加== | ||
| − | === | + | ===androidの準備=== |
| − | google-services. | + | google-services.jsonをandroid/appの下へ |
android/build.gradle | android/build.gradle | ||
| 行70: | 行70: | ||
<pre> | <pre> | ||
void initState() { | void initState() { | ||
| − | + | MobileAdTargetingInfo targetingInfo = MobileAdTargetingInfo( | |
keywords: <String>['flutterio', 'beautiful apps'], | keywords: <String>['flutterio', 'beautiful apps'], | ||
contentUrl: 'https://flutter.io', | contentUrl: 'https://flutter.io', | ||
| 行87: | 行87: | ||
BannerAd myBanner = BannerAd( | BannerAd myBanner = BannerAd( | ||
adUnitId: adUnitId, | adUnitId: adUnitId, | ||
| − | size: AdSize. | + | size: AdSize.smartBanner, |
targetingInfo: targetingInfo, | targetingInfo: targetingInfo, | ||
listener: (MobileAdEvent event) { | listener: (MobileAdEvent event) { | ||
| 行100: | 行100: | ||
); | ); | ||
</pre> | </pre> | ||
| + | chorOffset: 50.0ぐらいにすると1行ぐらい開く。 | ||
=="BannerAd event is MobileAdEvent.failedToLoad"エラーが出たら== | =="BannerAd event is MobileAdEvent.failedToLoad"エラーが出たら== | ||
androidの場合は以下でテストデバイスidが上記testDeviceに設定されてるか確認する。 | androidの場合は以下でテストデバイスidが上記testDeviceに設定されてるか確認する。 | ||
$ adb logcat | grep Test | $ adb logcat | grep Test | ||
2020年1月27日 (月) 11:19時点における最新版
目次
インストール
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設定を追加
androidの準備
google-services.jsonをandroid/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.smartBanner,
targetingInfo: targetingInfo,
listener: (MobileAdEvent event) {
print("BannerAd event is $event");
},
);
myBanner
..load()
..show(
anchorOffset: 0.0,
anchorType: AnchorType.bottom,
);
chorOffset: 50.0ぐらいにすると1行ぐらい開く。
"BannerAd event is MobileAdEvent.failedToLoad"エラーが出たら
androidの場合は以下でテストデバイスidが上記testDeviceに設定されてるか確認する。
$ adb logcat | grep Test
