Native
1、Abstract methods that need to be implemented
- Developers only need to inherit
TPNativeAdapter
and rewrite related methods:- When developers call the
loadAd()
API of TP SDK, theloadCustomAd()
method of the custom Adapter will be called - When developers call the
destory()
API of TP SDK, theclean()
method of the custom Adapter will be called
- When developers call the
Abstract method | Parameter description | Return value | Function |
---|---|---|---|
loadCustomAd() | context context; userParams local configuration parameters; tpParams server-issued parameters | Context; Map; Map | Used to obtain server-issued and locally configured parameters to implement custom ad loading logic |
clean() | ----- | ----- | Used to release resources |
getNetworkVersion() | ----- | String | Custom third-party ad version number |
getNetworkName() | ----- | String | Custom third-party ad name |
2、Native ad event callback
(1)mLoadAdapterListener
implements callback of advertising events
Method | Description |
---|---|
loadAdapterLoadFailed(TPError) | Implements callback of loading failure of advertising events. TPError is described below. |
loadAdapterLoaded(TPBaseAd) | Implements callback of successful loading of advertising events. It is necessary to package the advertising information of the custom advertising platform in the inherited TPBaseAd object and return it to TP. |
(2)After the ad is loaded successfully, the ad information of the custom ad platform needs to be packaged in the inherited TPBaseAd
object, and then called back through the mLoadAdapterListener.loadAdapterLoaded()
method. The methods inside can be implemented according to the requirements of the custom ad platform, and it is not necessary to implement all of them
Abstract method | Parameter description | Return value | Function |
---|---|---|---|
getNetworkObj() | Object | ----- | Used to return the third-party object. If the developer needs to get the material himself, he can directly get this object |
registerClickView() | viewGroup Developer's own layout; clickViews Clickable ad view | ----- | Self-rendering type support, the developer will pass the clickable view after rendering (call before addview) |
registerClickAfterRender() | viewGroup Developer's own layout; clickViews Clickable ad view | ----- | Supports both self-rendering and template types. This method is called after addview value. It can be ignored if there is no special requirement |
getTPNativeView() | ----- | TPNativeAdView object | Self-rendering type support, used to obtain TP-wrapped objects, used to obtain all self-rendering materials. Self-rendering type cannot return null. |
getNativeAdType() | ----- | int | Used to get the ad type: AD_TYPE_NORMAL_NATIVE: self-rendering; AD_TYPE_NATIVE_EXPRESS: template rendering; AD_TYPE_NATIVE_LIST: multiple template views |
getRenderView() | ----- | view | Template type support, used to get the template rendering view, corresponding to the ad type AD_TYPE_NATIVE_EXPRESS. Template type cannot return null. |
getMediaViews() | ----- | ListView | Template type support, used to get multiple template rendering views, corresponding to AD_TYPE_NATIVE_LIST |
getCustomAdContainer() | ----- | ViewGroup | Used to get the container of the third-party platform (some third-party sources need to use the container they provide) |
clean() | ----- | ----- | Used to release resources |
(3) mShowListener
implements the callback of display advertising events
Method | Description |
---|---|
onAdShown() | Implements the display callback of the ad event. |
onAdClosed() | Implements the closing callback of the ad event. |
onAdClicked() | Implements the click callback of the ad event. |
3、Other API descriptions
- TPError
TPError tpError = new TPError(NETWORK_NO_FILL);
tpError.setErrorCode(adError.getErrorCode() +"");
tpError.setErrorMessage(adError.getErrorMessage());
TPError method | Description |
---|---|
setTpErrorCode() | Set the third-party ErrorCode error code. |
setErrorMessage() | Set the third-party ErrorMsg error message. |
NETWORK_NO_FILL | Customized advertising platform NOFILL. |
ADAPTER_CONFIGURATION_ERROR | Customized advertising platform server sent parameter error. |
SHOW_FAILED | Customized advertising platform display failed. |
- Local configuration parameter constants in
loadCustomAd()
Constant | Description |
---|---|
AppKeyManager.GDPR_CONSENT | GDPR, for more information, see Policy Compliance, the same below |
AppKeyManager.KEY_GDPR_CHILD | GDPRChild, GDPR children |
AppKeyManager.KEY_COPPA | COPPA, the United States Children's Online Privacy Protection Act |
AppKeyManager.KEY_CCPA | CCPA, the California Consumer Privacy Act |
4、Demo
/*
* Native type needs to inherit TPNativeAdapter and rewrite the following methods
* loadCustomAd() is used to obtain the parameters sent by the server and locally configured to implement the loading logic of the custom advertising platform
* clean() Release resources after the Banner ad is removed from the screen
* getNetworkVersion() Custom third-party source version number
* getNetworkName Custom third-party source name
* */
public class FacebookNativeAdapter extends TPNativeAdapter {
private NativeAd mFacebookNative;
private FacebookNativeAd mFacebookNativeAd;
private NativeBannerAd mNativeBannerAd;
private String placementId;
private Context mContext;
private static final String TAG = "FacebookNative";
@Override
public void loadCustomAd(Context context, Map<String, Object> userParams, Map<String, String> tpParams) {
mContext = context;
// tpParams Get the fields sent from the server
if (tpParams.size() > 0 && tpParams.containsKey("placemntId")) {
placementId = tpParams.get("placemntId");
} else {
/*
* mLoadAdapterListener is generated synchronously when oadCustomAd is rewritten
* Callback method loadAdapterLoaded: Ad loading is successful
* Callback method loadAdapterLoadFailed: Ad loading fails
* Construct TPError method, ADAPTER_CONFIGURATION_ERROR server sends parameter error
* Method setTpErrorCode sets the third-party ErrorCode error code
* Method setErrorMessage sets the third-party ErrorMsg error message
*
* */
if (mLoadAdapterListener != null) {
mLoadAdapterListener.loadAdapterLoadFailed(new TPError(ADAPTER_ACTIVITY_ERROR));
}
return;
}
// userParams Get the parameters from the local configuration
// For example: Overseas sources need to set CCPA and COPPA. For specific access, refer to the Advanced Features - Privacy Specifications section.
FacebookInitializeHelper.setUserParams(userParams, mLoadAdapterListener);
//Initialize SDK
FacebookInitializeHelper.initialize(context);
//Create a three-party ad slot object
mFacebookNative = new NativeAd(context, placementId);
// Request Native Template
mFacebookNative.loadAd(
mFacebookNative.buildLoadAdConfig()
.withAdListener(nativeAdListener)
.withMediaCacheFlag(NativeAdBase.MediaCacheFlag.ALL)
.build());
}
//Set FB monitoring and set monitoring callback
NativeAdListener nativeAdListener = new NativeAdListener() {
@Override
public void onError(Ad ad, AdError adError) {
Log.i(TAG, "onError: code :" + adError.getErrorCode() + " , msg:" + adError.getErrorMessage());
if (mLoadAdapterListener != null) {
TPError tpError = new TPError(SHOW_FAILED);
tpError.setErrorCode(adError.getErrorCode() + "");
tpError.setErrorMessage(adError.getErrorMessage());
mLoadAdapterListener.loadAdapterLoadFailed(tpError);
}
}
@Override
public void onAdLoaded(Ad ad) {
// Native Template
if (mFacebookNative != null) {
Log.i(TAG, "TemplateNativeAdLoaded: ");
View adView = NativeAdView.render(mContext, mFacebookNative);
// Add the Native Ad View to your ad container.
// The recommended dimensions for the ad container are:
// Width: 280dp - 500dp
// Height: 250dp - 500dp
RelativeLayout relativeLayout = new RelativeLayout(mContext);
relativeLayout.addView(adView, new RelativeLayout.LayoutParams(DeviceUtils.dip2px(mContext, 320)
, DeviceUtils.dip2px(mContext, 340)));
relativeLayout.setGravity(CENTER);
//After the Native type ad is loaded successfully, the ad information of the custom ad platform needs to be packaged in the inherited TPBaseAd object, and then called back through the mLoadAdapterListener.loadAdapterLoaded() method
//Parameter 2: Native type AppKeyManager.TEMPLATE_RENDERING_YES is the native template
mFacebookNativeAd = new FacebookNativeAd(relativeLayout, AppKeyManager.TEMPLATE_RENDERING_YES);
if (mLoadAdapterListener != null) {
mLoadAdapterListener.loadAdapterLoaded(mFacebookNativeAd);
}
} else {
/*
* Use mLoadAdapterListener to implement loading failure callback of advertising events
* */
if (mLoadAdapterListener != null) {
mLoadAdapterListener.loadAdapterLoadFailed(new TPError(UNSPECIFIED));
}
}
}
@Override
public void onAdClicked(Ad ad) {
Log.i(TAG, "onAdClicked: ");
//Execute the click callback of the ad event through the method in mFacebookNativeAd
if (mFacebookNativeAd != null)
mFacebookNativeAd.onAdViewClicked();
}
@Override
public void onLoggingImpression(Ad ad) {
Log.i(TAG, "onLoggingImpression: ");
//Execute the display callback of the ad event through the method in mFacebookNativeAd
if (mFacebookNativeAd != null)
mFacebookNativeAd.onAdViewExpanded();
}
@Override
public void onMediaDownloaded(Ad ad) {
}
};
@Override
public void clean() {
// Release resources
AdSettings.clearTestDevices();
if (mFacebookNativeAd != null)
mFacebookNativeAd.clean();
}
@Override
public String getNetworkVersion() {
return BuildConfig.VERSION_NAME;
}
@Override
public String getNetworkName() {
return "audience-network";
}
}
After Loaded successfully, wrap the advertising information of the custom advertising platform in the inherited TPBaseAd object and rewrite the TPBaseAd class code example
public class FacebookNativeAd extends TPBaseAd {
private static final String TAG = "FacebookNative";
private NativeAd mFacebookNative;
private AdOptionsView adOptionsView;
private View mFacebookView;
private String mAdSize;
private Context mContext;
private int isRender;
private TPNativeAdView mNativeAdView; //Self Rendering
private NativeAdLayout mContainer;
// Template type gets the View and ad type returned by the third party
public FacebookNativeAd(View facebookView ,int templete) {
mFacebookView = facebookView;
isRender = templete;
}
// The self-rendering type is a third-party object wrapped by TPNativeAdView
// private void initNativeAd(Context context) {
// mNativeAdView = new TPNativeAdView();
// mContainer = new NativeAdLayout(context);
// MediaView nativeAdMedia = new MediaView(context);
// adOptionsView = new AdOptionsView(context, mFacebookNative, mContainer);
//
// mNativeAdView.setCallToAction(mFacebookNative.getAdCallToAction());
// mNativeAdView.setTitle(mFacebookNative.getAdHeadline());
// mNativeAdView.setSubTitle(mFacebookNative.getAdBodyText());
// mNativeAdView.setAdSource(mFacebookNative.getSponsoredTranslation());
// mNativeAdView.setIconImageUrl(mFacebookNative.getAdIcon().getUrl());
// mNativeAdView.setMediaView(nativeAdMedia);
// }
public void onAdViewClicked() {
if (mShowListener != null) {
mShowListener.onAdClicked();
}
}
public void onAdViewExpanded() {
if (mShowListener != null) {
mShowListener.onAdShown();
}
}
/**
* Returns the third-party object. If a developer needs to get the material himself, he can get this object directly
* @return
*/
@Override
public Object getNetworkObj() {
return mFacebookNative == null ? mFacebookNative : null;
}
/**
* In self-rendering mode, developers will pass the clickable view after rendering (call before addview)
* @param viewGroup The developer's own layout, the main views are marked with tags
* @param clickViews Clickable ad view
*/
@Override
public void registerClickView(ViewGroup viewGroup, ArrayList<View> clickViews) {
// if (mFacebookNative != null) {
// View iconView = viewGroup.findViewWithTag(TPBaseAd.NATIVE_AD_TAG_ICON);
// if (mNativeAdView != null && mNativeAdView.getMediaView() instanceof MediaView && iconView instanceof ImageView) {
// mFacebookNative.registerViewForInteraction(
// viewGroup,
// (MediaView) mNativeAdView.getMediaView(),
// (ImageView) iconView,
// clickViews);
// }
//
// FrameLayout adChoicesView = viewGroup.findViewWithTag(TPBaseAd.NATIVE_AD_TAG_ADCHOICES);
// if (adChoicesView != null && adOptionsView != null) {
// adChoicesView.removeAllViews();
// adChoicesView.addView(adOptionsView, 0);
// }
// }
}
/**
* Get the object packaged by tradplus, which can get all the self-rendered materials
* Corresponding ad type AD_TYPE_NORMAL_NATIVE
* @return
*/
@Override
public TPNativeAdView getTPNativeView() {
return null;
// return mNativeAdView;
}
/**
* Get the ad type,
* AD_TYPE_NORMAL_NATIVE: self-rendering,
* AD_TYPE_NATIVE_EXPRESS: template rendering,
* AD_TYPE_NATIVE_LIST: multiple template views
* @return
*/
@Override
public int getNativeAdType() {
//Returns ad type, template type, self-rendering type
if (isRender == AppKeyManager.TEMPLATE_RENDERING_YES) {
return AD_TYPE_NATIVE_EXPRESS; //template type
} else {
return AD_TYPE_NORMAL_NATIVE;//self-rendering type
}
}
/**
* Get the view rendered by the template, corresponding to the ad type AD_TYPE_NATIVE_EXPRESS
* @return
*/
@Override
public View getRenderView() {
if (mFacebookView != null) {
return mFacebookView;
} else {
return null;
}
}
/**
* Get multi-template rendering view, corresponding to AD_TYPE_NATIVE_LIST
* @return
*/
@Override
public List<View> getMediaViews() {
return null;
}
/**
* Get the container of the third-party platform (some third-party sources need to use the container they provide)
* @return
*/
@Override
public ViewGroup getCustomAdContainer() {
return null;
}
// Release resources
@Override
public void clean() {
if (mFacebookNative != null) {
mFacebookNative.unregisterView();
mFacebookNative.destroy();
mFacebookNative = null;
}
}
}