Search code examples
androidfacebook-audience-network

Facebook Audience Network setAdListner for native ads has been deprecated what is the newer way to do that?


I am trying to implement Facebook native ads in my application. In their documentation, they have mentioned setAdListner but in Android Studio I got a warning that setAddListner has been deprecated. So I wanna know what is the newer way to do that? Because in their documentation it is not mentioned.


Solution

  • com.facebook.ads.InterstitialAd facebookFullscren
    

    set Facebook Ad listner :

     facebookFullscren = new com.facebook.ads.InterstitialAd(activity,"YOUR_PLACEMENT_ID");
            InterstitialAdListener adListener = new InterstitialAdListener() {
                @Override
                public void onInterstitialDisplayed(Ad ad) {
                    Log.d(TAG, "onInterstitialDisplayed: ");
                }
    
                @Override
                public void onInterstitialDismissed(Ad ad) {
                    Log.e(TAG, "facebook Interstitial ad dismissed.");
                }
    
                @Override
                public void onError(Ad ad, AdError adError) {
                    Log.e(TAG, "facebook Interstitial ad failed to load: " + adError.getErrorMessage());
    
                }
    
                @Override
                public void onAdLoaded(Ad ad) {
                    Log.d(TAG, "onAdLoaded: ");
                }
    
                @Override
                public void onAdClicked(Ad ad) {
                    Log.d(TAG, "onAdClicked: ");
                }
    
                @Override
                public void onLoggingImpression(Ad ad) {
                    Log.d(TAG, "onLoggingImpression: ");
                }
            };
    

    load Ad:

                facebookFullscren.loadAd(facebookFullscren.buildLoadAdConfig()
                    .withAdListener(adListener)
                    .build());
    

    show Ad:

    if (facebookFullscren != null && facebookFullscren.isAdLoaded()) {
                        facebookFullscren.show();
                    }