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:
    58
    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:
    23
    Likes Received:
    14
    Great tutorial!
    I will try and included this into my latest game!

    Thanks
     
  3. blue soda

    blue soda Boxer

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

    Forgive Boxer

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

    belmir Avid Boxer

    Joined:
    Jun 29, 2018
    Messages:
    129
    Likes Received:
    45
    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:
    58
    Likes Received:
    4
    It is working in BB 2.3.8 2515. Reward action should be implemented correctly.
     
  7. Kevin Nguyen

    Kevin Nguyen Boxer

    Joined:
    Sep 27, 2018
    Messages:
    23
    Likes Received:
    14
    belmir, TheGameAppStudio and Forgive like this.
  8. belmir

    belmir Avid Boxer

    Joined:
    Jun 29, 2018
    Messages:
    129
    Likes Received:
    45
    hi keven
    Thanks for your help,
    I can not open mac os files on notepad is normal? how to comfigure the file
    what does this function with the latest beta bb 2.3.8 ?
    thank you
     
    Last edited: Dec 6, 2019
  9. Kevin Nguyen

    Kevin Nguyen Boxer

    Joined:
    Sep 27, 2018
    Messages:
    23
    Likes Received:
    14
    try the app Brackets to open and edit the file:
    http://brackets.io
     
  10. LitjiT

    LitjiT Boxer

    Joined:
    Oct 16, 2019
    Messages:
    5
    Likes Received:
    2
    Hi Forgive, Reward_error.PNG

    I have some error after paste the code like this
    How to fix this?

    Thanks
     
  11. J.E.Moores

    J.E.Moores Boxer

    Joined:
    Feb 5, 2019
    Messages:
    16
    Likes Received:
    4
    I get the exact same '/u201c' error... I'm using Android Studio on PC, copy/pasting from notepad with BuildBox 2.3.8 B#2520 any idea what we're doing wrong? It's gotta be something simple that I'm missing... Reward Videos would change my life...
     
  12. belmir

    belmir Avid Boxer

    Joined:
    Jun 29, 2018
    Messages:
    129
    Likes Received:
    45
    @jeMoores
    changed symbole '»' by ''
     
  13. J.E.Moores

    J.E.Moores Boxer

    Joined:
    Feb 5, 2019
    Messages:
    16
    Likes Received:
    4
    oooooh! That's sexy. Thank you. I knew it was something like that. I'm an artist not a coder, so I have lots of new challenges ahead. I appreciate your response. Hoping I can make it work. Heckers, I don't even know how to get my avatar on this forum... tells me I don't have access! LOL Thanks again, I'll wrangle this thing yet!
     
    belmir likes this.
  14. LitjiT

    LitjiT Boxer

    Joined:
    Oct 16, 2019
    Messages:
    5
    Likes Received:
    2
    It works, thanks @belmir
     
    belmir likes this.
  15. LitjiT

    LitjiT Boxer

    Joined:
    Oct 16, 2019
    Messages:
    5
    Likes Received:
    2
    Yeah man,
    I found the problem in line 26, 65 and 69, you just changed symbol '»' by '' in there. I already tried and it works!

    Cheers!
     
    belmir likes this.
  16. youcef

    youcef Boxer

    Joined:
    Jul 6, 2018
    Messages:
    8
    Likes Received:
    3
    The game crashes in the end of the video.. any solution !!
     
  17. Colin MH

    Colin MH Boxer

    Joined:
    Nov 15, 2018
    Messages:
    26
    Likes Received:
    17
    Hi, can you help? Where can we find the » symbol?
     
  18. Madhav Bansal

    Madhav Bansal Boxer

    Joined:
    Apr 18, 2020
    Messages:
    2
    Likes Received:
    0
    Add a delay node of delay=1 between rewarded video node to the node to load checkpoint, then the app will not crash.
     

Share This Page