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


To integrate Facebook Audience Network (FAN) rewarded video ads into your Android app, you can tap into the vast reach and monetization potential of FAN. The process involves incorporating FAN's SDK into your app, creating a rewarded video ad placement through your FAN account, and then loading and displaying the rewarded video ads within your app. By integrating FAN rewarded video ads, you can offer engaging and incentivized video content to your users while generating revenue through ad impressions and interactions. This integration allows you to leverage FAN's powerful ad network and maximize your app's monetization opportunities.

Facebook Audience Network (FAN)

Facebook Audience Network (FAN) is an advertising platform offered by Facebook that enables mobile app developers to monetize their applications by displaying targeted ads to a vast network of Facebook advertisers. FAN leverages the immense user data available on Facebook to deliver personalized and relevant ads to app users, helping developers generate revenue through ad impressions and interactions. It provides a seamless integration process and offers various ad formats, including rewarded video ads, to enhance user engagement while maximizing monetization potential for app developers.

Approaches

To integrate Facebook Audience Network (FAN) rewarded video ads into an Android app, you can follow the two methods described below::

  • Using Facebook Audience Network SDK

  • Using Google Mobile Ads SDK Mediation

Using Facebook Audience Network SDK

To integrate Facebook Audience Network (FAN) rewarded video ads in Android using the FAN SDK, you need to add the SDK to your project, initialize it with your App ID, and create a rewarded video ad placement in your FAN account. Then, in your app's code, you can load the rewarded video ad using the Placement ID and implement listeners to handle ad events. Finally, when the ad is loaded, you can show it to the user and handle the reward upon completion. This method directly integrates FAN SDK into your app for complete control over ad loading and displaying.

Algorithm

  • Set up your project:

  • Add the Facebook Audience Network SDK to your project.

  • Initialize the SDK with your App ID.

  • Create a Rewarded Video Ad placement:

  • Log in to your Facebook Audience Network account.

  • Create a new Rewarded Video Ad placement and note down the Placement ID.

  • Load and show the rewarded video ad:

  • In your app's code, load the rewarded video ad using the Placement ID.

  • Implement listeners to handle ad events like ad loaded, ad failed to load, ad completed, etc.

  • Show the rewarded video ad when it is loaded and handle the reward given to the user upon completion.

Example

// RewardedVideoActivity.java
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.facebook.ads.*;

public class RewardedVideoActivity extends AppCompatActivity {

   private RewardedVideoAd rewardedVideoAd;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_rewarded_video);

      rewardedVideoAd = new RewardedVideoAd(this, "YOUR_PLACEMENT_ID");
      RewardedVideoAdListener adListener = new RewardedVideoAdListener() {
         @Override
         public void onError(Ad ad, AdError adError) {
            // Handle ad error
         }

         @Override
         public void onAdLoaded(Ad ad) {
            // Ad loaded, you can now show the rewarded video ad
            if (rewardedVideoAd.isAdLoaded()) {
               rewardedVideoAd.show();
            }
         }

         @Override
         public void onAdClicked(Ad ad) {
            // Handle ad clicked event
         }

         @Override
         public void onLoggingImpression(Ad ad) {
            // Handle logging impression event
         }

         @Override
         public void onRewardedVideoCompleted() {
            // Handle rewarded video completed event
         }

         @Override
         public void onRewardedVideoClosed() {
            // Handle rewarded video closed event
         }
      };

      rewardedVideoAd.loadAd(rewardedVideoAd.buildLoadAdConfig().withAdListener(adListener).build());
   }

   @Override
   protected void onDestroy() {
      rewardedVideoAd.destroy();
      super.onDestroy();
   }
}

// activity_rewarded_video.xml<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"
   android:paddingBottom="@dimen/activity_vertical_margin"
   android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   tools:context=".RewardedVideoActivity">

   <!-- Add your layout elements here -->

</RelativeLayout>

// AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.rewardedvideoapp">

   <application
      android:allowBackup="true"
      android:name=".MyApplication"
      android:icon="@mipmap/ic_launcher"
      android:label="@string/app_name"
      android:roundIcon="@mipmap/ic_launcher_round"
      android:supportsRtl="true"
      android:theme="@style/AppTheme">
      <activity
         android:name=".RewardedVideoActivity"
         android:label="@string/app_name"
         android:theme="@style/AppTheme.NoActionBar">
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
      </activity>
   </application>

   <!-- Add necessary permissions here -->

