firebase_admob 0.5.4+1

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • 94

firebase_admob

A plugin for Flutter that supports loading and displaying banner, interstitial (full-screen), and rewarded video ads using the Firebase AdMob API.

Note: This plugin is in beta, and may still have a few issues and missing APIs. Feedback and Pull Requests are welcome.

Initializing the plugin

The AdMob plugin must be initialized with an AdMob App ID.

FirebaseAdMob.instance.initialize(appId: appId);

Using banners and interstitials

Banner and interstitial ads can be configured with target information. And in the example below, the ads are given test ad unit IDs for a quick start.

MobileAdTargetingInfo targetingInfo = new MobileAdTargetingInfo(
  keywords: <String>['flutterio', 'beautiful apps'],
  contentUrl: 'https://flutter.io',
  birthday: new DateTime.now(),
  childDirected: false,
  designedForFamilies: false,
  gender: MobileAdGender.male, // or MobileAdGender.female, MobileAdGender.unknown
  testDevices: <String>[], // Android emulators are considered test devices
);

BannerAd myBanner = new BannerAd(
  // Replace the testAdUnitId with an ad unit id from the AdMob dash.
  // https://developers.google.com/admob/android/test-ads
  // https://developers.google.com/admob/ios/test-ads
  adUnitId: BannerAd.testAdUnitId,
  size: AdSize.smartBanner,
  targetingInfo: targetingInfo,
  listener: (MobileAdEvent event) {
    print("BannerAd event is $event");
  },
);

InterstitialAd myInterstitial = new InterstitialAd(
  // Replace the testAdUnitId with an ad unit id from the AdMob dash.
  // https://developers.google.com/admob/android/test-ads
  // https://developers.google.com/admob/ios/test-ads
  adUnitId: InterstitialAd.testAdUnitId,
  targetingInfo: targetingInfo,
  listener: (MobileAdEvent event) {
    print("InterstitialAd event is $event");
  },
);

Ads must be loaded before they're shown.

myBanner
  // typically this happens well before the ad is shown
  ..load()
  ..show(
    // Positions the banner ad 60 pixels from the bottom of the screen
    anchorOffset: 60.0,
    // Banner Position
    anchorType: AnchorType.bottom,
  );
myInterstitial
  ..load()
  ..show(
    anchorType: AnchorType.bottom,
    anchorOffset: 0.0,
  );

BannerAd and InterstitialAd objects can be disposed to free up plugin resources. Disposing a banner ad that's been shown removes it from the screen. Interstitial ads, however, can't be programmatically removed from view.

Banner and interstitial ads can be created with a MobileAdEvent listener. The listener can be used to detect when the ad has actually finished loading (or failed to load at all).

Using rewarded video ads

Unlike banners and interstitials, rewarded video ads are loaded one at a time via a singleton object, RewardedVideoAd.instance. Its load method takes an AdMob ad unit ID and an instance of MobileAdTargetingInfo:

RewardedVideoAd.instance.load(myAdMobAdUnitId, targetingInfo);

To listen for events in the rewarded video ad lifecycle, apps can define a function matching the RewardedVideoAdListener typedef, and assign it to the listener instance variable in RewardedVideoAd. If set, the listener function will be invoked whenever one of the events in the RewardedVideAdEvent enum occurs. After a rewarded video ad loads, for example, the RewardedVideoAdEvent.loaded is sent. Any time after that, apps can show the ad by calling show:

RewardedVideoAd.instance.show();

When the AdMob SDK decides it's time to grant an in-app reward, it does so via the RewardedVideoAdEvent.rewarded event:

RewardedVideoAd.instance.listener =
    (RewardedVideoAdEvent event, [String rewardType, int rewardAmount]) {
  if (event == RewardedVideoAdEvent.rewarded) {
    setState(() {
      // Here, apps should update state to reflect the reward.
      _goldCoins += rewardAmount;
    });
  }
};

Because RewardedVideoAd is a singleton object, it does not offer a dispose method.

Limitations

This is just an initial version of the plugin. There are still some limitations:

  • Banner ads have limited positioning functionality. They can be positioned at the top or the bottom of the screen and at a logical pixel offset from the edge.
  • Banner ads cannot be animated into view.
  • It's not possible to specify a banner ad's size.
  • There's no support for native ads.
  • The existing tests are fairly rudimentary.
  • There is no API doc.
  • The example should demonstrate how to show gate a route push with an interstitial ad

