Native Ads

Prerequisites

Display a native ad in your app

Displaying a native ad is a two-step process. First you should create and load your MobileFuseNativeAd unit, then when you’re ready, get the native assets and views to add to build your content.

  • Create an native ad unit
  • Get the native assets and views to display

Create a native ad unit

The MobileFuseNativeAd class provided by the MobileFuse SDK will handle the loading of the native ad assets and views.

📘

Tip

To provide a good user experience, you should create and pre-load your native ad units before you intend to display the native ad. Once a native ad unit is created and pre-loaded, you can retrieve the content instantly with the get content methods. Having the ad preloaded will ensure that you’re ready to display the ad as soon as the opportunity occurs. However, please note that ads expire after 5 minutes (at which time you’ll need to request a new one), so plan accordingly.

First, create a new MobileFuseNativeAd unit and keep a reference to it within the Activity that you want to use it:

private MobileFuseNativeAd nativeAd;

private void createNativeAd() {
    String placementId = "000000";
    nativeAd = new MobileFuseNativeAd(this, placementId);
}
private var nativeAd: MobileFuseNativeAd? = null

private fun createNativeAd() {
    val placementId = "000000"
    nativeAd = MobileFuseNativeAd(this, placementId)
} 

📘

Note

Ensure that the SDK has fully initialized before creating your ad units. You can use the onInitSuccess callback as a good place to set up your ad units.

Next, add a listener - the listener will allow you to receive events from the ad unit.

nativeAd.setListener(new MobileFuseNativeAd.Listener() {
    @Override
    public void onAdLoaded() {
        // Ad has loaded - you are able to call get content methods after this callback is triggered
    }

    @Override
    public void onAdNotFilled() {
        // No ad is currently available to display to this user
    }

    @Override
    public void onAdRendered() {
        // Triggered when the ad content is added to the view hierarchy
    }

    @Override
    public void onAdClicked() {
        // Triggered when the ad is clicked by the user
    }

    @Override
    public void onAdExpired() {
        // Triggered when a loaded ad has expired - you should manually try to load a new ad here
    }

    @Override
    public void onAdError(AdError error) {
        // An error occurred with the ad, examine the `error` argument to determine what went wrong
    }
});
nativeAd.setListener(object: MobileFuseNativeAd.Listener {
    override fun onAdLoaded() {
        // Ad has loaded - you are able to call get content methods after this callback is triggered
    }

    override fun onAdNotFilled() {
        // No ad is currently available to display to this user
    }

    override fun onAdRendered() {
        // Triggered when the ad content is added to the view hierarchy
    }

    override fun onAdClicked() {
        // Triggered when the ad is clicked by the user
    }

    override fun onAdExpired() {
        // Triggered when a loaded ad has expired - you should manually try to load a new ad here
    }

    override fun onAdError(AdError error) {
        // An error occurred with the ad, examine the `error` argument to determine what went wrong
    }
})

Finally, request that the ad is preloaded using the loadAd method:

nativeAd.loadAd();
nativeAd.loadAd()

Display the ad

Once the native ad has triggered the onAdLoaded callback, you can then call the get content methods to add the content to your view.

You can call the "has" methods to see if the loaded ad has certain content available:

nativeAd.hasTitle();
nativeAd.hasSponsoredText();
nativeAd.hasDescriptionText();
nativeAd.hasDisplayUrl();
nativeAd.hasCtaButtonText();
nativeAd.hasIcon();
nativeAd.hasMainImage();
nativeAd.hasMainVideo();
nativeAd.hasTitle
nativeAd.hasSponsoredText
nativeAd.hasDescriptionText
nativeAd.hasDisplayUrl
nativeAd.hasCtaButtonText
nativeAd.hasIcon
nativeAd.hasMainImage
nativeAd.hasMainVideo

The get content methods are split into two groups:

  • String values
  • Views
// String values
nativeAd.getTitle("TITLE TEXT");
nativeAd.getSponsoredText("SPONSORED TEXT");
nativeAd.getDescriptionText("DESCRIPTION TEXT");
nativeAd.getDisplayUrl("DISPLAY URL");
nativeAd.getCtaButtonText("CTA TEXT");

// Views
nativeAd.getIconView(); // View
nativeAd.getIconDrawable(); // Drawable
nativeAd.getMainImageView(); // View
nativeAd.getMainVideoView(); // View
nativeAd.getMainContentView(); // returns a View of either Main Image or Video (whichever is avaliable)
// String values
nativeAd.getTitle("TITLE TEXT")
nativeAd.getSponsoredText("SPONSORED TEXT")
nativeAd.getDescriptionText("DESCRIPTION TEXT")
nativeAd.getDisplayUrl("DISPLAY URL")
nativeAd.getCtaButtonText("CTA TEXT")

// Views
nativeAd.getIconView() // View
nativeAd.getIconDrawable() // Drawable
nativeAd.getMainImageView() // View
nativeAd.getMainVideoView() // View
nativeAd.getMainContentView() // returns a View of either Main Image or Video (whichever is avaliable)

Register for interaction

For the user to interact with the ad you are required to register the main content and clickable views:

nativeAd.registerViewForInteraction(mainContentView, listOfClickViews);
nativeAd.registerViewForInteraction(mainContentView, listOfClickViews)