Skip to main content

RewardVideo

1、Abstract methods that need to be implemented#

  • Developers only need to inherit TPRewardAdapter 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、Rewarded video 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 closing callback of the ad event.
onAdVideoError(TPError)Implement the display failure callback of the ad event. TPError method setTpErrorCode, set the ErrorCode error code of the third party;
onAdClicked()Implement the click callback of the ad event.
onReward()Implement the reward 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 rewarded video type needs to inherit TPRewardAdapter 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
* showAd() implements the logic of displaying the rewarded 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 FacebookInterstitialVideoAdapter extends TPRewardAdapter {
private RewardedVideoAd mRewardedVideoAd;
private String mPlacementId;
private static final String TAG = "FacebookRewardedVideo";
@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")) {
mPlacementId = 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
mRewardedVideoAd = new RewardedVideoAd(context, mPlacementId);
//Set FB monitoring and set monitoring callback
RewardedVideoAdListener rewardedVideoAdListener = new RewardedVideoAdListener() {
@Override
public void onRewardedVideoCompleted() {
Log.i(TAG, "onRewardedVideoCompleted: ");
//Use mShowListener to implement reward callbacks for advertising events
if (mShowListener != null) {
mShowListener.onReward();
}
}
@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(NETWORK_NO_FILL);
tpError.setErrorCode(adError.getErrorCode() +"");
tpError.setErrorMessage(adError.getErrorMessage());
mLoadAdapterListener.loadAdapterLoadFailed(tpError);
}
}
@Override
public void onAdLoaded(Ad ad) {
if (mRewardedVideoAd == null) {
return;
}
Log.i(TAG, "onAdLoaded: ");
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: ");
//Use mShowListener to implement display callback of advertising events
if (mShowListener != null) {
mShowListener.onAdShown();
}
}
@Override
public void onRewardedVideoClosed() {
Log.i(TAG, "onRewardedVideoClosed: ");
//Use mShowListener to implement the closing callback of advertising events
if (mShowListener != null) {
mShowListener.onAdClosed();
}
}
};
//Request Ads
RewardedVideoAd.RewardedVideoAdLoadConfigBuilder rewardedVideoAdLoadConfigBuilder = mRewardedVideoAd.buildLoadAdConfig().withAdListener(rewardedVideoAdListener);
mRewardedVideoAd.loadAd(rewardedVideoAdLoadConfigBuilder.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 (mRewardedVideoAd != null && mRewardedVideoAd.isAdLoaded()) {
mRewardedVideoAd.show();
}else {
if (mShowListener != null) {
mShowListener.onAdVideoError(new TPError(SHOW_FAILED));
}
}
}
@Override
public void clean() {
super.clean();
// Release resources
if (mRewardedVideoAd != null) {
mRewardedVideoAd.destroy();
mRewardedVideoAd = null;
}
}
@Override
public boolean isReady() {
// Used to determine whether the advertisement has expired
if (mRewardedVideoAd != null) {
return mRewardedVideoAd.isAdInvalidated();
}
return false;
}
@Override
public String getNetworkVersion() {
return BuildConfig.VERSION_NAME;
}
@Override
public String getNetworkName() {
return "audience-network";
}
}