</manifest>

Output

Using Google Mobile Ads SDK Mediation

Alternatively, you can integrate FAN rewarded video ads through Google Mobile Ads SDK mediation. First, add the Google Mobile Ads SDK to your project and set up an AdMob account. Then, create an AdMob app and obtain the Ad Unit ID for rewarded video ads. Next, set up mediation in your AdMob account, adding Facebook Audience Network as a mediation network. In your app, implement AdMob's rewarded video ad loading and showing functionality, along with listeners. AdMob's mediation will automatically fetch rewarded video ads from Facebook Audience Network, along with other mediated networks, simplifying the integration process by using a single SDK for multiple ad networks.

Algorithm

  • Set up your project:

  • Add the Google Mobile Ads SDK to your project.

  • Create an AdMob account.

  • Create an AdMob app and obtain the Ad Unit ID for rewarded video ads.

  • Set up mediation:

  • Log in to your AdMob account.

  • Set up mediation for Facebook Audience Network.

  • Add Facebook Audience Network as a mediation network and provide the necessary credentials.

  • Implement AdMob mediation in your app:

  • Update your app's manifest file to include the necessary metadata for AdMob mediation.

  • Implement AdMob's rewarded video ad loading, listeners, and showing functionality.

  • AdMob's mediation will automatically load and display rewarded video ads from Facebook Audience Network, along with other mediated networks.

Example

// MainActivity.java
import android.os.Bundle;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;

import com.facebook.ads.AdSettings;
import com.facebook.ads.AdSize;
import com.facebook.ads.AdView;
import com.facebook.ads.AudienceNetworkAds;
import com.facebook.ads.RewardedVideoAd;
import com.facebook.ads.RewardedVideoAdListener;

public class MainActivity extends AppCompatActivity implements 
RewardedVideoAdListener {

   private RewardedVideoAd rewardedVideoAd;
   private Button showAdButton;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

      // Initialize Facebook Audience Network SDK
      AudienceNetworkAds.initialize(this);

      // Enable testing mode for Facebook ads
      AdSettings.setTestMode(true);

      // Load rewarded video ad
      rewardedVideoAd = new RewardedVideoAd(this, "YOUR_PLACEMENT_ID"); // Replace with your placement ID
      rewardedVideoAd.setAdListener(this);
      rewardedVideoAd.loadAd();

      // Show ad button
      showAdButton = findViewById(R.id.showAdButton);
      showAdButton.setOnClickListener(view -> {
         if (rewardedVideoAd.isAdLoaded()) {
            rewardedVideoAd.show();
         }
      });
   }

   // RewardedVideoAdListener methods
   @Override
   public void onRewardedVideoAdLoaded() {
      showAdButton.setEnabled(true);
   }

   @Override
   public void onRewardedVideoAdClicked() {
   }

   @Override
   public void onRewardedVideoAdClosed() {
      rewardedVideoAd.loadAd();
   }

   @Override
   public void onRewardedVideoAdFailedToLoad(int errorCode) {
      showAdButton.setEnabled(false);
   }

   @Override
   public void onRewardedVideoCompleted() {
   }

   @Override
   public void onLoggingImpression() {
   }
}

// 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"
   android:padding="16dp"
   tools:context=".MainActivity">

   <Button
      android:id="@+id/showAdButton"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_centerInParent="true"
      android:text="Show Ad"
      android:enabled="false" />

</RelativeLayout>

Output

Conclusion

In this tutorial, integrating Facebook Audience Network (FAN) rewarded video ads in your Android app can significantly enhance your monetization efforts while providing a more engaging and rewarding experience for your users. Whether you choose to directly integrate FAN's SDK or leverage Google Mobile Ads SDK mediation, FAN offers a powerful advertising platform with extensive reach and personalized targeting capabilities. By incorporating FAN rewarded video ads, you can unlock new revenue streams and optimize user engagement, ultimately leading to the success and sustainability of your app.

Updated on: 27-Jul-2023

141 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements