Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RewardedVideoAd callback listener mismatch

I have implemented RewardedVideoAd for use of PRO features.

When I am using RewardedVideoAd 1 time(in a single screen) in the whole app then it works perfectly. but when I use multiple times (in multiple screens) then RewardedVideoAd callback listener is a mismatch between both screens.

let me explain the whole scenario

  1. I have open SCREEN-A then I have watched RewardedVideoAd. after successfully completing this video I got reward.
  2. now I am going to next SCREEN-B.
  3. then, I am going to next SCREEN-C with SCREEN-B finish.
  4. so, now app status is in foreground state SCREEN-C & in background state SCREEN-A.
  5. now I am watching RewardedVideoAd in SCREEN-C. after successfully completing this video I got reward.
  6. then I have closed SCREEN-C. so, I redirect to SCREEN-A which is in the background state.
  7. now I am watching RewardedVideoAd in SCREEN-A. After successfully completing this video. I did not get a reward.

so the issue is that last time I have watched a video in SCREEN-C. so all the callbacks are going to SCREEN-C, not in SCREEN-A.

I have also implemented the below code in all screens.

@Override
public void onResume() {
    mRewardedVideoAd.resume(this);
    super.onResume();
}

@Override
public void onPause() {
    mRewardedVideoAd.pause(this);
    super.onPause();
}

@Override
public void onDestroy() {
    mRewardedVideoAd.destroy(this);
    super.onDestroy();
}
like image 706
VIISHRUT MAVANII Avatar asked Oct 02 '19 12:10

VIISHRUT MAVANII


2 Answers

I have made one common module for showing RewardedVideoAd.you can try this. I think it may solve your problem.

put below the class in your project.

AdRewardedHandler.java

public class AdRewardedHandler {

    private static final String TAG = "AdRewardedHandler";

    private Context context;
    private static AdRewardedHandler adRewardedHandler;

    private RewardedVideoAd rewardedVideoAd;
    private RewardedAdListener rewardedAdListener;

    private boolean isFailedToLoad = false;


    public static AdRewardedHandler getInstance() {
        if (adRewardedHandler == null) {
            adRewardedHandler = new AdRewardedHandler();
        }
        return adRewardedHandler;
    }

    public void initializeRewardedHandler(Context context) {
        this.context = context;
        rewardedVideoAd = MobileAds.getRewardedVideoAdInstance(context);
        loadRewardedVideoAd();
    }

    public void setAdHandlerListener(RewardedAdListener rewardedAdListener1) {
        this.rewardedAdListener = rewardedAdListener1;
    }

    public boolean isAdLoaded() {
        if (rewardedVideoAd != null) {
            return rewardedVideoAd.isLoaded();
        } else {
            return false;
        }
    }

    public void loadRewardedVideoAd() { 

        rewardedVideoAd.loadAd("ca-app-pub-3940256099942544/5224354917",new AdRequest.Builder().build());
        rewardedVideoAd.setRewardedVideoAdListener(new RewardedVideoAdListener() {

            @Override
            public void onRewardedVideoAdLoaded() {
                Log.i(TAG, "onRewardedVideoAdLoaded: ");
                if (rewardedAdListener != null) {
                    rewardedAdListener.onRewardedVideoAdLoaded();
                }

            }

            @Override
            public void onRewardedVideoAdOpened() {
                Log.i(TAG, "onRewardedVideoAdOpened: ");

                if (rewardedAdListener != null) {
                    rewardedAdListener.onRewardedVideoAdOpened();
                }
            }

            @Override
            public void onRewardedVideoStarted() {
                Log.i(TAG, "onRewardedVideoStarted: ");
                if (rewardedAdListener != null) {
                    rewardedAdListener.onRewardedVideoStarted();

                }
            }

            @Override
            public void onRewardedVideoAdClosed() {
                Log.i(TAG, "onRewardedVideoAdClosed: ");
                if (rewardedAdListener != null) {
                    rewardedAdListener.onRewardedVideoAdClosed();
                    if (!isAdLoaded()) {
                        loadRewardedVideoAd();
                    }
                }
            }

            @Override
            public void onRewarded(RewardItem var1) {
                Log.i(TAG, "onRewarded: " + var1);
                if (rewardedAdListener != null) {
                    rewardedAdListener.onRewarded(var1);
                }
            }

            @Override
            public void onRewardedVideoAdLeftApplication() {
                Log.i(TAG, "onRewardedVideoAdLeftApplication: ");
                if (rewardedAdListener != null) {
                    rewardedAdListener.onRewardedVideoAdLeftApplication();
                }
            }

            @Override
            public void onRewardedVideoAdFailedToLoad(int var1) {
                Log.i(TAG, "onRewardedVideoAdFailedToLoad: " + var1);
                if (!isFailedToLoad) {
                    isFailedToLoad = true;
                    if (!isAdLoaded()) {
                        loadRewardedVideoAd();
                    }
                }

                if (rewardedAdListener != null) {
                    rewardedAdListener.onRewardedVideoAdFailedToLoad(var1);

                }


            }

            @Override
            public void onRewardedVideoCompleted() {
                Log.i(TAG, "onRewardedVideoCompleted: ");
                if (rewardedAdListener != null) {
                    rewardedAdListener.onRewardedVideoCompleted();
                    if (!isAdLoaded()) {
                        loadRewardedVideoAd();
                    }
                }

            }
        });


    }


