Tutorial For Admob Rewarded Video Ads(custom) - Android

Discussion in 'Advertising' started by Forgive, Nov 10, 2019.

  1. Forgive

    Forgive Boxer

    Joined:
    Apr 16, 2019
    Messages:
    57
    Likes Received:
    4
    Hi everybody,

    I worked hard to solve this problem, I researched all of possibilities. Heyzap, Appodeal, Mopub, etc. I encountered some problems with these mediation services. Besides tutorials are out-dated, I could not trust them.

    I was wondering how can I implement Admob rewarded video with custom option. Most people claimed that it is not working in Android(custom rewarded videos) In fact it is working in Android without any problem. I prepared an easy tutorial for you.

    After adding purchase button in UI. Do settings like in the image.

    [​IMG]

    Also you can declare action in rewarded video. It is not subject of this post. You can research on the internet to declare it as action(you can reward user by coins, etc.)

    Other thing is that, when you choose Ad network custom, it does not work in preview. Don't care! It will work on Android OS.

    That's it in Buildbox side! Let's go to code..

    It is really easy!

    Go to app/src/main/java/com/buildbox/AdIntegrator.java file. Remove all code - copy & paste the code below.

    Code:
    package com.buildbox;
    import android.util.Log;
    import android.view.Gravity;
    import android.view.View;
    import android.widget.FrameLayout;
    import android.widget.RelativeLayout;
    import android.widget.Toast;
    
    
    import org.cocos2dx.lib.Cocos2dxActivity;
    import java.lang.ref.WeakReference;
    import com.google.android.gms.ads.AdRequest;
    import com.google.android.gms.ads.AdSize;
    import com.google.android.gms.ads.AdView;
    import com.google.android.gms.ads.InterstitialAd;
    import com.google.android.gms.ads.AdListener;
    import com.google.android.gms.ads.reward.RewardedVideoAd;
    import com.google.android.gms.ads.MobileAds;
    import com.google.android.gms.ads.reward.RewardedVideoAdListener;
    import com.google.android.gms.ads.reward.RewardItem;
    
    public class AdIntegrator {
        public static native boolean rewardedVideoDidEnd();
        public static RewardedVideoAd mRewardedVideoAd;
    
        public static String appId = “testads”; //put your Admob app id here.
        public static String rewardedAdUnitId ="ca-app-pub-3940256099942544/5224354917";  //Test ad unit ID. put your rewarded ad unit id here.
    
    
        private static WeakReference<Cocos2dxActivity> activity;
        public static void initBridge(Cocos2dxActivity act){
            activity = new WeakReference<Cocos2dxActivity>(act);
        }
        public static void initAds(){
            activity.get().runOnUiThread( new Runnable() {
                public void run() {
                    //Admob AppID
                    MobileAds.initialize(activity.get(),appId);
                    //rewarded
                    mRewardedVideoAd = MobileAds.getRewardedVideoAdInstance(activity.get());
                    mRewardedVideoAd.loadAd(rewardedAdUnitId, new AdRequest.Builder().build());
                    mRewardedVideoAd.setRewardedVideoAdListener(new RewardedVideoAdListener() {
                                                                    @Override
                                                                    public void onRewardedVideoAdClosed () {
                                                                        mRewardedVideoAd.loadAd(rewardedAdUnitId, new AdRequest.Builder().build());
                                                                    }
                                                                    @Override
                                                                    public void onRewardedVideoAdOpened() {
                                                                    }
                                                                    @Override
                                                                    public void onRewardedVideoStarted() {
    
                                                                    }
                                                                    @Override
                                                                    public void onRewardedVideoCompleted() {
    
                                                                    }
                                                                    @Override
                                                                    public void onRewardedVideoAdLeftApplication() {
    
                                                                    }
                                                                    @Override
                                                                    public void onRewarded(RewardItem reward) {
                                                                        AdIntegrator.rewardedVideoDidEnd();
                                                                        Toast.makeText(activity.get(), “Rewarded!”, Toast.LENGTH_SHORT).show();
                                                                    }
                                                                    @Override
                                                                    public void onRewardedVideoAdFailedToLoad(int errorCode) {
                                                                        Toast.makeText(activity.get(), “No fill from Admob!“, Toast.LENGTH_SHORT).show();
    
                                                                    }
                                                                    @Override
                                                                    public void onRewardedVideoAdLoaded() {
    
                                                                    }
                                                                }
                    );
                }
            });
    
        }
    
        public static boolean isRewardedVideoAvialable(){
            return true;
        }
        public static void showRewardedVideo(){
            activity.get().runOnUiThread( new Runnable() {
                public void run() {
                    if (mRewardedVideoAd.isLoaded()) {
                        mRewardedVideoAd.show();
                    }
                }
            });
        }
    
    }
    
    
    After that, your rewarded videos should work without any problem. This code uses Admob test ads IDs so you can test it directly without any modification. If test ads working without any problem,
    modify appId and rewardedAdUnitId variables.

    Congratulations! You have successfully implemented rewarded video for Android!

    Note that: Admob sometimes, can not fill requests for test ads. It is admob's problem. When no fill from server you can see toast message in smartphone. I declared it in the code.

    Tested in Buildbox 2.3.8 #2515

    I am researching to add rewarded Admob video for iOS, after if I find a solution, I will prepare another tutorial for iOS. If you have any tutorial, link, youtube video for iOS Admob Rewarded videos, please mention it. I can benefit from it.
     
  2. Kevin Nguyen

    Kevin Nguyen Boxer

    Joined:
    Sep 27, 2018
    Messages:
    12
    Likes Received:
    8
    Great tutorial!
    I will try and included this into my latest game!

    Thanks
     
  3. blue soda

    blue soda Boxer

    Joined:
    Sep 14, 2019
    Messages:
    5
    Likes Received:
    1
    still, reward video to unlock character not working
     
  4. Forgive

    Forgive Boxer

    Joined:
    Apr 16, 2019
    Messages:
    57
    Likes Received:
    4
    It should work, in my Android game no problem it trigger reward.(my reward is coin)
     
  5. belmir

    belmir Boxer

    Joined:
    Jun 29, 2018
    Messages:
    75
    Likes Received:
    20
    hi forgive
    I did exactly as you told us
    the video works but the reward does not work at the end of the video
    bb 2.3.8 last beta
     
  6. Forgive

    Forgive Boxer

    Joined:
    Apr 16, 2019
    Messages:
    57
    Likes Received:
    4
    It is working in BB 2.3.8 2515. Reward action should be implemented correctly.
     

Share This Page