Hinweis
Für den Zugriff auf diese Seite ist eine Autorisierung erforderlich. Sie können versuchen, sich anzumelden oder das Verzeichnis zu wechseln.
Für den Zugriff auf diese Seite ist eine Autorisierung erforderlich. Sie können versuchen, das Verzeichnis zu wechseln.
This article describes how to use the Android SDK Facebook adapter to retrieve and display native ads from Meta Audience Network.
SDK installation
Add the Android SDK and Facebook CSR adapter to your app module's build.gradle file. The adapter version combines the Android SDK version and its supported Meta Audience Network version. For other releases, see Android SDK release notes.
dependencies {
implementation 'com.appnexus.opensdk:appnexus-sdk:9.12.0'
implementation 'com.appnexus.opensdk.csr:appnexus-facebook-csr:9.12.0-6.22.0'
}
Initialize Facebook's Audience Network SDK
Early in your app lifecycle, initialize Meta Audience Network.
AudienceNetworkAds.buildInitSettings(this)
.withInitListener {
// Load ads after initialization completes
}
.initialize()
Note
When you register the native ad, provide either a Meta MediaView or an Android ImageView for the icon. Choose the corresponding FBNativeBannerAdResponse.registerView overload.
Create a native banner ad layout
In your activity layout, add a com.facebook.ads.NativeAdLayout container. This class wraps FrameLayout and enables Meta Audience Network to render its ad reporting flow.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
...
<com.facebook.ads.NativeAdLayout
android:id="@+id/native_banner_ad_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" />
...
</RelativeLayout>
Click here to view a sample code provided by Facebook for setting up native banner ad custom layouts.
Create a native ad request and load the ad
Note
Hold a reference to the request until you receive a response.
Check the response returned from NativeAdRequest. Register FBNativeBannerAdResponse directly with the Facebook adapter. Register other native responses with NativeAdSDK.
The following example assumes that adView, nativeAdIconView, and nativeAdCallToAction are initialized from your native ad layout.
class MainActivity : AppCompatActivity(), NativeAdRequestListener, NativeAdEventListener {
private lateinit var request: NativeAdRequest
private var response: NativeAdResponse? = null
private lateinit var adView: View
private lateinit var nativeAdIconView: ImageView
private lateinit var nativeAdCallToAction: Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
request = NativeAdRequest(this, "17823252") // Create the native ad request
request.listener = this // Set the request listener
request.loadAd() // Load the ad
}
override fun onAdLoaded(response: NativeAdResponse) {
this.response = response
nativeAdCallToAction.text = response.callToAction // Render the call-to-action text
val clickableViews = listOf<View>(nativeAdCallToAction)
if (response is FBNativeBannerAdResponse) {
response.registerView(adView, nativeAdIconView, clickableViews, this) // Register the Facebook response
} else {
NativeAdSDK.registerTracking(response, adView, clickableViews, this) // Register other native responses
}
}
override fun onAdFailed(errorCode: ResultCode, adResponseInfo: ANAdResponseInfo?) {
Log.d("NativeBanner", "Failed: ${errorCode.message}")
}
override fun onAdWasClicked() = Unit
override fun onAdWillLeaveApplication() = Unit
override fun onAdWasClicked(clickUrl: String, fallbackURL: String) = Unit
override fun onAdImpression() = Unit
override fun onAdAboutToExpire() = Unit
override fun onAdExpired() = Unit
override fun onDestroy() {
when (val currentResponse = response) {
is FBNativeBannerAdResponse -> currentResponse.unregisterView() // Unregister the Facebook response
null -> Unit
else -> NativeAdSDK.unRegisterTracking(adView) // Unregister other native responses
}
response = null
super.onDestroy()
}
}