Banner
1、Abstract methods that need to be implemented
- Developers only need to inherit
TPBannerAdapter
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、Banner 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, you need to create TPBannerAdImpl, pass in the View object that has been successfully loaded on the custom ad platform, and then call it back through the mLoadAdapterListener.loadAdapterLoaded()
method. **
//Parameter 1: null by default; Parameter 2: view that has been successfully loaded on the custom ad platform
TPBannerAdImpl mTpBannerAd = new TPBannerAdImpl(null, view);
//Through loadAdapterLoaded, implement the ad event loading success callback
mLoadAdapterListener.loadAdapterLoaded(mTpBannerAd);
(3) Implement the callback of the display ad event through the TPBannerAdImpl
object
Method | Description |
---|---|
onAdShown() | Implement the display callback of the ad event. |
onAdClosed() | Implement the closing callback of the ad event. |
onAdClicked() | Implement 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
/*
* The Banner type needs to inherit TPBannerAdapter and rewrite the following methods
* loadCustomAd() is used to obtain the parameters sent by the server and configured locally, and 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 FacebookBannerAdapter extends TPBannerAdapter {
private AdView mFacebookBanner;
private String placementId;
private TPBannerAdImpl mTpBannerAd;
private static final String TAG = "FacebookBanner";
@Override
public void loadCustomAd(Context context, Map<String, Object> userParams, Map<String, String> tpParams) {
// 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_CONFIGURATION_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
mFacebookBanner = new AdView(context, placementId, AdSize.BANNER_HEIGHT_50);
//Set FB monitoring and set monitoring callback
AdListener adListener = new AdListener() {
@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) {
Log.i(TAG, "onAdLoaded: ");
if (mFacebookBanner == null) {
return;
}
if (mLoadAdapterListener != null) {
//The Banner type needs to create TPBannerAdImpl and pass in the View object that has been successfully loaded.
mTpBannerAd = new TPBannerAdImpl(null, mFacebookBanner);
//Pass the TPBannerAdImpl object to loadAdapterLoaded to implement the ad event loading success callback
mLoadAdapterListener.loadAdapterLoaded(mTpBannerAd);
}
}
@Override
public void onAdClicked(Ad ad) {
Log.i(TAG, "onAdClicked: ");
//Use mTpBannerAd to implement click callback of ad events
if (mTpBannerAd != null) {
mTpBannerAd.adClicked();
}
}
@Override
public void onLoggingImpression(Ad ad) {
Log.i(TAG, "onLoggingImpression: ");
//Use mTpBannerAd to implement display callback of advertising events
if (mTpBannerAd != null)
mTpBannerAd.adShown();
}
};
//Request Ad
mFacebookBanner.loadAd(mFacebookBanner.buildLoadAdConfig().withAdListener(adListener).build());
}
@Override
public void clean() {
Log.i(TAG, "clean: ");
if (mFacebookBanner != null) {
Views.removeFromParent(mFacebookBanner);
mFacebookBanner.destroy();
mFacebookBanner = null;
}
}
@Override
public String getNetworkVersion() {
return BuildConfig.VERSION_NAME;
}
@Override
public String getNetworkName() {
return "audience-network";
}
}