In this article we will learn about the Facebook interstitials integrate in our android studio app. So first of all we need an account for app id and unit id.

  1. Create Facebook Developer account.
  2. Get app id and unit id of ad.
  3. Add internet permission in manifest file.
  4. Add meta data in manifest file.
  5. Paste Code of interstitial ad and remember the test code.
  6. Build Your app and enjoy.

Manifest.xml

Permission

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

Metadata

In meta data add your facebook app id. Without app id ads not shown in your app.

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

Build.Gradule

Repository 

mavenCentral()

Dependency

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

 

Java Code

private InterstitialAd interstitialAd;
interstitialAd = new InterstitialAd(this, "YOUR_PLACEMENT_ID");
interstitialAd.loadAd();

private void showAds() {

    if (interstitialAd == null || !interstitialAd.isAdLoaded()) {
        return;
    }

    if (interstitialAd.isAdInvalidated()) {
        return;
    }
    // Show the ad
    interstitialAd.show();
}
@Override
protected void onDestroy() {
    if (interstitialAd != null) {
        interstitialAd.destroy();
    }
    super.onDestroy();
}

Note!! For Testing on Emulator Add this code and remove this code when publish on play console.

// EMulator testing remove this 
  if (BuildConfig.DEBUG) {
            AdSettings.setTestMode(true);
        }

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *