Interstitial
Overview
Loading interstitial ad consists of several stages:
- Loading BidMachine
InterstitialRequest - Setting up
AdManagerAdRequestaccording to loaded BidMachineInterstitialRequest - Loading
AdManagerInterstitialAdwith configuredAdManagerAdRequest - Loading BidMachine
InterstitialAd - Showing BidMachine
InterstitialAd
Loading BidMachine InterstitialRequest
Create a new InterstitialRequest instance with AdRequestListener.
Call request on the InterstitialRequest instance to start loading.
When the onRequestSuccess callback is triggered (it means that the request has been successfully loaded)- you can proceed to AdManagerAdRequest settings using InterstitialRequest.
// Create new BidMachine request
InterstitialRequest interstitialRequest = new InterstitialRequest.Builder()
.setListener(new InterstitialRequest.AdRequestListener() {
@Override
public void onRequestSuccess(@NonNull InterstitialRequest interstitialRequest,
@NonNull AuctionResult auctionResult) {
runOnUiThread(() -> loadAdManagerInterstitial());
}
})
.build();
// Request an ad from BidMachine without loading it
interstitialRequest.request(this);
Setting up AdManagerAdRequest
Steps to set up AdManagerAdRequest by BidMachine InterstitialRequest:
- Create a new
AdManagerAdRequestinstance
AdManagerAdRequest adManagerAdRequest =
AMBidMachineUtils.createAdManagerRequest(interstitialRequest);
- Create a new
AdManagerAdRequest.Builderinstance
AdManagerAdRequest.Builder adManagerAdRequestBuilder =
AMBidMachineUtils.createAdManagerRequestBuilder(interstitialRequest);
AdManagerAdRequest adManagerAdRequest = adManagerAdRequestBuilder.build();
- Fill an existing
AdManagerAdRequest.Builderinstance
AdManagerAdRequest.Builder adManagerAdRequestBuilder =
new AdManagerAdRequest.Builder();
AMBidMachineUtils.appendRequest(adManagerAdRequestBuilder, interstitialRequest);
AdManagerAdRequest adManagerAdRequest = adManagerAdRequestBuilder.build();
Loading AdManagerInterstitialAd
BidMachine uses AppEventListener to receive mediation result. If data from it matches the expected result, then you need to start loading the BidMachine ad object; if data does not match - BidMachine has lost the mediation and the AdManagerInterstitialAd needed to be shown.
Also call notifyMediationWin/notifyMediationLoss on the InterstitialRequest instance when BidMachine wins/loses the mediation.
Use the AdManagerAdRequest that was created during the previous step to load the AdManagerInterstitialAd.
AdManagerInterstitialAdLoadCallback adLoadCallback = new AdManagerInterstitialAdLoadCallback() {
@Override
public void onAdLoaded(@NonNull AdManagerInterstitialAd adManagerInterstitialAd) {
// Wait for AppEventListener#onAppEvent to fire to determine if BidMachine won.
// It is recommended to add a timer to prevent mediation from stopping if BidMachine loses the mediation.
// In this case, AppEventListener#onAppEvent will not fire.
adManagerInterstitialAd.setAppEventListener(new AppEventListener() {
@Override
public void onAppEvent(@NonNull String key, @NonNull String value) {
// Checking whether it is BidMachine or not
if (AMBidMachineUtils.isBidMachineInterstitial(key)) {
// Don't forget to stop a timer.
// Notify BidMachine about win
bidMachineInterstitialRequest.notifyMediationWin();
// Load BidMachine ad object, before show BidMachine ad
loadBidMachineInterstitial();
}
}
});
}
};
AdManagerInterstitialAd.load(..., ..., adManagerAdRequest, adLoadCallback);
AppEventListener
Set
AppEventListenerimmediately afteronAdLoadedon the same thread, otherwiseonAppEventmight not come.
Loading BidMachine InterstitialAd
If BidMachine has won the mediation, the next step will be loading BidMachine InterstitialAd.
InterstitialAd interstitialAd = new InterstitialAd(this);
interstitialAd.setListener(...);
interstitialAd.load(interstitialRequest);
Show BidMachine InterstitialAd
After loading BidMachine InterstitialAd, show it by calling show on the InterstitialAd instance.
Don't forget to check if it can be shown by calling
canShowon theInterstitialAdinstance before showing
if (interstitialAd != null && interstitialAd.canShow()) {
interstitialAd.show();
}
Sample App
You can find Sample App with BidMachineSDK and Ad Manager here: Github
Updated almost 2 years ago