Menu

Native Ad

1. Native advertising abstract method implementation and advertising event callbacks

1.1 Native advertising requires additional implementation of abstract methods

You only need to inherit CustomNativeAdapter and implement public abstract methods

After the advertisement is loaded successfully, The advertising information of the custom advertising platform needs to be packaged in an object inheriting CustomNativeAd and called back through the ATCustomLoadListener.onAdCacheLoaded method. The content of the advertisement needs to be provided by overriding the CustomNativeAd method: (method inside The implementation can be implemented according to the needs of the custom advertising platform, and it is not necessary to implement all of them)

MethodParametersReturn valueDescription
isNativeExpress-booleanUsed to determine whether the current native ad is a template ad
getCustomAdContainer-ViewGroupProvides a container for customizing the native advertising platform to load native ads< /td>
getAdMediaView-View is used to provide The large picture of the native ad or the view of the video (if it is a template ad, the view of the template ad needs to be provided through this method)
getTitle- StringUsed to provide native ad titles
getDescriptionText-StringUsed to provide native ad description
getMainImageUrl-StringUsed to provide native advertising large image url
getIconImageUrl-StringUsed to provide native ad icon url
getCallToActionText-StringUsed to provide native advertising CTA button copy
getStarRating-DoubleUsed for Provide star rating for native ads
getAdChoiceIconUrl-StringURL for the ad ID used to serve native ads
getAdFromStringAd source copy used to serve native ads
getImageUrlList-List<String> ;Image list used to serve native ads
prepareView view: Container for displaying native ads FrameLayout.LayoutParams layoutParams: The position of the ad tag voidRealize binding native ads Click event
prepareView view: List of containers that display native ads<View> clickViewList: List of Views that need to be bound to click events FrameLayout.LayoutParams layoutParams: Position of the ad tagvoidImplement the click event of binding native advertising
clearView view: Container for displaying native ads< /td>voidRealize ad click events Binding
onResume-voidExecuted when the native ad is displayed
onPause-voidExecuted when the native ad is not displayed
destory-voidExecuted when the native ad is destroyed

1.2 Native ad event callback

Customized advertising platform information packaging can be used to execute callbacks for advertising events through the methods in CustomiNative Ad after inheriting it:


方法说明
notifyAdClickedExecute callback to developers when advertising clicks
notifyAdDislikeClickWhen the advertisement clicks the Dislike button, execute a callback to the developer (if not implemented, do not execute)
notifyAdVideoStartExecute callback to developers during video playback of advertisements (optional if not implemented)
notifyAdVideoEndExecute a callback to the developer at the end of the advertisement video (optional if not implemented)

1.3 Sample Code


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();
    }
}

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
Banner Ad
Next
Splash Ad
Last modified: 2025-05-30Powered by