Skip to content
Related Articles

Related Articles

Improve Article

How to Integrate Facebook Audience Network (FAN) Rewarded Video Ads in Android?

  • Last Updated : 01 Sep, 2020
Geek Week

In order to earn money from the Android app or game, there are many ways such as in-App Purchases, Sponsorship, Advertisements, and many more. But there is another popular method to earn money from the Android app is by integrating a third party advertisement e.g known as Facebook Audience Network (FAN). Facebook Audience Network is designed to help monetize with the user experience in mind. By using high-value formats, quality ads, and innovative publisher tools it helps to grow the business while keeping people engaged.

Why Facebook Audience Network?

  • Facebook Audience Network is one of the best alternatives for Google Admob to monetize the Android or IOS App.
  • Minimum Payout is $100
  • Wide Range of Ad Formats
  • Maximum Fill Rates
  • High eCPM(Effective Cost Per Mille)
  • Quality Ads
  • Personalized Ads

Formats of Facebook Audience Network

There are mainly five types of flexible, high-performing format available in Facebook Audience Network

  • Native: Ads that you design to fit the app, seamlessly
  • Interstitial: Full-screen ads that capture attention and become part of the experience.
  • Banner: Traditional formats in a variety of placements.
  • Rewarded Video: An immersive, user-initiated video ad that rewards users for watching.
  • Playables: A try-before-you-buy ad experience allowing users to preview a game before installing.

In this article let’s integrate Facebook Audience Network Rewarded Video ads in the Android app.

Rewarded Video: 

  • A rewarded video ad is a full-screen ad that covers the whole UI of the app. The eCPM (Effective Cost Per Mille) of Rewarded Video ads are relatively higher than banner and Interstitial ads and also leads to higher CTR(Click Through Rate) which results in more earning from the app.
  • The user gets an in-App reward when they watch the Rewarded Video from start to end.

FAN rewared video



Approach

Step 1: Creating a New Project

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that choose Java as language though we are going to implement this project in Java language.

Step 2: Before going to the coding section first do some pre-task

  • Go to app -> res -> values -> colors.xml file and set the colors for the app.

colors.xml




<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#0F9D58</color>
    <color name="colorPrimaryDark">#0F9D58</color>
    <color name="colorAccent">#05af9b</color>
</resources>
  • Go to Gradle Scripts -> build.gradle (Module: app) section and import following dependencies and click the “sync now” on the above pop up.

 implementation ‘com.facebook.android:audience-network-sdk:5.+’

<uses-permission android:name=”android.permission.INTERNET”/>

Step 3: Designing the UI 

In the activity_main.xml file add only one Button, so whenever the user clicks the Button the Rewarded video ad will be played.

activity_main.xml




<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
  
    <!-- Button to Show Rewarded Video Ad By Clicking it -->
    <Button
        android:id="@+id/showVideoBtn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_margin="8dp"
        android:background="@color/colorPrimary"
        android:padding="16dp"
        android:text="Show  Rewarded  Video Ad "
        android:textColor="#ffff"
        android:textSize="20dp" />
  
</RelativeLayout>

Step 4: Working with MainActivity.java file

  • Open the MainActivity.java file there within the class, first create the object of the Button class.

 // Creating an object of Button class

Button showVideoAdBtn;



  • Now inside the onCreate() method, link those objects with their respective IDs that are given in activity_main.xml file.

 // link those objects with their respective id’s that we have given in activity_main.xml file 

 showVideoAdBtn = (Button) findViewById(R.id.showVideoBtn);

  • Now inside onCreate() method, initialize the Facebook Audience Network SDK

// initializing the Audience Network SDK

AudienceNetworkAds.initialize(this);

  • Create an object of RewardedVideoAd inside MainActivity.java class

// creating object of RewardedVideoAd

private RewardedVideoAd fbRewardedVideoAd;

  • Next create a private void loadRewardedVideoAd() method outside onCreate() method and define it.

void loadRewardedVideoAd()

{

 // initializing RewardedVideoAd Object

// RewardedVideoAd Constructor Takes 2 Arguments



// 1)Context

// 2)Placement Id

fbRewardedVideoAd = new RewardedVideoAd(this, “YOUR_PLACEMENT_ID”);

// loading Ad

fbRewardedVideoAd.loadAd();

}

  • Call the loadRewardedVideoAd() inside oncreate() method after initializing the SDK

Note: Replace “YOUR_PLACEMENT_ID” with your own placement id to show real ads.

  • Next create a void showRewardedVideoAd() method outside onCreate() method which we call later to show ad.

 public void showRewardedVideoAd()

 {

  // Checking If Ad is Loaded or Not

   if(fbRewardedVideoAd.isAdLoaded())

 { 

  // showing Video Ad

   fbRewardedVideoAd.show();

 }

else

{

  // Loading Video Ad If it  is Not Loaded

  fbRewardedVideoAd.loadAd();

 }

}

  • The next thing is to call the showRewardedVideoAd() method when a user clicks a show ad button.
  • Now in oncreate() method create a ClickListener for the button and call showRewardedVideoAd()

// click listener to show Rewarded Video Ad