For Flutter plugins for other Firebase products, see FlutterFire.md.

0.5.4+1

  • Graduate to beta.

0.5.4

  • Fixed a bug that was causing rewarded video failure event to be called on the wrong listener.

0.5.3

  • Updated Google Play Services dependencies to version 15.0.0.
  • Added handling of rewarded video completion event.

0.5.2

  • Simplified podspec for Cocoapods 1.5.0, avoiding link issues in app archives.

0.5.1

  • Fixed Dart 2 type errors.

0.5.0

  • Breaking change. The BannerAd constructor now requires an AdSize parameter. BannerAds can be created with AdSize.smartBanner, or one of the other predefined AdSize values. Previously BannerAds were always defined with the smartBanner size.

0.4.0

  • Breaking change. Set SDK constraints to match the Flutter beta release.

0.3.2

  • Fixed Dart 2 type errors.

0.3.1

  • Enabled use in Swift projects.

0.3.0

  • Added support for rewarded video ads.
  • Breaking change. The properties and parameters named "unitId" in BannerAd and InterstitialAd have been renamed to "adUnitId" to better match AdMob's documentation and UI.

0.2.3

  • Simplified and upgraded Android project template to Android SDK 27.
  • Updated package description.

0.2.2

  • Added platform-specific App IDs and ad unit IDs to example.
  • Separated load and show functionality for interstitials in example.

0.2.1

  • Use safe area layout to place ad in iOS 11

0.2.0

  • Breaking change. MobileAd TargetingInfo requestAgent is now hardcoded to 'flutter-alpha'.

0.1.0

  • Breaking change. Upgraded to Gradle 4.1 and Android Studio Gradle plugin 3.0.1. Older Flutter projects need to upgrade their Gradle setup as well in order to use this version of the plugin. Instructions can be found here.
  • Relaxed GMS dependency to [11.4.0,12.0[

0.0.3

  • Add FLT prefix to iOS types
  • Change GMS dependency to 11.4.+

0.0.2

  • Change GMS dependency to 11.+

0.0.1

  • Initial Release: not ready for production use

example/lib/main.dart

// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter/material.dart';
import 'package:firebase_admob/firebase_admob.dart';

// You can also test with your own ad unit IDs by registering your device as a
// test device. Check the logs for your device's ID value.
const String testDevice = 'YOUR_DEVICE_ID';

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => new _MyAppState();
}

class _MyAppState extends State<MyApp> {
  static final MobileAdTargetingInfo targetingInfo = new MobileAdTargetingInfo(
    testDevices: testDevice != null ? <String>[testDevice] : null,
    keywords: <String>['foo', 'bar'],
    contentUrl: 'http://foo.com/bar.html',
    birthday: new DateTime.now(),
    childDirected: true,
    gender: MobileAdGender.male,
  );

  BannerAd _bannerAd;
  InterstitialAd _interstitialAd;
  int _coins = 0;

  BannerAd createBannerAd() {
    return new BannerAd(
      adUnitId: BannerAd.testAdUnitId,
      size: AdSize.banner,
      targetingInfo: targetingInfo,
      listener: (MobileAdEvent event) {
        print("BannerAd event $event");
      },
    );
  }

  InterstitialAd createInterstitialAd() {
    return new InterstitialAd(
      adUnitId: InterstitialAd.testAdUnitId,
      targetingInfo: targetingInfo,
      listener: (MobileAdEvent event) {
        print("InterstitialAd event $event");
      },
    );
  }

  @override
  void initState() {
    super.initState();
    FirebaseAdMob.instance.initialize(appId: FirebaseAdMob.testAppId);
    _bannerAd = createBannerAd()..load();
    RewardedVideoAd.instance.listener =
        (RewardedVideoAdEvent event, {String rewardType, int rewardAmount}) {
      print("RewardedVideoAd event $event");
      if (event == RewardedVideoAdEvent.rewarded) {
        setState(() {
          _coins += rewardAmount;
        });
      }
    };
  }

  @override
  void dispose() {
    _bannerAd?.dispose();
    _interstitialAd?.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      home: new Scaffold(
        appBar: new AppBar(
          title: const Text('AdMob Plugin example app'),
        ),
        body: new SingleChildScrollView(
          child: new Center(
            child: new Column(
              crossAxisAlignment: CrossAxisAlignment.center,
              mainAxisSize: MainAxisSize.min,
              children: <Widget>[
                new RaisedButton(
                    child: const Text('SHOW BANNER'),
                    onPressed: () {
                      _bannerAd ??= createBannerAd();
                      _bannerAd
                        ..load()
                        ..show();
                    }),
                new RaisedButton(
                    child: const Text('REMOVE BANNER'),
                    onPressed: () {
                      _bannerAd?.dispose();
                      _bannerAd = null;
                    }),
                new RaisedButton(
                  child: const Text('LOAD INTERSTITIAL'),
                  onPressed: () {
                    _interstitialAd?.dispose();
                    _interstitialAd = createInterstitialAd()..load();
                  },
                ),
                new RaisedButton(
                  child: const Text('SHOW INTERSTITIAL'),
                  onPressed: () {
                    _interstitialAd?.show();
                  },
                ),
                new RaisedButton(
                  child: const Text('LOAD REWARDED VIDEO'),
                  onPressed: () {
                    RewardedVideoAd.instance.load(
                        adUnitId: RewardedVideoAd.testAdUnitId,
                        targetingInfo: targetingInfo);
                  },
                ),
                new RaisedButton(
                  child: const Text('SHOW REWARDED VIDEO'),
                  onPressed: () {
                    RewardedVideoAd.instance.show();
                  },
                ),
                new Text("You have $_coins coins."),
              ].map((Widget button) {
                return new Padding(
                  padding: const EdgeInsets.symmetric(vertical: 16.0),
                  child: button,
                );
              }).toList(),
            ),
          ),
        ),
      ),
    );
  }
}

