Getting Started

  • Before integrating native ads in your app, you’ll need to create an account on the PubNative Dashboard, and integrate the SDK into your project.
  • Add a native ad unit to your app in the PubNative Dashboard
  • Make sure you have added the ad network SDKs you wish to use to your app

For native ads, first you need to create a request, then fill it with your data, and, after sending, provide your ad with a callback for the ad response.

Requesting Native Ads

Initializing Request

You can request a native ad by using PNRequest

PNRequest request = new PNRequest();
request.start(<CONTEXT>,  "<YOUR_APP_TOKEN_HERE>", "<YOUR_PLACEMENT_NAME_HERE>", new PNRequest.Listener() {

            @Override
            public void onPNRequestLoadFinish(PNRequest request, PNAdModel ad) {
                // Requested ad returned
            }

            @Override
            public void onPNRequestLoadFail(PNRequest request, Exception exception) {
                // Request failed
            }
        });

You can set up several data before starting the request by using the helper PNRequest methods. This is optional, but in the long term it will seriously improve your ad placement behavior.

Cache Resource

By default, resources are cached before serving ads. However, you can set it to false as follows in order to use urls to download resources such as icons, banners, etc.

request.setCacheResources(false);

Fill View

In order to display a native ad on screen, you need to get ad content by calling public methods of PNAdModel as follows:

pubnativeAdModel.getTitle();           // Ad's title

pubnativeAdModel.getDescription();     // Ad's description

pubnativeAdModel.getIcon();            // Ad's icon image view

pubnativeAdModel.getBanner();          // Ad's banner view

pubnativeAdModel.getStarRating();      // Rating of the ad. The rating range is 1-5.

pubnativeAdModel.getCallToAction();    // Ad's CTA text

Get Advertising Disclosure View

You can get the advertising disclosure view (ad choices, sponsor label, etc.) by using getAdvertisingDisclosureView().

pubnativeAdModel.getContentInfoView();

This will return a View that can be added into ad container.

Because advertising disclosure view depends on the network and there may be some networks that don’t have it, it is necessary to check for null before placement in any ViewGroup.

Note: It's always necessary to show advertising disclosure view on your ads.

Get the Ad's Image Bitmap

By default, caching is enabled for resources in PubNative Mediation-SDK, so you can get the images of the ad as a pre-cached bitmap, and, if requested, using getBanner and getIcon.

pubnativeAdModel.getIcon()   // this will return bitmap image that can be set to <ICON_VIEW>.

pubnativeAdModel.getBanner() // this will return bitmap image that can be set to <BANNER_VIEW>.

If you are disabling resource caching by using setCacheResources(false) while requesting, then you can also set listener to fetch resource images of the ad as bitmap. You can use FetchListener of PNAdModel as follows:

pubnativeAdModel.fetch(new PNAdModel.FetchListener() {

    @Override
    public void onFetchFinished() {
       // here you can get bitmap for resources
       pubnativeAdModel.getBanner() // this will return bitmap image that can be set to <BANNER_VIEW>.
       pubnativeAdModel.getIcon()   // this will return bitmap image that can be set to <ICON_VIEW>.
    }
});

Track

Once you have your ad rendered, you should make the model start tracking your view so we can confirm the impression and handle the clicks, this can be done with the following method from the PNAdModel

pubnativeAdModel.startTracking(<CONTEXT>, <AD_CONTAINER_VIEW_GROUP>);

You can also start tracking and filling content together by using:

pubnativeAdModel.withTitle(<TITlE_VIEW>) 
  .withDescription(<DESCRIPTION_VIEW>)
  .withIcon(<ICON_VIEW>)
  .withBanner(<BANNER_VIEW>)
  .withRating(<RATING_VIEW>)
  .withCallToAction(<CALL_TO_ACTION_VIEW>) //CALL_TO_ACTION_VIEW view can be TextView or Button view
  .withContentInfoContainer(<ADVERTISING_DISCLOSURE_VIEW>);
  .startTracking(<CONTEXT>, <AD_CONTAINER_VIEW_GROUP>);

If your ad is removed from the screen or you're exiting the activity, you'll need to stop the tracking process with the following method:

pubnativeAdModel.stopTracking()

You can also set up a listener on the model to listen for callbacks on the tracking process.

pubnativeAdModel.setListener(new PNAdModel.Listener() {

    @Override
    public void onPNAdImpression(PNAdModel model) {
        // Called when the ad impression was confirmed
    }

    @Override
    public void onPNAdClick(PNAdModel model) {
        // Called when the ad was clicked
    }
});

Other

If you disable resource caching by using setCacheResources(false) while requesting, then you can also set listener to fetch resources as follows:

pubnativeAdModel.fetch(new PubnativeAdModel.FetchListener() {

    @Override
    public void onFetchFinished() {
       // here you can get bitmap for resources
       pubnativeAdModel.getBanner() //this will return bitmap image that can be set to <BANNER_VIEW>.
       pubnativeAdModel.getIcon()   //this will return bitmap image that can be set to <ICON_VIEW>.
    }
});