showVideoAdBtn.setOnClickListener(new View.OnClickListener() {

           @Override

           public void onClick(View view) {

               showRewardedVideoAd();

           }

   });

  • Now we add RewardedVideoAdListener for Rewarded Video Ad, so that users will know the status of the ads.
  • To add RewardedVideoAdListener open loadRewardedVideoAd() method and add the below code before fbRewardedVideoAd.loadAd();

 // RewardedVideoAd AdListener

 fbRewardedVideoAd.setAdListener(new RewardedVideoAdListener() {



           @Override

           public void onError(Ad ad, AdError error) {

               // Showing Toast Message

               Toast.makeText(MainActivity.this, “onError”, Toast.LENGTH_SHORT).show();

           }

           @Override

           public void onAdLoaded(Ad ad) {

               // Showing Toast Message

               Toast.makeText(MainActivity.this, “onAdLoaded”, Toast.LENGTH_SHORT).show();

           }

           @Override

           public void onAdClicked(Ad ad) {

               // Showing Toast Message

               Toast.makeText(MainActivity.this, “onAdClicked”, Toast.LENGTH_SHORT).show();

           }

           @Override

           public void onLoggingImpression(Ad ad) {

               // Showing Toast Message

               Toast.makeText(MainActivity.this, “onLoggingImpression”, Toast.LENGTH_SHORT).show();

           }

           @Override

           public void onRewardedVideoCompleted() {

               // Showing Toast Message

               Toast.makeText(MainActivity.this, “onRewardedVideoCompleted”, Toast.LENGTH_SHORT).show();

           }

           @Override

           public void onRewardedVideoClosed() {

               // Showing Toast Message

               Toast.makeText(MainActivity.this, “onRewardedVideoClosed”, Toast.LENGTH_SHORT).show();

           }

  });

  • And inside RewardedVideoAdListener Override methods show a toast message so that users know the status of the ad. Below is the complete code for the MainActivity.java file.

MainActivity.java




package org.geeksforgeeks.project;
  
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.facebook.ads.Ad;
import com.facebook.ads.AdError;
import com.facebook.ads.AudienceNetworkAds;
import com.facebook.ads.RewardedVideoAd;
import com.facebook.ads.RewardedVideoAdListener;
  
public class MainActivity extends AppCompatActivity {
  
    // Creating a object of Button class
    Button showVideoAdBtn;
  
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        // link those objects with their respective id's
        // that we have given in activity_main.xml file
        showVideoAdBtn
            = (Button)findViewById(R.id.showVideoBtn);
  
        // initializing the Audience Network SDK
        AudienceNetworkAds.initialize(this);
  
        // initializing and loading rewarded video ad
        loadRewardedVideoAd();
  
        // click listener to show Rewarded Video  Ad
        showVideoAdBtn.setOnClickListener(
            new View.OnClickListener() {
                @Override public void onClick(View view)
                {
                    showRewardedVideoAd();
                }
            });
    }
  
    // creating object of RewardedVideoAd
    private RewardedVideoAd fbRewardedVideoAd;
  
    void loadRewardedVideoAd()
    {
        // initializing RewardedVideoAd Object
        // RewardedVideoAd  Constructor Takes 2 Arguments
        // 1)Context
        // 2)Placement Id
        fbRewardedVideoAd = new RewardedVideoAd(
            this, "YOUR_PLACEMENT_ID");
  
        // RewardedVideoAd AdListener
        fbRewardedVideoAd.setAdListener(
            new RewardedVideoAdListener() {
                @Override
                public void onError(Ad ad, AdError error)
                {
                    // Showing Toast Message
                    Toast
                        .makeText(MainActivity.this,
                                  "onError",
                                  Toast.LENGTH_SHORT)
                        .show();
                }
  
                @Override public void onAdLoaded(Ad ad)
                {
                    // Showing Toast Message
                    Toast
                        .makeText(MainActivity.this,
                                  "onAdLoaded",
                                  Toast.LENGTH_SHORT)
                        .show();
                }
  
                @Override public void onAdClicked(Ad ad)
                {
                    // Showing Toast Message
                    Toast
                        .makeText(MainActivity.this,
                                  "onAdClicked",
                                  Toast.LENGTH_SHORT)
                        .show();
                }
  
                @Override
                public void onLoggingImpression(Ad ad)
                {
                    // Showing Toast Message
                    Toast
                        .makeText(MainActivity.this,
                                  "onLoggingImpression",
                                  Toast.LENGTH_SHORT)
                        .show();
                }
  
                @Override
                public void onRewardedVideoCompleted()
                {
                    // Showing Toast Message
                    Toast
                        .makeText(
                            MainActivity.this,
                            "onRewardedVideoCompleted",
                            Toast.LENGTH_SHORT)
                        .show();
                }
  
                @Override
                public void onRewardedVideoClosed()
                {
                    // Showing Toast Message
                    Toast
                        .makeText(MainActivity.this,
                                  "onRewardedVideoClosed",
                                  Toast.LENGTH_SHORT)
                        .show();
                }
            });
  
        // loading Ad
        fbRewardedVideoAd.loadAd();
    }
  
    public void showRewardedVideoAd()
    {
        // Checking If Ad is Loaded
        // or Not
        if (fbRewardedVideoAd.isAdLoaded()) {
            // showing Video Ad
            fbRewardedVideoAd.show();
        }
        else {
            // Loading Video Ad If it  is Not Loaded
            fbRewardedVideoAd.loadAd();
        }
    }
}

Output: Run on Emulator

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :