I am trying to add an AdMob banner and I have been following the codelab here: https://codelabs.developers.google.com/codelabs/admob-ads-in-flutter#4
I have created an ad_helper.dart with the production values, and an ad_helper_test.dart with for test banners. I am currently importing the test one in my code and everything seems to work fine, I can see the test ads in the app.
So far I have been testing on emulators.
However, I'd like to know how can I switch to production values.
I considered using the production values even when testing, but Google says I have to mark my device as a test device, and I am not exactly sure how to do that.
Alternatively, if there's a way to have a different config for production builds, I'd consider using that, because the instructions on how to mark a device as a test one aren't very clear to me.
Thanks.
CodePudding user response:
The best practice is to use production ads by going to admod.google.com and creating real ad units. Replace the test ad units with the corresponding real ad units. Once done go to The Admod Site>Your test App>Settings>Test Devices and add your device's Advertising ID/IDFA. Finding your unique Advertising ID on Android
This article will help if you are using a real device. Since emulators are primarily programmed for testing purposes, they are automatically considered test devices by the Admob algorithm so there is no need to worry about policy violation.Just go ahead and run your application using real ad units. Google's policy on testing using emulators.
CodePudding user response:
I created one helper class. using this you can handle multiple Banner, interstitial, Reward Ads. You can try this:
- create enum first and add your Admob unit id & ad ids.
- just add that enum with ids in switch case both blocks (debug & release).
so for debug mode, you need to manage Ad ids once. just add your ids with a related ids case. for release mode switch case block add all ids platform-wise.
// define globally
enum Ads {
addUnitId,
//If Only single ids in app
bannerAdUnitId,
interstitialAdUnitId,
rewardedAdUnitId,
//If more than one ids in app
bannerAdHomeScreenId,
bannerAdSettingScreenId,
interstitialHomeAdUnitId,
interstitialSettingAdUnitId,
}
class AdHelper {
static String getAdmobAdId({required Ads adsName}) {
// check platform
final isPlatformAndroid = Platform.isAndroid;
// Testing IDs added from admob websites platform-wise.
final testAppUnitId =
isPlatformAndroid ? "ca-app-pub-3940256099942544~3347511713" : "ca-app-pub-3940256099942544~1458002511";
final testBannerAdId =
isPlatformAndroid ? "ca-app-pub-3940256099942544/6300978111" : "ca-app-pub-3940256099942544/2934735716";
final testInterstitialAdId =
isPlatformAndroid ? "ca-app-pub-3940256099942544/1033173712" : "ca-app-pub-3940256099942544/4411468910";
final testRewardAdId =
isPlatformAndroid ? "ca-app-pub-3940256099942544/5224354917" : "ca-app-pub-3940256099942544/1712485313";
if (kDebugMode) {
// If in debug mode
switch (adsName) {
case Ads.addUnitId:
return testAppUnitId;
// for all banner ads in app in Debug mode
case Ads.bannerAdUnitId:
case Ads.bannerAdHomeScreenId:
case Ads.bannerAdSettingScreenId:
return testBannerAdId;
// for all interstitial ads in app in Debug mode
case Ads.interstitialAdUnitId:
case Ads.interstitialHomeAdUnitId:
case Ads.interstitialSettingAdUnitId:
return testInterstitialAdId;
// for all reward ads in app in Debug mode
case Ads.rewardedAdUnitId:
return testRewardAdId;
default:
return "null";
}
} else {
switch (adsName) {
// Release mode real Ads id declare here based on enum Ads
case Ads.addUnitId:
return isPlatformAndroid ? "android_unit_id" : "iOS_unit_id";
case Ads.bannerAdUnitId:
return isPlatformAndroid ? "android_banner_id" : "iOS_banner_id";
case Ads.interstitialAdUnitId:
return isPlatformAndroid ? "android_interstitial_id" : "iOS_interstitial_id";
case Ads.rewardedAdUnitId:
return isPlatformAndroid ? "android_reward_id" : "iOS_reward_id";
//IF Multiple Banner/Reward Just add one by based on Enum
case Ads.bannerAdHomeScreenId:
return isPlatformAndroid ? "android_banner_home_id" : "iOS_banner_home_id";
case Ads.interstitialSettingAdUnitId:
return isPlatformAndroid ? "android_interstitial_setting_id" : "iOS_interstitial_setting_id";
default:
return "null";
}
}
}
}
How to use:
final adIds = AdHelper.getAdmobAdId(adsName: Ads.interstitialSettingAdUnitId);
For setup/add testing device check this link
Also, check this link use /check production ids while development/debug mode may be risky.
You can also add test device programmatically Check this link