Skip to content

Commit 8ade83d

Browse files
authored
Added optional userId and customData support for rewarded AD (#51)
* Added optional custom data support for rewarded AD * add: userId support * add: support for ios * make second parameter options table optional in `load_rewarded` * update script_api for `load_rewarded`
1 parent 31d5d4a commit 8ade83d

File tree

7 files changed

+74
-8
lines changed

7 files changed

+74
-8
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ builtins
1111
/.idea/
1212
/debug.keystore
1313
/debug.keystore.pass.txt
14-
/.editor_settings
14+
/.editor_settings
15+
/.vscode

extension-admob/api/admob.script_api

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,22 @@
306306
Original docs
307307
[Android](https://developers.google.com/admob/android/rewarded-fullscreen),
308308
[iOS](https://developers.google.com/admob/ios/rewarded-ads)
309+
- name: options
310+
type: table
311+
optional: true
312+
desc: ServerSideVerificationOptions
313+
[Android](https://developers.google.com/admob/android/rewarded#validate-ssv),
314+
[iOS](https://developers.google.com/admob/ios/rewarded#validate-ssv)
315+
fields:
316+
- name: user_id
317+
type: string
318+
optional: true
319+
desc: A unique identifier assigned to each user.
320+
- name: custom_data
321+
type: string
322+
optional: true
323+
desc: Custom Data attached to server-side reward callbacks.
324+
309325

310326
#*****************************************************************************************************
311327

extension-admob/src/admob.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,24 @@ static int Lua_LoadRewarded(lua_State* L)
7272
return DM_LUA_ERROR("Expected string, got %s. Wrong type for Rewarded UnitId variable '%s'.", luaL_typename(L, 1), lua_tostring(L, 1));
7373
}
7474
const char* unitId_lua = luaL_checkstring(L, 1);
75-
LoadRewarded(unitId_lua);
75+
76+
char* userId_lua = 0;
77+
char* customData_lua = 0;
78+
if (lua_istable(L, 2))
79+
{
80+
lua_getfield(L, 2, "user_id");
81+
if (lua_isstring(L, -1)) {
82+
userId_lua = (char*)luaL_checkstring(L, -1);
83+
}
84+
lua_pop(L, 1);
85+
86+
lua_getfield(L, 2, "custom_data");
87+
if (lua_isstring(L, -1)) {
88+
customData_lua = (char*)luaL_checkstring(L, -1);
89+
}
90+
lua_pop(L, 1);
91+
}
92+
LoadRewarded(unitId_lua, userId_lua, customData_lua);
7693
return 0;
7794
}
7895

extension-admob/src/admob_android.cpp

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,20 @@ static void CallVoidMethodCharBoolean(jobject instance, jmethodID method, const
9393
env->DeleteLocalRef(jstr);
9494
}
9595

96+
static void CallVoidMethodCharCharChar(jobject instance, jmethodID method, const char* cstr, const char* cstr2, const char* cstr3)
97+
{
98+
dmAndroid::ThreadAttacher threadAttacher;
99+
JNIEnv* env = threadAttacher.GetEnv();
100+
101+
jstring jstr = env->NewStringUTF(cstr);
102+
jstring jstr2 = env->NewStringUTF(cstr2);
103+
jstring jstr3 = env->NewStringUTF(cstr3);
104+
env->CallVoidMethod(instance, method, jstr, jstr2, jstr3);
105+
env->DeleteLocalRef(jstr);
106+
env->DeleteLocalRef(jstr2);
107+
env->DeleteLocalRef(jstr3);
108+
}
109+
96110
static void CallVoidMethodInt(jobject instance, jmethodID method, int cint)
97111
{
98112
dmAndroid::ThreadAttacher threadAttacher;
@@ -116,7 +130,7 @@ static void InitJNIMethods(JNIEnv* env, jclass cls)
116130
g_admob.m_ShowAppOpen = env->GetMethodID(cls, "showAppOpen", "()V");
117131
g_admob.m_LoadInterstitial = env->GetMethodID(cls, "loadInterstitial", "(Ljava/lang/String;)V");
118132
g_admob.m_ShowInterstitial = env->GetMethodID(cls, "showInterstitial", "()V");
119-
g_admob.m_LoadRewarded = env->GetMethodID(cls, "loadRewarded", "(Ljava/lang/String;)V");
133+
g_admob.m_LoadRewarded = env->GetMethodID(cls, "loadRewarded", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V");
120134
g_admob.m_ShowRewarded = env->GetMethodID(cls, "showRewarded", "()V");
121135
g_admob.m_LoadRewardedInterstitial = env->GetMethodID(cls, "loadRewardedInterstitial", "(Ljava/lang/String;)V");
122136
g_admob.m_ShowRewardedInterstitial = env->GetMethodID(cls, "showRewardedInterstitial", "()V");
@@ -193,9 +207,9 @@ bool IsInterstitialLoaded()
193207
return CallBoolMethod(g_admob.m_AdmobJNI, g_admob.m_IsInterstitialLoaded);
194208
}
195209

196-
void LoadRewarded(const char* unitId)
210+
void LoadRewarded(const char* unitId, const char* userId, const char* customData)
197211
{
198-
CallVoidMethodChar(g_admob.m_AdmobJNI, g_admob.m_LoadRewarded, unitId);
212+
CallVoidMethodCharCharChar(g_admob.m_AdmobJNI, g_admob.m_LoadRewarded, unitId, userId, customData);
199213
}
200214

201215
void ShowRewarded()

extension-admob/src/admob_ios.mm

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,14 @@ void SetRewardedAd(GADRewardedAd *newAd) {
276276
rewardedAd = newAd;
277277
}
278278

279-
void LoadRewarded(const char* unitId) {
279+
void LoadRewarded(const char* unitId, const char* userId, const char* customData) {
280+
GADServerSideVerificationOptions *ssvOptions = [[GADServerSideVerificationOptions alloc] init];
281+
if (userId) {
282+
ssvOptions.userIdentifier = [NSString stringWithUTF8String:userId];
283+
}
284+
if (customData) {
285+
ssvOptions.customRewardString = [NSString stringWithUTF8String:customData];
286+
}
280287
[GADRewardedAd
281288
loadWithAdUnitID:[NSString stringWithUTF8String:unitId]
282289
request:createGADRequest()
@@ -288,6 +295,7 @@ void LoadRewarded(const char* unitId) {
288295
@"error", [NSString stringWithFormat:@"Error domain: \"%@\". %@", [error domain], [error localizedDescription]]);
289296
return;
290297
}
298+
ad.serverSideVerificationOptions = ssvOptions;
291299
SetRewardedAd(ad);
292300
SendSimpleMessage(MSG_REWARDED, EVENT_LOADED);
293301
}];

extension-admob/src/admob_private.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ void LoadAppOpen(const char* unitId, bool showImmediately);
5050
void ShowAppOpen();
5151
void LoadInterstitial(const char* unitId);
5252
void ShowInterstitial();
53-
void LoadRewarded(const char* unitId);
53+
void LoadRewarded(const char* unitId, const char* userId, const char* customData);
5454
void ShowRewarded();
5555
void LoadRewardedInterstitial(const char* unitId);
5656
void ShowRewardedInterstitial();

extension-admob/src/java/com/defold/admob/AdmobJNI.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import com.google.android.gms.ads.rewarded.RewardItem;
4444
import com.google.android.gms.ads.rewarded.RewardedAd;
4545
import com.google.android.gms.ads.rewarded.RewardedAdLoadCallback;
46+
import com.google.android.gms.ads.rewarded.ServerSideVerificationOptions;
4647

4748
import com.google.android.gms.ads.rewardedinterstitial.RewardedInterstitialAd;
4849
import com.google.android.gms.ads.rewardedinterstitial.RewardedInterstitialAdLoadCallback;
@@ -502,7 +503,15 @@ public boolean isInterstitialLoaded() {
502503

503504
private RewardedAd mRewardedAd;
504505

505-
public void loadRewarded(final String unitId) {
506+
private void setRewardedCustomData(final String userId, final String customData) {
507+
ServerSideVerificationOptions options = new ServerSideVerificationOptions.Builder()
508+
.setUserId(userId != null ? userId : "")
509+
.setCustomData(customData != null ? customData : "")
510+
.build();
511+
mRewardedAd.setServerSideVerificationOptions(options);
512+
}
513+
514+
public void loadRewarded(final String unitId, final String userId, final String customData) {
506515
activity.runOnUiThread(new Runnable() {
507516
@Override
508517
public void run() {
@@ -515,6 +524,7 @@ public void onAdLoaded(@NonNull RewardedAd rewardedAd) {
515524
// Log.d(TAG, "onAdLoaded");
516525
mRewardedAd = rewardedAd;
517526
sendSimpleMessage(MSG_REWARDED, EVENT_LOADED);
527+
setRewardedCustomData(userId, customData);
518528
mRewardedAd.setFullScreenContentCallback(new FullScreenContentCallback() {
519529
@Override
520530
public void onAdDismissedFullScreenContent() {

0 commit comments

Comments
 (0)