Ganesh P
Ganesh P

Reputation: 196

how to get facebook NativeAdsManager ad click listener in android

i using facebook ad network for displaying native ads its working fine. So how to implement the event listener for loaded, clicked, error. So i just stuck on that implementation so can any one help me how to do.

listNativeAdsManager = new NativeAdsManager(activity, "mykey", 5)
        listNativeAdsManager.setListener(new NativeAdsManager.Listener() {
            @Override
            public void onAdsLoaded() {
                com.facebook.ads.NativeAd fbNative = listNativeAdsManager.nextNativeAd();
                fbNative.setAdListener(new AdListener() {
                    @Override
                    public void onError(Ad ad, AdError adError) {
                        Log.d("fb-native","onError ");
                    }

                    @Override
                    public void onAdLoaded(Ad ad) {
                        Log.d("fb-native","onAdLoaded ");
                    }

                    @Override
                    public void onAdClicked(Ad ad) {
                        Log.d("fb-native","onAdClicked ");
                    }
                });
                View adView = NativeAdView.render(getActivity(), fbNative, NativeAdView.Type.HEIGHT_100);
                html_view.addView(adView);
            }

            @Override
            public void onAdError(AdError adError) {

            }
        });

this is how i implemented but i didn't get any call back from onAdLoaded, onAdClicked so what i have to change

Upvotes: 0

Views: 1806

Answers (3)

Pankaj Talaviya
Pankaj Talaviya

Reputation: 3458

NativeAdsManager listNativeAdsManager = new NativeAdsManager(activity, "mykey", 5);

please not send 5 in parameter of NativeAdsManager 5 is a request number of request send in server at a time so send only 1 in braces like

NativeAdsManager listNativeAdsManager = new NativeAdsManager(activity, "mykey", 1);

this is worked for me...

Upvotes: 0

marmor
marmor

Reputation: 28179

Here's a possible workaround:

Here's a custom FrameLayout that detects clicks on it:

public class AdContainer extends FrameLayout implements OnGestureListener {

    GestureDetector clickDetector;
    private NativeAd ad;
    private AdListener listener;

    public AdContainer(@NonNull Context context) {
        super(context);
        init();
    }

    public AdContainer(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public AdContainer(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    @TargetApi(VERSION_CODES.LOLLIPOP)
    public AdContainer(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr, @StyleRes int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    private void init() {
        clickDetector = new GestureDetector(getContext(), this);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        clickDetector.onTouchEvent(ev);
        return super.onInterceptTouchEvent(ev);
    }

    public void setAd(NativeAd ad, AdListener listener) {
        this.ad = ad;
        this.listener = listener;
    }

    // OnGestureListener

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        Log.d("AdContainer", "detected a click in an ad container: " + ad);
        if ((ad != null) && (listener != null)) {
            listener.onAdClicked(ad);
        }
        return false;
    }

    @Override
    public boolean onDown(MotionEvent e) {
        return false;
    }

    @Override
    public void onShowPress(MotionEvent e) {
    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        return false;
    }

    @Override
    public void onLongPress(MotionEvent e) {
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        return false;
    }
}

Use it like this:
(1) Inflate your ad layout into the new container class:

<com.example.AdContainer
    android:id="@+id/ad_container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

(2) When binding your ad to the layout, register it with the AdContainer:

AdContainer container = (ViewGroup) findViewById(R.id.ad_container);
container.setAd(ad, this); // make sure the current class implements AdListener

Upvotes: 1

Emanuel Moecklin
Emanuel Moecklin

Reputation: 28856

I'm running into the same issues here. As a matter of fact none of the methods is called ever and this is a bug in the Audience SDK! If you revert back to 4.11.0 you'll get the onAdClicked at least but all versions higher than 4.11.0 won't call any of the callback methods. There's this bug report open (which a co-worker of mine has filed): https://developers.facebook.com/bugs/158853171214759/.

The solution is simple: don't use the NativeAdsManager but load the ads directly:

    final NativeAd nativeAd = new NativeAd(context, facebookNativeAdUnitId);

    nativeAd.setAdListener(new AdListener() {
        @Override
        public void onError(Ad ad, AdError adError) {
        }

        @Override
        public void onAdLoaded(Ad ad) {
        }

        @Override
        public void onAdClicked(Ad ad) {
        }
    });

    nativeAd.loadAd(NativeAd.MediaCacheFlag.ALL);

Upvotes: 2

Related Questions