Implement Bidding with Partner Mediation in your Android App

This guide shows you how to add a banner ad to your app using Facebook Audience Network and a mediation partner.

Facebook App Dashboard

  1. Create a new Facebook App for your app. This will give you a Facebook App ID that represents your app.
  2. Add a Verified Business to your app. You will use this business to get a System User Access Token needed for the secure connection between Facebook and Google.
  3. Add the Audience Network Product to your app. This will take you to the Facebook Monetization Manager.

Facebook Monetization Manager Dashboard

  1. In the left navigation, go to Integration > Properties
  2. Click Create Property and Create Ad Space, and add a name and platform (android app) and click the Create button
  3. In the Ad Space create a placement
  4. Give the placement name for your ad type
  5. Under the Get code dropdown get the Placement ID to be used in the mediation partner dashboard.

Facebook Business Settings Dashboard

  1. Get a System User Access Token by navigating to System Users in the left side navigation.
  2. Assign a new asset, your app, to your system user by clicking Add Asset
  3. In the popup window, click Apps > your app > type of access ( Develop app under Standard Access or Manage app under Admin Access). Save Changes.
  4. Click Generate New Token.
  5. In the popup select your app and add read_audience_network_insights and click Generate Token.
  6. Copy and save this token. Once you close the popup, you will not be able to retrieve this token.

Connect with Mediation Partners

For connecting your app to a third-party mediation provider, see the documentation for the mediation partners you want to connect with.

Medication ProviderIntegration Details
AdManager (DFP)

2 platforms

Android

iOS

AdMob

2 platforms

Android

iOS

Fyber

2 platforms

Android

iOS

ironSource

2 platforms

Android

iOS

MoPub

2 platforms

Android

iOS

Implement FB and Google SDKs in Code

  1. In your project’s build.gradle file add `mavenCentral()` to the buildscript repositories
  2. In the app's build.gradle file add the mediation provider SDKs, for example, Google, and the Facebook SDK.
    //Google Ads SDK
    implementation 'com.google.android.gms:play-services-ads:18.3.0'
    //Google Ads Mediation SDK for Facebook
    implementation 'com.google.ads.mediation:facebook:5.6.0.0'
    //Facebook Audience Network SDK
    implementation 'com.facebook.android:audience-network-sdk:5.+'

MainActivity.java File

  1. In MainActivity class add:
    //Will build the ad
    private AdView mAdView;  
    
    //Your Google Admod App ID 
    private static final String ADMOD_APP_ID = "ca-app-pub-6562014297208404~1337910868";
  2. In onCreate function add:
    //Initialize Facebook Audience Network Ads
    AudienceNetworkAds.initialize(this);
    
    //Initialize Google Mobile Ads
    MobileAds.initialize(this, ADMOD_APP_ID);
    
    //Create the ad (This example is a banner ad)
    AdView adView = new AdView(this);
    adView.setAdSize(AdSize.BANNER);
    adView.setAdUnitId("ca-app-pub-6562014297208404/1220388576");  //Your Google Ad Unit ID (For test ads, use Google’s sample Ad Unit ID)
    MobileAds.initialize(this, new OnInitializationCompleteListener() {
       @Override
       public void onInitializationComplete(InitializationStatus initializationStatus) {
       }
    
    mAdView = findViewById(R.id.adView);
    AdRequest adRequest = new AdRequest.Builder().build();
    mAdView.loadAd(adRequest);

Activity_main.xml File

Add the code to create the ad element.

<LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
   android:id = "@+id/parent"
   xmlns:tools = "http://schemas.android.com/tools"
   android:layout_width = "match_parent"
   android:layout_height = "match_parent"
   tools:context = ".MainActivity"
   android:gravity = "center"
   android:orientation = "vertical">
   <TextView
       android:id = "@+id/text"
       android:textSize = "28sp"
       android:textAlignment = "center"
       android:layout_width = "match_parent"
       android:layout_height = "wrap_content" />
</LinearLayout>

<com.google.android.gms.ads.AdView
   xmlns:ads="http://schemas.android.com/apk/res-auto"
   android:id="@+id/adView"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_centerHorizontal="true"
   android:layout_alignParentBottom="true"
   app:layout_constraintTop_toTopOf="parent"
   ads:layout_constraintRight_toRightOf="parent"
   ads:layout_constraintLeft_toLeftOf="parent"
   ads:adSize="BANNER"
   ads:adUnitId="ca-app-pub-6562014297208404/5457119275">
</com.google.android.gms.ads.AdView>

Network_security_config.xml File

To allow the connection to Facebook, we need to create a security xml file.

In the MyProject > app > src > main > res, create a new XML folder. In the new XML folder create an XML file named network_security_config.xml and add:

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>

   <domain-config cleartextTrafficPermitted="true">
       <domain includeSubdomains="true">127.0.0.1</domain>
   </domain-config>

</network-security-config>

AndroidManifest.xml File

Within the application manifest, add a meta-data tag for your google application ID and a meta-data tag for your Facebook app ID.

<application .../>

<meta-data
   android:name="com.google.android.gms.ads.APPLICATION_ID"
   android:value="ca-app-pub-6562014297208404~6273388795"/>

<meta-data
   android:name="com.facebook.sdk.ApplicationId"
   android:value="996910654020386"/>

...
</application>