AdMob in Flutter

Like for previous FlutterFire plugins, we need to add the dependency for AdMob to our pubspec.yaml, as follows: 

dependencies:
firebase_admob: ^0.8.0+4 # AdMob

After getting dependencies with flutter packages get, we are ready to use Firebase AdMob in our project. 

The FirebaseAdMob class is our starting point to add banners to the application. Unlike previously seen Firebase plugins that get all the information needed to run from the google-services.json (Android) and GoogleService-info.plist (iOS) files, in this case, we need an additional setting before we can use the plugin effectively.

We need to manually initialize the plugin with our application IDs. This can be done at any point. In our Favors app, for example, we can do this in the main method as shown here:

void main() {
FirebaseAdMob.instance.initialize(
appId: Platform.isAndroid
? 'ca-app-pub-3940256099942544~3347511713' // replace with your Android app id
: 'ca-app-pub-3940256099942544~1458002511', // replace with your iOS app id
);
runApp(MyApp());
}

As you can see, we initialize the plugin by providing our registered app ID (important for release). In the previous example, we are using just test IDs. This is the same value as present in the library's FirebaseAdMob.testAppId property. We can test our banners in the following two ways:

  • By using test ads provided by Google. With this, we use a set of mock ads, with no real traffic in our application ads.
This setting is really important, as generating invalid traffic to our apps can cause in account invalidation. So make sure you are using testing ads during development; find out more here: https://developers.google.com/admob/android/test-ads and after, change it to real app ID with test devices.
  • By adding testing devices with our real IDs. This is the preferred option, as it means we have the real look of the ads.
When using Android emulators or iOS simulators, they are automatically configured as test devices. For real devices, the first time you run a properly configured AdMob app, the test device ID will appear in LogCat (Android) or Console log (iOS). Use this ID to mark your device as a test device. Check out more here: https://developers.google.com/admob/ios/test-ads and https://developers.google.com/admob/android/test-ads.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.14.121.242