Facebook Audience Network is a mobile advertising machine. It allows you to monetize your Android or IOS apps with facebook ads. You can monetize with facebook native ads, banner ads, interstitial ads and in-stream video ads. In this tutorial, you will learn to integrate facebook audience network SDK in android application and display facebook ads in your app.

You may see different types of facebook ads on instagram and facebook app. But now, facebook allows displaying their ads on other apps and mobile websites through Facebook Audience Network.

Related:
Facebook Integration in Android - Facebook Login
How to Integrate Google AdMob in Android
Integrating Google Firebase Analytics to your Android Project

Create a New Android Project

Let’s start by creating a new android project. Open Android Studio and create a new android project with app name “Facebook Audience Network Tutorial”, company domain “viralandroid.com”, set your minimum SDK version to API 15 or higher.

Facebook Audience Network Android SDK Integration


Add Facebook Audience Network Dependencies

Open your app level build.gradle file and add the following dependencies.

dependencies {
  ...
  compile 'com.facebook.android:audience-network-sdk:4.+'
}


Configure Your AndroidManifest.xml File

You need to add the INTERNET and ACCESS_NETWORK_STATE permissions and include AudienceNetworkActivity under the application element:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.viralandroid.facebookaudiencenetworktutorial">
  ...
<uses-permission android:name="android.permission.INTERNET"/>
  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

      <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
    ...
    <activity android:name="com.facebook.ads.AudienceNetworkActivity"
        android:configChanges="keyboardHidden|orientation|screenSize" />
    ...
  </application>
  ...
</manifest>

Configure Your App on Facebook

Go to facebook audience network page (https://developers.facebook.com/docs/audience-network), log in with your facebook account.

Facebook Audience Network Android SDK Integration

Click start button and create new facebook app or choose existing one.

Configure Ad Placements

1. Navigate to your app’s Audience Network setting.
2. In the Placement section, click Create Ad Placement

Facebook Audience Network Android SDK Integration


3. Give a name, step to trigger ad, choose display format.  The bottom slider allows you to optimize ad pricing for this placement. In most cases, Optimize for Fill will yield the highest revenue. Selecting Optimize for CPM will give higher price per impression but will reduce the fill rate. This may be the right setting if you are using mediation with multiple demand sources and are less concerned about unfilled requests.

4. Click Save to save your placement configuration.

Integrate Facebook Native Ad in Android

Facebook Native Ads allows you to build a customized UI for the ads you show in your app. You can customize and receive ad properties such as a title, an image, a call to action and so on.

Requesting a Facebook Native Ad in Android

Import the facebook ads SDK at the top of your activity:

import com.facebook.ads.*;

Create Your Native Ad Layout

Open your XML layout file and add a container for your Native Ad, Your XML layout file code will look like below.

res/layout/activity_main.xml


Create a custom native ad layout with name custom_native_ad_layout.xml. Your custom native ad layout code will look like below.

res/layout/custom_native_ad_layout.xml


Modifying Your App Java Activity File

Open your java activity file and then, instantiate a NativeAd object, set an AdListener, and call loadAd(). Following is the complete code for your java activity file:

src/MainActivity.java


Change NativeAd id with your ad unit. You will get ad unit under Audience Network > Placements.

Facebook Audience Network Android SDK Integration

Now run your Facebook Audience Network example project by clicking run button from menu bar. After running your app, you will see facebook ad in your app, which will look like above screenshot.

Download Complete Example Project

You can download complete Facebook Audience Network Android SDK Integration example project from GitHub.

Post a Comment

Powered by Blogger.