Integrating Admob

Create a new admob app from the following link
https://apps.admob.com/v2/apps

In flutter add the following lines in the strings.
<string name="admob_app_id">YOUR_ADMOB_APP_ID</string>

In the AndroidManifest.xml file add the following lines
<meta-data
                android:name="com.google.android.gms.ads.APPLICATION_ID"
                android:value="@string/admob_app_id"/>


And follow the sample codes below to start showing your ads in the application.

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

BannerAd myBanner = 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 = 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,
    // Positions the banner ad 10 pixels from the center of the screen to the right
    horizontalCenterOffset: 10.0,
    // Banner Position
    anchorType: AnchorType.bottom,
  );
myInterstitial
  ..load()
  ..show(
    anchorType: AnchorType.bottom,
    anchorOffset: 0.0,
    horizontalCenterOffset: 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.

Comments

Popular posts from this blog

Publishing Flutter app

Solving flutter web app CORS policy