    public void showRewardedAd(RewardedAdListener rewardedAdListener1) {

        setAdHandlerListener(rewardedAdListener1);

        Log.i(TAG, "showRewardedAd: ");

        try {
            if (rewardedVideoAd != null && rewardedVideoAd.isLoaded()) {
                rewardedVideoAd.show();
            }
        } catch (Throwable th) {
            th.printStackTrace();
        } 

    }

    public void onResume() {
        Log.i(TAG, "onResume: Resume Call :");
        if (rewardedVideoAd != null) {
            rewardedVideoAd.resume(context);
        }
    }

    public void onPause() {
        Log.i(TAG, "onPause: Pause Call : ");
        if (rewardedVideoAd != null) {
            rewardedVideoAd.pause(context);
        }
    }

    public void onDestroy() {
        Log.i(TAG, "onDestroy: Destroy Call :");
    }


    public interface RewardedAdListener {

        void onRewardedVideoAdLoaded();

        void onRewardedVideoAdOpened();

        void onRewardedVideoStarted();

        void onRewardedVideoAdClosed();

        void onRewarded(RewardItem var1);

        void onRewardedVideoAdLeftApplication();

        void onRewardedVideoAdFailedToLoad(int var1);

        void onRewardedVideoCompleted();
    }
}

Put below line in your application file.

AdRewardedHandler.getInstance().initializeRewardedHandler(this);

Now, finally How to use

public class YourActivity extends AppCompatActivity implements AdRewardedHandler.RewardedAdListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        AdRewardedHandler.getInstance().setAdHandlerListener(this);

        if (!AdRewardedHandler.getInstance().isAdLoaded()) {
            AdRewardedHandler.getInstance().loadRewardedVideoAd();
        }

    }

    public void showRewardedVideoAd() {
        // showRewardedAd                            
        AdRewardedHandler.getInstance().showRewardedAd(this);
    }

    @Override
    public void onResume() {
        AdRewardedHandler.getInstance().onResume();
        super.onResume();
    }


    @Override
    public void onPause() {
        AdRewardedHandler.getInstance().onPause();
        super.onPause();
    }

    @Override
    public void onRewardedVideoAdLoaded() {
        Log.i(TAG, "onRewardedVideoAdLoaded: ");
    }

    @Override
    public void onRewardedVideoAdOpened() {
        Log.i(TAG, "onRewardedVideoAdOpened: ");
    }

    @Override
    public void onRewardedVideoStarted() {
        Log.i(TAG, "onRewardedVideoStarted: ");
    }

    @Override
    public void onRewardedVideoAdClosed() {
        Log.i(TAG, "onRewardedVideoAdClosed: ");
    }

    @Override
    public void onRewardedVideoAdLeftApplication() {
        Log.i(TAG, "onRewardedVideoAdLeftApplication: ");
    }

    @Override
    public void onRewardedVideoAdFailedToLoad(int var1) {
        Log.i(TAG, "onRewardedVideoAdFailedToLoad: " + var1);
    }

    @Override
    public void onRewardedVideoCompleted() {
        Log.i(TAG, "onRewardedVideoCompleted: ");
    }

}
like image 151
S_i_l_e_n_t C_o_d_e_r Avatar answered Oct 17 '22 05:10

S_i_l_e_n_t C_o_d_e_r


I was facing the same problem. In my case, When I am going to Activity A to B so, my Activity A in background and Activity B is on foreground, now when i'm back to Activity A that time I don't get callback of RewardedVideoAds.

I was solved this problem in simple way, when i'm back to Activity A that time I set the listener in OnResume Method.

 @Override
 public void onResume() {
    super.onResume();
    Log.i(TAG, "onResume Call.");

    if (rewardedVideoAd != null) {
        rewardedVideoAd.setRewardedVideoAdListener(this);
        rewardedVideoAd.resume(activity);
    }
}

Note that for this solution you have to remove this two line from your Activity B.

@Override
public void onDestroy() {
    super.onDestroy();
    Log.e(TAG, "onDestroy: ");
    rewardedVideoAd.destroy(activity);
    rewardedVideoAd.setRewardedVideoAdListener(null);
}
like image 43
mihir ahir Avatar answered Oct 17 '22 06:10

mihir ahir