Menu

Native Ad

1. Implementation of Native Ad Abstract Methods and Ad Event Callbacks

1.1 Additional Abstract Methods Required for Native Ads

Only need to inherit CustomNativeAdapter and implement the common abstract methods.

After the ad loads successfully, the custom ad platform's ad information needs to be wrapped in an object that inherits CustomNativeAd, and called back through the ATCustomLoadListener.onAdCacheLoaded method. The ad content needs to be provided by overriding the methods of CustomNativeAd: (The method implementations can follow the custom ad platform's requirements. They do not all need to be implemented.)

Method Parameters Return Value Description
isNativeExpress - boolean Used to determine whether the current native ad is a template ad.
getCustomAdContainer - ViewGroup Provide the container inherent to the custom ad platform for loading native ads.
getAdMediaView - View Used to provide the main image or video View of the native ad (if it is a template ad, the template ad's View should be provided through this method).
getTitle - String Used to provide the native ad title.
getDescriptionText - String Used to provide the native ad description.
getMainImageUrl - String Used to provide the native ad main image URL.
getIconImageUrl - String Used to provide the native ad icon URL.
getCallToActionText - String Used to provide the native ad CTA button text.
getStarRating - Double Used to provide the native ad star rating.
getAdChoiceIconUrl - String Used to provide the native ad's ad identifier URL.
getAdFrom - String Used to provide the native ad's ad source text.
getImageUrlList - List<String> Used to provide the native ad's image list.
prepare View view: The container displaying the native ad FrameLayout.LayoutParams layoutParams: The position of the ad badge. void Implement binding the native ad's click events.
prepare View view: The container displaying the native ad List<View> clickViewList: The list of Views that need click event binding FrameLayout.LayoutParams layoutParams: The position of the ad badge. void Implement binding the native ad's click events.
clear View view: The container displaying the native ad void Implement removing the ad click event binding.
onResume - void Executed when the native ad is displayed.
onPause - void Executed when the native ad is not displayed.
destory - void Executed when the native ad is destroyed.

1.2 Native Ad Event Callbacks

After wrapping the custom ad platform information in a class inheriting CustomNativeAd, ad event callbacks can be executed through the methods inside CustomNativeAd:

Method Description
notifyAdClicked Callback to the developer when the ad is clicked.
notifyAdDislikeClick Callback to the developer when the ad's Dislike button is clicked (not required if not implemented).
notifyAdVideoStart Callback to the developer when the ad's video starts playing (not required if not implemented).
notifyAdVideoEnd Callback to the developer when the ad's video ends (not required if not implemented).

1.3 Sample Code

java Copy
public class FacebookNativeAdapter extends CustomNativeAdapter {

    String mPayload;
    String unitId = "";
    String unitType = "";
    String unitHeight = "";
    boolean isAutoPlay = false;

    @Override
    public void loadCustomNetworkAd(final Context context, final Map<String, Object> serverExtras, final Map<String, Object> localExtras) {
        try {
            if (serverExtras.containsKey("unit_id")) {
                unitId = serverExtras.get("unit_id").toString();
            }

            if (serverExtras.containsKey("unit_type")) {
                unitType = serverExtras.get("unit_type").toString();
            }

            if (serverExtras.containsKey("height")) {
                unitHeight = serverExtras.get("height").toString();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (TextUtils.isEmpty(unitId)) {
            if (mLoadListener != null) {
                mLoadListener.onAdLoadError("", "facebook unitId is empty.");
            }
            return;
        }

        try {
            if (serverExtras != null) {
                isAutoPlay = Boolean.parseBoolean(serverExtras.get(CustomNativeAd.IS_AUTO_PLAY_KEY).toString());
            }
        } catch (Exception e) {

        }

        FacebookInitManager.getInstance().initSDK(context.getApplicationContext(), serverExtras);

        if (serverExtras.containsKey("payload")) {
            mPayload = serverExtras.get("payload").toString();
        }

        startAdLoad(context);

    }

    private void startAdLoad(final Context context) {
        NativeAdListener nativeAdListener = new NativeAdListener() {
            @Override
            public void onMediaDownloaded(Ad ad) {

            }

            @Override
            public void onError(Ad ad, AdError adError) {
                if (mLoadListener != null) {
                    mLoadListener.onAdLoadError(adError.getErrorCode() + "", adError.getErrorMessage());
                }
            }

            @Override
            public void onAdLoaded(Ad ad) {
                switch (unitType) {
                    case "1":
                        FacebookNativeBannerAd nativeBanner = new FacebookNativeBannerAd(context, (NativeBannerAd) ad, unitHeight);
                        if (mLoadListener != null) {
                            mLoadListener.onAdCacheLoaded(nativeBanner);
                        }
                        break;
                    default:
                        FacebookNativeAd nativeAd = new FacebookNativeAd(context, (NativeAd) ad);
                        if (mLoadListener != null) {
                            mLoadListener.onAdCacheLoaded(nativeAd);
                        }
                        break;
                }

            }

            @Override
            public void onAdClicked(Ad ad) {

            }

            @Override
            public void onLoggingImpression(Ad ad) {

            }
        };

        switch (unitType) {
            case "1":
                NativeBannerAd nativeBanner = new NativeBannerAd(context, unitId);
                nativeBanner.setAdListener(nativeAdListener);
                nativeBanner.loadAd();

                break;
            default:
                NativeAd nativeAd = new NativeAd(context, unitId);
                nativeAd.setAdListener(nativeAdListener);
                nativeAd.loadAd();
                break;
        }
    }

    @Override
    public void destory() {

    }

    @Override
    public String getNetworkName() {
        return FacebookInitManager.getInstance().getNetworkName();
    }

    @Override
    public boolean setUserDataConsent(Context context, boolean isConsent, boolean isEUTraffic) {
        return false;
    }

    @Override
    public String getNetworkPlacementId() {
        return unitId;
    }

    @Override
    public String getNetworkSDKVersion() {
        return FacebookInitManager.getInstance().getNetworkVersion();
    }
}
java Copy
public class FacebookNativeAd extends CustomNativeAd implements NativeAdListener {

    NativeAd mFacebookNativeAd;
    Context mContext;

    public FacebookNativeAd(Context context, NativeAd nativeAd) {
        mContext = context.getApplicationContext();
        mFacebookNativeAd = nativeAd;
        mFacebookNativeAd.setAdListener(this);
    }

    public void loadAd(String bidPayload) {
        if (TextUtils.isEmpty(bidPayload)) {
            mFacebookNativeAd.loadAd();
        } else {
            mFacebookNativeAd.loadAdFromBid(bidPayload);
        }

    }

    // Lifecycle Handlers
    @Override
    public void prepare(final View view, FrameLayout.LayoutParams layoutParams) {
        if (view == null) {
            return;
        }
        try {
            if (mContainer != null) {
                mFacebookNativeAd.registerViewForInteraction(mContainer, mMediaView, mAdIconView);
            } else {
                mFacebookNativeAd.registerViewForInteraction(view, mMediaView, mAdIconView);
            }
            prepareFacebookAdChoiceView(view, layoutParams);

        } catch (Exception e) {

        }
    }

    private void prepareFacebookAdChoiceView(View view, FrameLayout.LayoutParams layoutParams) {
        AdOptionsView adOptionsView = new AdOptionsView(view.getContext(), mFacebookNativeAd, mContainer);
        if (layoutParams == null) {
            layoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
            layoutParams.gravity = Gravity.RIGHT | Gravity.TOP;
        }

        if (layoutParams.height > 0) {
            float scale = mContext.getResources().getDisplayMetrics().density;
            int iconSize = (int) (layoutParams.height / scale + 0.5f);
            adOptionsView.setIconSizeDp(iconSize);
        }

        mContainer.addView(adOptionsView, layoutParams);
    }

    @Override
    public void prepare(View view, List<View> clickViewList, FrameLayout.LayoutParams layoutParams) {
        if (view == null) {
            return;
        }
        try {
            if (mContainer != null) {
                mFacebookNativeAd.registerViewForInteraction(mContainer, mMediaView, mAdIconView, clickViewList);
            } else {
                mFacebookNativeAd.registerViewForInteraction(view, mMediaView, mAdIconView, clickViewList);
            }
            prepareFacebookAdChoiceView(view, layoutParams);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    NativeAdLayout mContainer;

    @Override
    public ViewGroup getCustomAdContainer() {
        mContainer = new NativeAdLayout(mContext);
        return mContainer;
    }

    MediaView mMediaView;

    @Override
    public View getAdMediaView(Object... object) {
        try {
            if (mMediaView != null) {
                mMediaView.setListener(null);
                mMediaView.destroy();
                mMediaView = null;
            }
            mMediaView = new MediaView(mContext);
            mMediaView.setListener(new MediaViewListener() {
                @Override
                public void onPlay(MediaView mediaView) {
                }

                @Override
                public void onVolumeChange(MediaView mediaView, float v) {
                }

                @Override
                public void onPause(MediaView mediaView) {
                }

                @Override
                public void onComplete(MediaView mediaView) {
                    notifyAdVideoEnd();
                }

                @Override
                public void onEnterFullscreen(MediaView mediaView) {
                }

                @Override
                public void onExitFullscreen(MediaView mediaView) {
                }

                @Override
                public void onFullscreenBackground(MediaView mediaView) {
                }

                @Override
                public void onFullscreenForeground(MediaView mediaView) {
                }
            });
            return mMediaView;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

    }

    AdIconView mAdIconView;

    @Override
    public View getAdIconView() {
        try {
            if (mAdIconView != null) {
                mAdIconView.destroy();
                mAdIconView = null;
            }
            mAdIconView = new AdIconView(mContext);
            return mAdIconView;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void clear(final View view) {
        if (mMediaView != null) {
            mMediaView.setListener(null);
            mMediaView.destroy();
            mMediaView = null;
        }
        if (mFacebookNativeAd != null) {
            mFacebookNativeAd.unregisterView();
        }

    }

    @Override
    public void destroy() {
        if (mFacebookNativeAd != null) {
            mFacebookNativeAd.setAdListener(null);
            mFacebookNativeAd.unregisterView();
            mFacebookNativeAd.destroy();
            mFacebookNativeAd = null;
        }
        if (mMediaView != null) {
            mMediaView.setListener(null);
            mMediaView.destroy();
            mMediaView = null;
        }
        mContext = null;
        if (mAdIconView != null) {
            mAdIconView.destroy();
            mAdIconView = null;
        }
        if (mContainer != null) {
            mContainer.removeAllViews();
            mContainer = null;
        }
    }

    /**
     * facebook listener--------------------------------------------------------------------------------
     **/

    @Override
    public void onError(Ad ad, AdError adError) {

    }

    @Override
    public void onAdLoaded(Ad ad) {
    }

    @Override
    public String getTitle() {
        if (mFacebookNativeAd != null) {
            return mFacebookNativeAd.getAdHeadline();
        }
        return "";
    }

    @Override
    public String getDescriptionText() {
        if (mFacebookNativeAd != null) {
            return mFacebookNativeAd.getAdBodyText();
        }
        return "";
    }

    @Override
    public String getCallToActionText() {
        if (mFacebookNativeAd != null) {
            return mFacebookNativeAd.getAdCallToAction();
        }
        return "";
    }

    @Override
    public String getAdFrom() {
        if (mFacebookNativeAd != null) {
            return mFacebookNativeAd.getSponsoredTranslation();
        }
        return "";
    }

    @Override
    public void onAdClicked(Ad ad) {
        notifyAdClicked();
    }

    @Override
    public void onLoggingImpression(Ad ad) {
    }

    boolean mIsAutoPlay;

    public void setIsAutoPlay(boolean isAutoPlay) {
        mIsAutoPlay = isAutoPlay;
    }

    @Override
    public void onMediaDownloaded(Ad ad) {

    }
}
Previous
Interstitial Ad
Next
Rewarded Video
Last modified: 2026-07-08Powered by