- https://pub.dev/packages/firebase_admob
- https://codelabs.developers.google.com/codelabs/admob-ads-in-flutter
- https://developers.google.com/admob/ios/test-ads
- https://developers.google.com/admob/ios/quick-start
- https://developers.google.com/admob/android/test-ads
- https://developers.google.com/admob/android/quick-start
Android app 設定方式:
1. 更新 flutter_app/pubspec.yaml 添加 dependencies (記得要運行 flutter pub get)
firebase: ^8.0.0
firebase_admob: ^0.11.0+1
2. 安置從 Firebase 建立專案時得到的 google-services.json 檔案,擺在 flutter_app/android/app/ 中
3. 更新 flutter_app/android/build.gradle 的 buildscript -> dependencies 設定
//classpath 'com.android.tools.build:gradle:3.5.0'// 為了修正錯誤訊息:error: unexpected element <queries> found in <manifest>.classpath 'com.android.tools.build:gradle:4.0.0'classpath 'com.google.gms:google-services:4.3.3'
4. 更新 flutter_app/android/gradle/wrapper/gradle-wrapper.properties 的 gradle 版本至 6.1.1
#為了修正錯誤訊息:Minimum supported Gradle version is 6.1.1. Current version is 5.6.2. If using the gradle wrapperdistributionUrl=https\://services.gradle.org/distributions/gradle-6.1.1-all.zip
5. 增加 flutter_app/app/build.gradle 的 apply 項目
apply plugin: 'com.google.gms.google-services'
6. 更新 flutter_app/android/app/src/main/AndroidManifest.xml 添加 meta data (僅範例程式數值)
<meta-data android:name="com.google.android.gms.ads.APPLICATION_ID" android:value="ca-app-pub-3940256099942544~3347511713"/>
7. 更新 flutter_app/lib/main.dart (此改自於範例程式,讓 add Button 可以產生一個插頁廣告)
iOS app 設定方式:
1. 更新 flutter_app/pubspec.yaml 添加 dependencies(記得要運行 flutter pub get)
firebase: ^8.0.0firebase_admob: ^0.11.0+1
2. 安置從 Firebase 建立專案時得到的 GoogleService-Info.plist ,用 Xcode 打開專案,把 GoogleService-Info.plist 拖拉擺入跟 Info.plist 的同階目錄位置。
% open flutter_app/ios/Runner.xcworkspace% ls flutter_app/ios/Runner/GoogleService-Info.plist
3. 更新 Info.plist 內容,添加資訊 GADApplicationIdentifier 等資訊(僅範例程式數值)
<key>GADApplicationIdentifier</key><string>ca-app-pub-3940256099942544~1458002511</string><key>SKAdNetworkItems</key><array><dict><key>SKAdNetworkIdentifier</key><string>cstr6suwn9.skadnetwork</string></dict></array>
4. 更新 Podfile ,上方添加 platform :ios, '10.0'
為了修正錯誤訊息:
Automatically assigning platform `iOS` with version `9.0` on target `Runner` because no platform was specified. Please specify a platform for this target in your Podfile.
5. 更新 flutter_app/lib/main.dart (此改自於範例程式,讓 add Button 可以產生一個插頁廣告)
如此就收工啦!而 flutter_app/lib/main.dart 內容如下:
import 'package:flutter/material.dart';import 'package:firebase_core/firebase_core.dart';import 'package:firebase_admob/firebase_admob.dart';Future<void> main() async {WidgetsFlutterBinding.ensureInitialized();await Firebase.initializeApp();runApp(MyApp());}class MyApp extends StatelessWidget {@overrideWidget build(BuildContext context) {return MaterialApp(title: 'Flutter Demo',theme: ThemeData(primarySwatch: Colors.blue,visualDensity: VisualDensity.adaptivePlatformDensity,),home: MyHomePage(title: 'Flutter Demo Home Page'),);}}class MyHomePage extends StatefulWidget {MyHomePage({Key key, this.title}) : super(key: key);final String title;@override_MyHomePageState createState() => _MyHomePageState();}class _MyHomePageState extends State<MyHomePage> {int _counter = 0;MobileAdTargetingInfo targetingInfo = MobileAdTargetingInfo(keywords: <String>['flutterio', 'beautiful apps'],contentUrl: 'https://flutter.io',childDirected: true,nonPersonalizedAds: true,);InterstitialAd _interstitialAd;InterstitialAd createInterstitialAd() {return InterstitialAd(adUnitId: InterstitialAd.testAdUnitId,targetingInfo: targetingInfo,listener: (MobileAdEvent event) {print("InterstitialAd event $event");},);}@overridevoid initState() {super.initState();FirebaseAdMob.instance.initialize(appId: FirebaseAdMob.testAppId);//_interstitialAd = createInterstitialAd()..load();}@overridevoid dispose() {_interstitialAd?.dispose();super.dispose();}void _incrementCounter() {_interstitialAd?.dispose();_interstitialAd = createInterstitialAd()..load()..show();setState(() {_counter++;});}@overrideWidget build(BuildContext context) {return Scaffold(appBar: AppBar(title: Text(widget.title),),body: Center(child: Column(mainAxisAlignment: MainAxisAlignment.center,children: <Widget>[Text('You have pushed the button this many times:',),Text('$_counter',style: Theme.of(context).textTheme.headline4,),],),),floatingActionButton: FloatingActionButton(onPressed: _incrementCounter,tooltip: 'Increment',child: Icon(Icons.add),), // This trailing comma makes auto-formatting nicer for build methods.);}}
沒有留言:
張貼留言