Native
Overview
Loading native ads consists of several stages:
- Loading BidMachine
NativeRequest - Setting up
AdManagerAdRequestaccording to loaded BidMachineNativeRequest - Loading AdManager
NativeAdwith configuredAdManagerAdRequestthrought AdManagerAdLoader - Loading BidMachine
NativeAd - Showing BidMachine
NativeAd
Auto-Refresh
Auto-refresh is not supported. For this type of integration, all of the above steps must be repeated.
Loading BidMachine NativeRequest
Create a new NativeRequest instance with a specific AdRequestListener.
Call request on the NativeRequest instance to start loading.
When the onRequestSuccess callback is triggered (it means that the request has been successfully loaded) - you can proceed to setting up AdManagerAdRequest.
// Create new BidMachine NativeRequest instance
NativeRequest nativeRequest = new NativeRequest.Builder()
.setListener(new NativeRequest.AdRequestListener() {
@Override
public void onRequestSuccess(@NonNull NativeRequest nativeRequest,
@NonNull AuctionResult auctionResult) {
runOnUiThread(() -> loadAdManagerNative());
}
})
.build();
// Request an ad from BidMachine without loading it
nativeRequest.request(...);
Setting up AdManagerAdRequest
Steps to set up AdManagerAdRequest by BidMachine NativeRequest:
- Create a new
AdManagerAdRequestinstance
AdManagerAdRequest adManagerAdRequest =
AMBidMachineUtils.createAdManagerRequest(nativeRequest);
- Create a new
AdManagerAdRequest.Builderinstance
AdManagerAdRequest.Builder adManagerAdRequestBuilder =
AMBidMachineUtils.createAdManagerRequestBuilder(nativeRequest);
AdManagerAdRequest adManagerAdRequest = adManagerAdRequestBuilder.build();
- Fill an existing
AdManagerAdRequest.Builderinstance
AdManagerAdRequest.Builder adManagerAdRequestBuilder =
new AdManagerAdRequest.Builder();
AMBidMachineUtils.appendRequest(adManagerAdRequestBuilder, nativeRequest);
AdManagerAdRequest adManagerAdRequest = adManagerAdRequestBuilder.build();
Loading AdManager NativeAd
When configuring the AdManager NativeAd, add an extra check to the top of onNativeAdLoaded callback in the OnNativeAdLoadedListener to determine if BidMachine has won/lost the mediation. Also, call notifyMediationWin/notifyMediationLoss on the NativeRequest instance when BidMachine wins/loses the mediation.
Use the AdManagerAdRequest that was created during the previous step to load the AdManager NativeAd.
AdLoader adManagerNativeAdLoader = new AdLoader.Builder(...)
.forNativeAd(nativeAd -> {
// Checking whether it is BidMachine or not
if (AMBidMachineUtils.isBidMachineNative(nativeAd)) {
// Notify BidMachine about win
bidMachineNativeRequest.notifyMediationWin();
// Load BidMachine ad object, before show BidMachine ad
loadBidMachineNative();
} else {
// Notify BidMachine about loss
bidMachineNativeRequest.notifyMediationLoss();
}
})
.withAdListener(...)
.build();
adManagerNativeAdLoader.loadAd(adManagerAdRequest);
Loading BidMachine NativeAd
If BidMachine has won the mediation, the next step will be loading BidMachine NativeAd.
NativeAd bidMachineNativeAd = new NativeAd(...);
bidMachineNativeAd.setListener(...);
bidMachineNativeAd.load(nativeRequest);
Show BidMachine NativeAd
After loading BidMachine NativeAd, register native ad and show it by adding it to ViewGroup.
Don't forget to check if it can be shown by executing
canShowon theNativeAdinstance before showing
if (bidMachineNativeAd != null && bidMachineNativeAd.canShow()) {
NativeAdContentLayout nativeAdContentLayout = (NativeAdContentLayout) getLayoutInflater()
.inflate(R.layout.item_bidmachine_native_ad, adContainer, false);
nativeAdContentLayout.bind(bidMachineNativeAd);
nativeAdContentLayout.registerViewForInteraction(bidMachineNativeAd);
adContainer.removeAllViews();
adContainer.addView(nativeAdContentLayout);
}
Sample App
You can find Sample App with BidMachine and AdManager here: Github
Updated 7 months ago