Here are some integration best practices to for developer implementation on Android. These coding tips and scenarios will help your team better understand our Audience Network SDK, make the integration process more effective, and keep your solution up to date after launch.
loadAd
again when the ad has been dismissed or ad loading comes across an error, except for banner ads.Even if you get notified of ad dismiss or error on the callback, ensure you have Max Number of Retries to avoid retrying indefinitely. The onError(Ad, AdError)
will get called no matter what causes ad load failure. For example, in case of "No Internet", you are supposed to stop loading the ad when it reaches to Max Number of Retries.
In the Audience Network SDK, for any type of ad except for banner ad, you should be careful when you call loadAd
. You should never call loadAd
while an ad is displaying. The best practice is to implement AdListener
to get notified whenever an ad has dismissed, completed, closed, or come across errors, which allows you to legally call loadAd
again and does not cause any issues.
loadAd
in onInterstitialDismissed
Callback.private InterstitialAd mInterstitialAd; private static final MAX_NUMBER_OF_RETRIES = 3; private boolean shouldLoadAd = true; private int retryCount = 0; @Override protected void onCreate(Bundle savedInstanceState) { // call the super class onCreate super.onCreate(savedInstanceState); ... mInterstitialAd = new InterstitialAd(this, "YOUR_PLACEMENT_ID"); InterstitialAdListener interstitialAdListener = new InterstitialAdListener() { ... @Override public void onInterstitialDismissed(Ad ad) { // Interstitial dismissed callback if (shouldLoadAd) { /* Change shouldLoadAd value to false, or a new interstitial ad will show immediately when previous interstitial ad gets dismissed. */ shouldLoadAd = false; mInterstitialAd.loadAd(); } } @Override public void onError(Ad ad, AdError adError) { // Ad error callback // Stop retrying when it reaches to MAX_NUMBER_OF_RETRIES if(retryCount < MainActivity.MAX_NUMBER_OF_RETRIES) { retryCount += 1; mInterstitialAd.loadAd(); } } ... }; mInterstitialAd.loadAd( mInterstitialAd.buildLoadAdConfig() .withAdListener(interstitialAdListener) .build()); ... }
onError
callback for NativeAd
.private static final MAX_NUMBER_OF_RETRIES = 3; private NativeAd mNativeAd; private int retryCount = 0; @Override protected void onCreate(Bundle savedInstanceState) { // call the super class onResume super.onCreate(savedInstanceState); ... mNativeAd = new NativeAd(this, "YOUR_PLACEMENT_ID"); NativeAdListener nativeAdListener = new AdListener() { ... @Override public void onError(Ad ad, AdError error) { // Ad error callback // Stop retrying when it reaches to MAX_NUMBER_OF_RETRIES if (retryCount < MainActivity.MAX_NUMBER_OF_RETRIES) { retryCount += 1; mNativeAd.loadAd(); } } ... }; mNativeAd.loadAd( mNativeAd.buildLoadAdConfig() .withAdListener(nativeAdListener) .build()); ... }
private Button showNativeAdButton; @Override public void onCreate(Bundle savedInstanceState) { // call the super class onResume super.onCreate(savedInstanceState); ... showNativeAdButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // Create a native ad request with a placement ID nativeAd = new NativeAd(NativeAdSampleActivity.this, "YOUR_PLACEMENT_ID"); // Create a listener to get notified when the ad was loaded. NativeAdListener nativeAdListener = new AdListener() { ... } // Initiate a request to load an ad. nativeAd.loadAd( nativeAd.buildLoadAdConfig() .withAdListener(nativeAdListener) .build()); } }); ... }
loadAd
in some Android callbacks, such as onResume()
or onStart()
callback, which may cause loadAd
to be called over and over without dismissing or releasing the last ad in memory.@Override protected void onCreate(Bundle savedInstanceState) { // call the super class onResume super.onCreate(savedInstanceState); ... mNativeAd = new NativeAd(this, "YOUR_PLACEMENT_ID"); ... } @Override protected void onResume() { super.onResume(); ... // DON'T DO THIS! mNativeAd.loadAd( mNativeAd.buildLoadAdConfig() .withAdListener(NativeAdSampleActivity.this) .build()); }
loadAd
indefinitely in onError callback. Below is an example of what you should never do. In case of "No Internet", loadAd
will be called over and over, which finally crashes your app.private NativeAd mNativeAd; private boolean shouldRetry = true; @Override protected void onCreate(Bundle savedInstanceState) { // call the super class onResume super.onCreate(savedInstanceState); ... mNativeAd = new NativeAd(this, "YOUR_PLACEMENT_ID"); NativeAdListener nativeAdListener = new AdListener() { ... @Override public void onError(Ad ad, AdError error) { // Ad error callback // Remember to change shouldRetry to false, or DON'T implement like this! if (shouldRetry) { mNativeAd.loadAd(); } } ... }; mNativeAd.loadAd( mNativeAd.buildLoadAdConfig() .withAdListener(NativeAdSampleActivity.this) .build()); ... }
Test ads integration with your app
Submit your app for review.
As soon as we receive a request for an ad from your app or website, we'll review it to make sure it complies with Audience Network policies and the Facebook community standards.
More Resources |
Getting Started GuideTechnical guide to get started with Audience Network | API ReferenceFacebook SDK for iOS Reference |