Skip to main content

Intersitial

1、Abstract methods that need to be implemented#

  • Developers only need to inherit TPInterstitialAdapter and rewrite related methods:
    • When developers call the loadAd() API of TP SDK, the loadCustomAd() method of the custom Adapter will be called
    • When developers call the isReady() API of TP SDK, the isReady() method of the custom Adapter will be called
    • When developers call the showAd(Activity activity, String sceneId) API of TP SDK, the isReady() and showAd() methods of the custom Adapter will be called in sequence
Abstract methodParameter descriptionReturn valueFunction
loadCustomAd()context context; userParams local configuration parameters; tpParams server-issued parametersContext; Map; MapUsed to obtain server-issued and locally configured parameters to implement custom ad loading logic
showAd()----------Implement the logic of displaying custom ads
isReady()-----booleanUsed to determine whether the custom ad is expired before displaying the ad
clean()----------Used to release resources
getNetworkVersion()-----StringCustom third-party ad version number
getNetworkName()-----StringCustom third-party ad name

2、Interstitial ad event callback#

(1)mLoadAdapterListener implements callback of advertising events

MethodDescription
loadAdapterLoadFailed(TPError)Implements the callback for the failed loading of the ad event. TPError is described below.
loadAdapterLoaded(TPBaseAd)Implements the callback for the successful loading of the ad event. This type directly passes null

(2)mShowListener implements the callback of display ad events

MethodDescription
onAdShown()Implement the display callback of the ad event.
onAdClosed()Implement the close callback of the ad event.
onAdVideoError(TPError)Implement the display failure callback of the ad event. TPError Parameter 1: error code; Parameter 2: ErrorMessage, error message.
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 methodDescription
setTpErrorCode()Set the third-party ErrorCode error code.
setErrorMessage()Set the third-party ErrorMsg error message.
NETWORK_NO_FILLCustomized advertising platform NOFILL.
ADAPTER_CONFIGURATION_ERRORCustomized advertising platform server sent parameter error.
SHOW_FAILEDCustomized advertising platform display failed.
  • Local configuration parameter constants in loadCustomAd()
ConstantDescription
AppKeyManager.GDPR_CONSENTGDPR, for more information, see Policy Compliance, the same below
AppKeyManager.KEY_GDPR_CHILDGDPRChild, GDPR children
AppKeyManager.KEY_COPPACOPPA, the United States Children's Online Privacy Protection Act
AppKeyManager.KEY_CCPACCPA, the California Consumer Privacy Act

4、Demo#

/*
* The interstitial type needs to inherit TPInterstitialAdapter 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
* showAd() implements the logic of displaying the incentive video of the custom advertising platform
* isReady() is used to determine whether the custom ad is expired before displaying the ad
* clean() is used to release resources
* getNetworkVersion() The version number of the custom third-party source
* getNetworkName The name of the custom third-party source
* */
public class FacebookInterstitialAdapter extends TPInterstitialAdapter {
private InterstitialAd mFacebookInterstitial;
private String placementId;
private static final String TAG = "FacebookInterstitial";
@Override
public void loadCustomAd(final Context context,
final Map<String, Object> userParams,
final 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
mFacebookInterstitial = new InterstitialAd(context, placementId);
//Set FB monitoring and set monitoring callback
InterstitialAdListener interstitialAdListener = new InterstitialAdListener() {
@Override
public void onError(Ad ad, AdError adError) {
Log.i(TAG, "onError: code :" + adError.getErrorCode() + " , msg:" + adError.getErrorMessage());
//NETWORK_NO_FILL
if (mLoadAdapterListener != null) {
TPError tpError = new TPError(NETWORK_NO_FILL);
tpError.setErrorCode(adError.getErrorCode() + "");
tpError.setErrorMessage(adError.getErrorMessage());
mLoadAdapterListener.loadAdapterLoadFailed(tpError);
}
}
@Override
public void onAdLoaded(Ad ad) {
Log.i(TAG, "onAdLoaded: ");
if (mFacebookInterstitial == null) {
return;
}
if (mLoadAdapterListener != null) {
//Use mLoadAdapterListener to implement the ad event loading success callback
mLoadAdapterListener.loadAdapterLoaded(null);
}
}
@Override
public void onAdClicked(Ad ad) {
Log.i(TAG, "onAdClicked: ");
//Use mShowListener to implement click callback of advertising events
if (mShowListener != null) {
mShowListener.onAdClicked();
}
}
@Override
public void onLoggingImpression(Ad ad) {
Log.i(TAG, "onLoggingImpression: ");
}
@Override
public void onInterstitialDisplayed(Ad ad) {
Log.i(TAG, "onInterstitialDisplayed: ");
//Use mShowListener to implement display callback of advertising events
if (mShowListener != null) {
mShowListener.onAdShown();
}
}
@Override
public void onInterstitialDismissed(Ad ad) {
Log.i(TAG, "onInterstitialDismissed: ");
//Use mShowListener to implement the closing callback of advertising events
if (mShowListener != null) {
mShowListener.onAdClosed();
}
}
};
// request Ad
InterstitialAd.InterstitialAdLoadConfigBuilder interstitialAdLoadConfigBuilder = mFacebookInterstitial.buildLoadAdConfig().withAdListener(interstitialAdListener);
mFacebookInterstitial.loadAd(interstitialAdLoadConfigBuilder.build());
}
@Override
public void showAd() {
/*
* mShowListener is generated when showAd() is rewritten, and the user implements the event callback after calling show()
* Callback method onAdShown: Ad starts to display
* Callback method onAdClosed: Ad is closed
* Callback method onAdVideoError: Ad display failed, parameter 1: ErrorCode error code; parameter 2: ErrorMsg error message
* Callback method onAdClicked: Ad is clicked
* Callback method onReward: Ad reward
* */
if (mFacebookInterstitial != null && mFacebookInterstitial.isAdLoaded()) {
mFacebookInterstitial.show();
}else {
if (mShowListener != null) {
mShowListener.onAdVideoError(new TPError(SHOW_FAILED));
}
}
}
@Override
public void clean() {
// Release resources
if (mFacebookInterstitial != null) {
mFacebookInterstitial.destroy();
mFacebookInterstitial = null;
}
}
@Override
public boolean isReady() {
// Used to determine whether the advertisement has expired
if (mFacebookInterstitial != null) {
return !isAdsTimeOut() && !mFacebookInterstitial.isAdInvalidated();
}
return false;
}
@Override
public String getNetworkVersion() {
return BuildConfig.VERSION_NAME;
}
@Override
public String getNetworkName() {
return "audience-network";
}
}