ShiVa Developer Forum • View topic - AdMob Rewarded Video
 

AdMob Rewarded Video

Google Play Store, Amazon Kindle, Nook, and so many more

AdMob Rewarded Video

Postby Uralbeat » 2019-03-21 11:12

Hello. This is a quick guide how to implement Admob Rewarded videos in Android games using hooks and callbacks. I did it with the latest version of Shiva 2.0 beta and Android Studio 3.3.2. Yes, the code is not so clean, but it works.

The following pictures show how the minimal project in Shiva looks like.
Image
Image

AIModel contains one string variable and three handlers:
Image
Image
Image
Image
Image

In HUD there is button with 'Click action':
Image
Image

That's all. Select Android target in the Authoring module and build it as 'Android Studio project (ZIP)'.
Unzip the archive and 'Import project (Gradle, Eclipse ADT, etc.)' in Android Studio.

Next we will use test links for development build (for distribution use your own):
ca-app-pub-3940256099942544~3347511713 - TEST Admob App ID
ca-app-pub-3940256099942544/5224354917 - TEST Admob Ad ID

Files to edit: build.gradle(perform a Gradle sync), AndroidManifest.xml, S3DClient.cpp and S3DEngine.java.

You need to add the text highlighted in yellow and substitute your own values instead of the text highlighted in red.

build.gradle:
...
dependencies {
implementation fileTree(dir: 'src/main/libs', include: ['*.jar'])
implementation 'com.google.android.gms:play-services-ads:17.2.0'
}
...

AndroidManifest.xml:
...
<manifest ... >
...
<application>
...
<meta-data android:name="com.google.android.gms.ads.APPLICATION_ID" android:value="ca-app-pub-3940256099942544~3347511713"/>
</application>
...
</manifest>

