「Flutter/admob/firebase admob」の版間の差分
提供: 初心者エンジニアの簡易メモ
(→firebase設定を追加) |
|||
| (同じ利用者による、間の14版が非表示) | |||
| 行3: | 行3: | ||
<pre> | <pre> | ||
dependencies: | dependencies: | ||
| − | firebase_admob: 0.9.0 | + | firebase_admob: ^0.9.0+10 |
</pre> | </pre> | ||
==準備== | ==準備== | ||
| − | android/app/AndroidManifest.xml | + | android/app/src/main/AndroidManifest.xml |
<pre> | <pre> | ||
<meta-data | <meta-data | ||
| 行24: | 行24: | ||
==firebase設定を追加== | ==firebase設定を追加== | ||
| − | === | + | ===androidの準備=== |
| + | google-services.jsonをandroid/appの下へ | ||
| + | |||
| + | android/build.gradle | ||
| + | <pre> | ||
| + | buildscript { | ||
| + | repositories { | ||
| + | google() // Google's Maven repository | ||
| + | } | ||
| + | dependencies { | ||
| + | classpath 'com.google.gms:google-services:3.2.1' | ||
| + | } | ||
| + | } | ||
| + | allprojects { | ||
| + | repositories { | ||
| + | google() // Google's Maven repository | ||
| + | } | ||
| + | } | ||
| + | </pre> | ||
| + | |||
| + | android/app/build.gradle のファイルの末尾へapply~を追加 | ||
| + | <pre> | ||
| + | dependencies { | ||
| + | } | ||
| + | apply plugin: 'com.google.gms.google-services' // Google Play services Gradle plugin | ||
| + | </pre> | ||
===iosの準備=== | ===iosの準備=== | ||
xcode でios/Runner.xcworkspaceを開きRunner/RunnerへGoogleService-Info.plistファイルをドラッグする。 | xcode でios/Runner.xcworkspaceを開きRunner/RunnerへGoogleService-Info.plistファイルをドラッグする。 | ||
| + | |||
| + | ==サンプル== | ||
| + | 初回起動時 | ||
| + | <pre> | ||
| + | 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'); | ||
| + | } | ||
| + | </pre> | ||
| + | 広告表示 | ||
| + | <pre> | ||
| + | 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, | ||
| + | ); | ||
| + | </pre> | ||
| + | chorOffset: 50.0ぐらいにすると1行ぐらい開く。 | ||
| + | |||
| + | =="BannerAd event is MobileAdEvent.failedToLoad"エラーが出たら== | ||
| + | androidの場合は以下でテストデバイスidが上記testDeviceに設定されてるか確認する。 | ||
| + | $ 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
