Custom Manual Integration of Native Ads - MoPub-Solutions-Eng/mopub.wiki.android.native.betasite GitHub Wiki

Compare to Manual approach, the Custom Manual approach for native ads gives publishers the ability to extract ad assets and render them manually. This approach provides an additional degree of customization, and is useful for use cases where more control over ad assets is needed. However, it may increase more complexity. With this integration, publishers have to manually request and receive ads, handle caching, and handle cases where there is no fill. Please do note as this is a non-standard and non-recommended approach, we are not able to render fully support.

For more details on other supported integration methods, please check out the Native Ad Integration Types Overview.

Prerequisite

Before integrating native ads, follow the steps in our Android Getting Started Guide to create a MoPub account & integrate the MoPub SDK into your project. Additionally, check the Best Practices for guidelines on how native ads should be displayed in your app.

Requesting and Rendering Native Ads

  1. Create a MoPubNative instance, specifying a Context, (String) Ad Unit ID, and a MoPubNativeNetworkListener object.
MoPubNative moPubNative = new MoPubNative(context, adUnitID, moPubNativeNetworkListener);
  1. Create a MoPubStaticNativeAdRenderer and register it with the MoPubNative instance with a dummy ViewBinder.
ViewBinder viewBinder = new ViewBinder.Builder(0).build();

MoPubStaticNativeAdRenderer moPubStaticNativeAdRenderer = new MoPubStaticNativeAdRenderer(viewBinder);
moPubNative.registerAdRenderer(moPubStaticNativeAdRenderer);

}

moPubNative = new MoPubNative(this, "11a17b188668469fb0412708c3d16813 ", moPubNativeNetworkListener);

To test your implementation, use the following test ad unit ID: 11a17b188668469fb0412708c3d16813
It will always return a static MoPub native creative.


  1. Call MoPubNative.makeRequest(), which will request an ad from the server and download associated resources asynchronously. You may optionally pass in a RequestParameters object to specify targeting information, including keywords and location.
RequestParameters requestParameters = new RequestParameters.Builder().keywords("gender: " + mediationAdRequest.getGender() + ", age:" + mediationAdRequest.getBirthday())       .location(mediationAdRequest.getLocation()).build();

moPubNative.makeRequest(requestParameters);

  1. Upon success, the MoPubNativeNetworkListener.onNativeLoad() method will be called with an instance of NativeAd. In this method, retrieve the ad assets like below:
  • Retrieve your NativeAd object in step 6 of the manual integration tutorial (i.e. in the success callback of MoPubNativeNetworkListener.onNativeLoad()).
  • (Optional) To receive callbacks for impression and click tracking, in the onNativeLoad() callback, register the MoPubNativeEventListener, and override onImpression() and onClick():
nativeAd.setMoPubNativeEventListener(new NativeAd.MoPubNativeEventListener() {
            @Override
            public void onImpression(View view) {
...
            }
            @Override
            public void onClick(View view) {
...
            }
        });
  • Get the BaseNativeAd object: BaseNativeAd adData = nativeAd.getBaseNativeAd();
  • Typecast it to either a StaticNativeAd or a VideoNativeAd:
    if (adData instanceOf StaticNativeAd) {
    StaticNativeAd staticAdData = (StaticNativeAd) adData;
    }
  • adData will now have all the getters of the type-casted class, like adData.getTitle(). For a complete list of available getters, refer to this section in StaticNativeAd.java.

  1. Now, use the assets retrieved in step 4 to construct your own ad. Once done, pass the parent view to StaticNativeAd.prepare() to register impression and click tracking on your view.

  1. Make sure to add the privacy information icon to your view with the help of adData.getPrivacyInformationIconImageUrl(). Below is some additional steps:
privacyInformationIconImageView = new ImageView(context);

String privacyInformationImageUrl = adData.getPrivacyInformationIconImageUrl();
final String privacyInformationClickthroughUrl =  adData.getPrivacyInformationIconClickThroughUrl();

if (privacyInformationImageUrl == null) {   
privacyInformationIconImageView.setImageDrawable(Drawables.NATIVE_PRIVACY_INFORMATION_ICON.createDrawable(context));
} else {
   NativeImageHelper.loadImageView(privacyInformationImageUrl,
           privacyInformationIconImageView);
}

privacyInformationIconImageView.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(final View v) {
       new UrlHandler.Builder()
               .withSupportedUrlActions(
                       UrlAction.IGNORE_ABOUT_SCHEME,
                       UrlAction.OPEN_NATIVE_BROWSER,
                       UrlAction.OPEN_IN_APP_BROWSER,
                       UrlAction.HANDLE_SHARE_TWEET,
                       UrlAction.FOLLOW_DEEP_LINK_WITH_FALLBACK,
                       UrlAction.FOLLOW_DEEP_LINK)
               .build().handleUrl(context, privacyInformationClickthroughUrl);
   }
});

Important: To refresh or render a new ad into the same view, make sure to call StaticNativeAd.clear() on your ad view before attaching the new view. This will clear the state associated with the view. Then, call StaticNativeAd.prepare() for impression and click registration.