void main() {
  runApp(new MyApp());
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  firebase_admob: ^0.5.4+1

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter packages get

Alternatively, your editor might support flutter packages get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:firebase_admob/firebase_admob.dart';
  
Version Uploaded Documentation Archive
0.7.0 Nov 10, 2018 Go to the documentation of firebase_admob 0.7.0 Download firebase_admob 0.7.0 archive
0.6.1+1 Oct 12, 2018 Go to the documentation of firebase_admob 0.6.1+1 Download firebase_admob 0.6.1+1 archive
0.6.1 Sep 4, 2018 Go to the documentation of firebase_admob 0.6.1 Download firebase_admob 0.6.1 archive
0.5.7 Aug 21, 2018 Go to the documentation of firebase_admob 0.5.7 Download firebase_admob 0.5.7 archive
0.5.6 Jul 23, 2018 Go to the documentation of firebase_admob 0.5.6 Download firebase_admob 0.5.6 archive
0.5.5 Jun 1, 2018 Go to the documentation of firebase_admob 0.5.5 Download firebase_admob 0.5.5 archive
0.5.4+1 May 7, 2018 Go to the documentation of firebase_admob 0.5.4+1 Download firebase_admob 0.5.4+1 archive
0.5.4 May 2, 2018 Go to the documentation of firebase_admob 0.5.4 Download firebase_admob 0.5.4 archive
0.5.3 Apr 18, 2018 Go to the documentation of firebase_admob 0.5.3 Download firebase_admob 0.5.3 archive
0.5.2 Apr 11, 2018 Go to the documentation of firebase_admob 0.5.2 Download firebase_admob 0.5.2 archive

All 23 versions...

Popularity:
Describes how popular the package is relative to other packages. [more]
97
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
80
Overall:
Weighted score of the above. [more]
94
Learn more about scoring.

We analyzed this package on Nov 15, 2018, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.0.0
  • pana: 0.12.6
  • Flutter: 0.11.3

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Maintenance suggestions

The description is too short. (-20 points)

Add more detail about the package, what it does and what is its target use case. Try to write at least 60 characters.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev.28.0 <3.0.0
flutter 0.0.0
meta ^1.0.4 1.1.6
platform ^2.0.0 2.2.0
Transitive dependencies
collection 1.14.11
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_test
mockito ^2.0.2