S3DClient.cpp:
...
//----------------------------------------------------------------------
// @@BEGIN_JNI_INSTALL_EVENT_HOOKS@@
//----------------------------------------------------------------------
S3DClient_InstallCurrentUserEventHook ( "Main", "onAdMobSend", onAdMobSendCallback, NULL );
//----------------------------------------------------------------------
// @@END_JNI_INSTALL_EVENT_HOOKS@@
//----------------------------------------------------------------------
...
//----------------------------------------------------------------------
// @@BEGIN_JNI_CALLBACKS@@
//----------------------------------------------------------------------
void onAdMobSendCallback ( unsigned char _iArgumentCount, const void *_pArguments, void *_pUserData ) {
if ( pJavaVM ) {
JNIEnv *pEnv ;
if ( pJavaVM->GetEnv ( (void**) &pEnv, JNI_VERSION_1_4 ) >= 0 ) {
jclass pClass = pEnv->FindClass ( "
com/mycompany/myproduct/S3DEngine" );
if ( pClass != 0 ) {
jmethodID pMethod = pEnv->GetStaticMethodID ( pClass, "CPPtoJAVAshowAdd", "()I");
if ( pMethod ) {
int nNumber = pEnv->CallStaticIntMethod ( pClass, pMethod );
}
}
}
}
}

...
//----------------------------------------------------------------------
// @@END_JNI_CALLBACKS@@
//----------------------------------------------------------------------
JNIEXPORT jstring JNICALL Java_com_mycompany_myproduct_S3DEngine_getNativeString(JNIEnv *env, jobject obj, jstring aa, jstring bb) {
const char *cc = env->GetStringUTFChars( aa , NULL );
const char *dd = env->GetStringUTFChars( bb , NULL );
S3DX::AIVariable variables[2];
variables[0] = cc;
variables[1] = dd;
S3DClient_SendEventToCurrentUser ( "Main", "onAdMobReceive", 2, &variables );
return env->NewStringUTF("OK");
}

...

S3DEngine.java:

...
import android.os.CountDownTimer;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.reward.RewardedVideoAd;
import com.google.android.gms.ads.reward.RewardItem;
import com.google.android.gms.ads.reward.RewardedVideoAdListener;

//----------------------------------------------------------------------
public class S3DEngine extends Application implements RewardedVideoAdListener
{
private RewardedVideoAd mRewardedVideoAd;
private static boolean bShowAd = false;
private static int nLoadAd = 0;
private native String getNativeString(String aa, String bb);

//------------------------------------------------------------------
// Application overrides
//
@Override
public void onCreate ( )
{
// Register it to the S3DX Android tools
//
S3DXAndroidTools.setMainApplication ( this ) ;
// Call parent constructor
//
super.onCreate ( ) ;
MobileAds.initialize(this, "ca-app-pub-3940256099942544~3347511713");
mRewardedVideoAd = MobileAds.getRewardedVideoAdInstance(this);
mRewardedVideoAd.setRewardedVideoAdListener(this);
loadRewardedVideoAd();
new CountDownTimer(360000000, 1000) {
public void onTick(long millisUntilFinished) {
if (mRewardedVideoAd.isLoaded()) {
getNativeString("sAdMobRewardedVideoStatus", "loaded");
} else {
getNativeString("sAdMobRewardedVideoStatus", "notloaded");
nLoadAd++;
if (nLoadAd > 10) {
loadRewardedVideoAd();
}
}
if (bShowAd) {
bShowAd = false;
showRewardedVideoAd();
}
}
public void onFinish() {
}
}.start();
}
public static int CPPtoJAVAshowAdd( ) {
bShowAd = true;
return 0;
}
private void loadRewardedVideoAd() {
nLoadAd = 0;
mRewardedVideoAd.loadAd("
ca-app-pub-3940256099942544/5224354917",
new AdRequest.Builder().build());
}
public void showRewardedVideoAd() {
if (mRewardedVideoAd.isLoaded()) {
nLoadAd = -30;
mRewardedVideoAd.show();
}
}
@Override
public void onRewarded(RewardItem reward) {
getNativeString("sAdMobReward", "" + reward.getType() + "###" + reward.getAmount());
}
@Override
public void onRewardedVideoAdLeftApplication() {
}
@Override
public void onRewardedVideoAdClosed() {
loadRewardedVideoAd();
}
@Override
public void onRewardedVideoAdFailedToLoad(int errorCode) {
}
@Override
public void onRewardedVideoAdLoaded() {
getNativeString("sAdMobRewardedVideoStatus", "loaded");
}
@Override
public void onRewardedVideoAdOpened() {
}
@Override
public void onRewardedVideoStarted() {
}
@Override
public void onRewardedVideoCompleted() {
}

//------------------------------------------------------------------
@Override
public void onTerminate ( )
{
//Log.d ( "S3DENGINE MAIN", "### onTerminate" ) ;
super.onTerminate ( ) ;
}
//------------------------------------------------------------------
@Override
public void onConfigurationChanged ( Configuration newConfig )
{
//Log.d ( "S3DENGINE MAIN", "### onConfigurationChanged : " + newConfig.orientation ) ;
//super.onConfigurationChanged ( newConfig ) ;
}
}


Finally, build APK.

Small notes:
- video will be shown when you click on the button (if the video is loaded)
- when the advertisement is uploaded you will see it (the text on the button will change)
- after successful advertising, you should reward the user with 'sRewardType' and 'sRewardAmount' (which you specified in your AdMob account)
User avatar
Uralbeat
Junior Boarder
Junior Boarder
 
Posts: 39
Joined: 2012-07-04 14:07
Location: Russia

Re: AdMob Rewarded Video

Postby broozar » 2019-03-27 23:20

thank you very much! I will add your tutorial to the KnowledgeBase soon.
User avatar
broozar
Administrator
Administrator
 
Posts: 4172
Joined: 2007-09-16 10:22
Location: Berlin - Germany

Re: AdMob Rewarded Video

Postby broozar » 2019-04-09 12:05

The tutorial is now live on the KnowledgeBase: http://www.shiva-engine.com/kbe_knowled ... ed-videos/ thanks @Uralbeat!
User avatar
broozar
Administrator
Administrator
 
Posts: 4172
Joined: 2007-09-16 10:22
Location: Berlin - Germany


Return to Google Android

Who is online

Users browsing this forum: No registered users and 2 guests

cron