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)
Method | Parameters | Return value | Description |
---|---|---|---|
isNativeExpress | - | boolean | Used to determine whether the current native ad is a template ad |
getCustomAdContainer | - | ViewGroup | Provides 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 | - | String | Used to provide native ad titles |
getDescriptionText | - | String | Used to provide native ad description |
getMainImageUrl | - | String | Used to provide native advertising large image url |
getIconImageUrl | - | String | Used to provide native ad icon url |
getCallToActionText | - | String | Used to provide native advertising CTA button copy |
getStarRating | - | Double | Used for Provide star rating for native ads |
getAdChoiceIconUrl | - | String | URL for the ad ID used to serve native ads |
getAdFrom | String | Ad source copy used to serve native ads | |
getImageUrlList | - | List<String> ; | Image list used to serve native ads |
prepare | View view: Container for displaying native ads FrameLayout.LayoutParams layoutParams: The position of the ad tag | void | Realize binding native ads Click event |
prepare | View 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 tag | void | Implement the click event of binding native advertising |
clear | View view: Container for displaying native ads< /td> | void | Realize ad click events 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 |
Customized advertising platform information packaging can be used to execute callbacks for advertising events through the methods in CustomiNative Ad after inheriting it:
方法 | 说明 |
---|---|
notifyAdClicked | Execute callback to developers when advertising clicks |
notifyAdDislikeClick | When the advertisement clicks the Dislike button, execute a callback to the developer (if not implemented, do not execute) |
notifyAdVideoStart | Execute callback to developers during video playback of advertisements (optional if not implemented) |
notifyAdVideoEnd | Execute a callback to the developer at the end of the advertisement video (optional if not implemented) |
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) {
}
}