Skip to content

Instantly share code, notes, and snippets.

@vkrm26
Created May 23, 2020 18:09
Show Gist options
  • Save vkrm26/4bfe0362f16a0ab0c20ec603fb17758c to your computer and use it in GitHub Desktop.
Save vkrm26/4bfe0362f16a0ab0c20ec603fb17758c to your computer and use it in GitHub Desktop.
Facebook Audience Network - Native Ad Integration
import android.content.Context
import com.mopub.nativeads.AdapterHelper
import com.mopub.nativeads.FacebookAdRenderer
import com.mopub.nativeads.MoPubNative
import com.mopub.nativeads.MoPubStaticNativeAdRenderer
import com.mopub.nativeads.NativeAd
import com.mopub.nativeads.NativeAd.MoPubNativeEventListener
import com.mopub.nativeads.NativeErrorCode
import com.mopub.nativeads.ViewBinder
class AdLoader {
/**
* Call this method to load Native ad
* @param context : Activity Context
* @param customAdListener : Custom Ad Listener to get - ad loaded, clicked, impression, failed callback
* @param adType : Custom Class for different type of Ad Formats - Banner, Native, Interstitial
* @param adapterHelper : AdapterHelper from MoPub
* @param moPubNativeEventListener : MoPub specific class to track impression & click
*/
fun loadNativeAds(context: Context, customAdListener: CustomAdListener, adType: AdType, adapterHelper: AdapterHelper, moPubNativeEventListener: MoPubNativeEventListener){
// set isMopubSdkInitialised variable in App / Utils class to know if MoPub SDK initialized properly or not
if(App.isMopubSdkInitialised){
val moPubNative = MoPubNative(context, "ad_unit_id", getMoPubNativeNetworkListener(adapterHelper, customAdListener, moPubNativeEventListener))
requestAndLoadNativeAd(moPubNative, adType)
}
}
/**
* This method integrates view binders & ad renderer for both MoPub & FAN, then request Ad to mopub
* @param moPubNative : MoPubNative object
* @param adType : Custom Class for different type of Ad Formats - Banner, Native, Interstitial
*/
private fun requestAndLoadNativeAd(moPubNative : MoPubNative, adType: AdType) {
// mopub view binder
val moPubNativeViewBinder = ViewBinder.Builder(R.layout.mopub_native_small_ad_view)
.iconImageId(R.id.native_icon_image)
.titleId(R.id.native_title)
.textId(R.id.native_text)
.privacyInformationIconImageId(R.id.native_privacy_information_icon_image)
.callToActionId(R.id.native_cta)
.build()
// mopub native ad renderer
val moPubStaticNativeAdRenderer = MoPubStaticNativeAdRenderer(moPubNativeViewBinder)
// facebook view binder
val facebookViewBinder = FacebookAdRenderer.FacebookViewBinder.Builder(R.layout.medium_native_fb_ad)
.titleId(R.id.native_title)
.textId(R.id.native_text)
// Binding to new layouts from Facebook 4.99.0+
.mediaViewId(R.id.native_ad_main_image)
.adIconViewId(R.id.native_ad_icon_image)
.adChoicesRelativeLayoutId(R.id.native_ad_choices_relative_layout)
.advertiserNameId(R.id.native_title) // Bind either the titleId or advertiserNameId depending on the FB SDK version
// End of binding to new layouts
.callToActionId(R.id.native_cta)
.build()
// facebook ad renderer
val facebookAdRenderer = FacebookAdRenderer(facebookViewBinder)
// this is needed for Facebook Native Ad
moPubNative.setLocalExtras(getFBLocalExtra(adType) as HashMap<String, Any>)
// register Facebook Ad Renderer
moPubNative.registerAdRenderer(facebookAdRenderer)
// register MoPub Ad Renderer - Always add this in last
moPubNative.registerAdRenderer(moPubStaticNativeAdRenderer)
// make request to MoPub which internally talk to FAN also
moPubNative.makeRequest()
}
/**
* This provides FAN related parameters for Native Ads
* @param adType : Custom Class for different type of Ad Formats - Banner, Native, Interstitial
*/
private fun getFBLocalExtra(adType: AdType) : HashMap<String, Boolean> {
val localExtras = HashMap<String, Boolean>()
when (adType) {
AdType.NATIVE_MEDIUM -> {
localExtras["native"] = true
return localExtras
}
AdType.NATIVE_SMALL -> {
localExtras["native_banner"] = true
return localExtras
}
}
// default :: native banner
localExtras["native_banner"] = true
return localExtras
}
/**
* Returns MoPub Native Network Listener which gives callback when native is loaded or failed
* @param adapterHelper : AdapterHelper from MoPub
* @param mCustomAdListener : Custom Ad Listener to get - ad loaded, clicked, impression, failed callback
* @param moPubNativeEventListener : MoPub specific class to track impression & click
* @return MoPubNativeNetworkListener : Network listener for Ad Loading
*/
private fun getMoPubNativeNetworkListener(adapterHelper : AdapterHelper, mCustomAdListener : CustomAdListener, moPubNativeEventListener : MoPubNativeEventListener) : MoPubNative.MoPubNativeNetworkListener {
return object : MoPubNative.MoPubNativeNetworkListener {
override fun onNativeLoad(nativeAd: NativeAd?) {
// Retrieve the pre-built ad view that AdapterHelper prepared for us.
// Retrieve the pre-built ad view that AdapterHelper prepared for us.
val renderedAdView = adapterHelper!!.getAdView(null, null, nativeAd, ViewBinder.Builder(0).build())
// Set the native event listeners (onImpression, and onClick).
nativeAd!!.setMoPubNativeEventListener(moPubNativeEventListener)
mCustomAdListener?.onNativeAdLoaded(renderedAdView)
}
override fun onNativeFail(errorCode: NativeErrorCode?) {
mCustomAdListener?.onAdFailed(errorCode?.intCode!!)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment