Skip to content

Commit 9d06c1e

Browse files
committed
fix(admob): request configuration ignored
feat(admob): expose current adrequest fixes #103
1 parent 87ea65c commit 9d06c1e

File tree

6 files changed

+356
-69
lines changed

6 files changed

+356
-69
lines changed

packages/firebase-admob/index.android.ts

Lines changed: 139 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,61 @@ function ensureAdListener() {
8787
AdListener = AdListenerImpl;
8888
}
8989

90+
export class AdRequest {
91+
#native: com.google.android.gms.ads.AdRequest;
92+
93+
static fromNative(request: com.google.android.gms.ads.AdRequest) {
94+
if (request instanceof com.google.android.gms.ads.AdRequest) {
95+
const ret = new AdRequest();
96+
ret.#native = request;
97+
return ret;
98+
}
99+
return null;
100+
}
101+
102+
get contentUrl(): string {
103+
return this.#native.getContentUrl();
104+
}
105+
106+
get keywords(): string[] {
107+
const kw = this.#native.getKeywords().toArray();
108+
const count = kw.length;
109+
const ret = [];
110+
for (let i = 0; i < count; i++) {
111+
ret.push(kw[i]);
112+
}
113+
return ret;
114+
}
115+
116+
get neighboringContentUrls(): string[] {
117+
const urls = this.#native.getNeighboringContentUrls();
118+
const count = urls.size();
119+
const ret = [];
120+
for (let i = 0; i < count; i++) {
121+
ret.push(urls.get(i));
122+
}
123+
return ret;
124+
}
125+
126+
isTestDevice(): boolean {
127+
return this.#native.isTestDevice(Utils.android.getApplicationContext());
128+
}
129+
130+
get native() {
131+
return this.#native;
132+
}
133+
134+
get android() {
135+
return this.native;
136+
}
137+
}
138+
90139
export class InterstitialAd implements IInterstitialAd {
91140
#native: com.google.android.gms.ads.interstitial.InterstitialAd;
92141
#adUnitId: string;
93142
#requestOptions?: RequestOptions;
94143
#loaded = false;
144+
#nativeRequest: com.google.android.gms.ads.AdRequest;
95145

96146
static createForAdRequest(adUnitId: string, requestOptions?: RequestOptions): InterstitialAd {
97147
const ad = new InterstitialAd();
@@ -120,7 +170,7 @@ export class InterstitialAd implements IInterstitialAd {
120170

121171
load(): void {
122172
const ref = new WeakRef(this);
123-
org.nativescript.firebase.admob.FirebaseAdmob.InterstitialAd.load(
173+
this.#nativeRequest = org.nativescript.firebase.admob.FirebaseAdmob.InterstitialAd.load(
124174
Application.android.foregroundActivity || Application.android.startActivity,
125175
this.#adUnitId,
126176
JSON.stringify(this.#requestOptions || {}),
@@ -173,13 +223,18 @@ export class InterstitialAd implements IInterstitialAd {
173223
get android() {
174224
return this.native;
175225
}
226+
227+
get request() {
228+
return AdRequest.fromNative(this.#nativeRequest);
229+
}
176230
}
177231

178232
export class RewardedInterstitialAd implements IRewardedInterstitialAd {
179233
#native: com.google.android.gms.ads.rewardedinterstitial.RewardedInterstitialAd;
180234
#adUnitId: string;
181235
#requestOptions?: RequestOptions;
182236
#loaded = false;
237+
#nativeRequest: com.google.android.gms.ads.AdRequest;
183238

184239
static createForAdRequest(adUnitId: string, requestOptions?: RequestOptions): RewardedInterstitialAd {
185240
const ad = new RewardedInterstitialAd();
@@ -206,7 +261,7 @@ export class RewardedInterstitialAd implements IRewardedInterstitialAd {
206261

207262
load(): void {
208263
const ref = new WeakRef(this);
209-
org.nativescript.firebase.admob.FirebaseAdmob.RewardedInterstitialAd.load(
264+
this.#nativeRequest = org.nativescript.firebase.admob.FirebaseAdmob.RewardedInterstitialAd.load(
210265
Application.android.foregroundActivity || Application.android.startActivity,
211266
this.#adUnitId,
212267
JSON.stringify(this.#requestOptions || {}),
@@ -284,13 +339,18 @@ export class RewardedInterstitialAd implements IRewardedInterstitialAd {
284339
get android() {
285340
return this.native;
286341
}
342+
343+
get request() {
344+
return AdRequest.fromNative(this.#nativeRequest);
345+
}
287346
}
288347

289348
export class RewardedAd implements IRewardedAd {
290349
#native: com.google.android.gms.ads.rewarded.RewardedAd;
291350
#adUnitId: string;
292351
#requestOptions?: RequestOptions;
293352
#loaded = false;
353+
#nativeRequest: com.google.android.gms.ads.AdRequest;
294354

295355
static createForAdRequest(adUnitId: string, requestOptions?: RequestOptions): RewardedAd {
296356
const reward = new RewardedAd();
@@ -317,7 +377,7 @@ export class RewardedAd implements IRewardedAd {
317377

318378
load(): void {
319379
const ref = new WeakRef(this);
320-
org.nativescript.firebase.admob.FirebaseAdmob.RewardedAd.load(
380+
this.#nativeRequest = org.nativescript.firebase.admob.FirebaseAdmob.RewardedAd.load(
321381
Application.android.foregroundActivity || Application.android.startActivity,
322382
this.#adUnitId,
323383
JSON.stringify(this.#requestOptions || {}),
@@ -395,6 +455,10 @@ export class RewardedAd implements IRewardedAd {
395455
get android() {
396456
return this.native;
397457
}
458+
459+
get request() {
460+
return AdRequest.fromNative(this.#nativeRequest);
461+
}
398462
}
399463

400464
export class RewardedItem implements IRewardedItem {
@@ -532,7 +596,7 @@ export class BannerAdSize extends BannerAdSizeBase {
532596
export class BannerAd extends BannerAdBase {
533597
#native: com.google.android.gms.ads.AdView;
534598
#listener;
535-
599+
#nativeRequest: com.google.android.gms.ads.AdRequest;
536600
[sizeProperty.setNative](value) {
537601
if (this.#native) {
538602
this.#native.setAdSize(value?.native);
@@ -559,13 +623,17 @@ export class BannerAd extends BannerAdBase {
559623

560624
load(options?: RequestOptions) {
561625
if (this.#native) {
562-
org.nativescript.firebase.admob.FirebaseAdmob.BannerAd.load(JSON.stringify(options || {}), this.#native);
626+
this.#nativeRequest = org.nativescript.firebase.admob.FirebaseAdmob.BannerAd.load(JSON.stringify(options || {}), this.#native);
563627
}
564628
}
565629

566630
isLoading(): boolean {
567631
return this.#native?.isLoading?.();
568632
}
633+
634+
get request() {
635+
return AdRequest.fromNative(this.#nativeRequest);
636+
}
569637
}
570638

571639
export class Admob implements IAdmob {
@@ -595,7 +663,7 @@ export class Admob implements IAdmob {
595663
});
596664
}
597665

598-
setRequestConfiguration(requestConfiguration: RequestConfiguration) {
666+
set requestConfiguration(requestConfiguration: RequestConfiguration) {
599667
try {
600668
const parsedConfiguration: any = { ...requestConfiguration };
601669
if (typeof parsedConfiguration.tagForChildDirectedTreatment === 'boolean') {
@@ -613,6 +681,71 @@ export class Admob implements IAdmob {
613681
} catch (e) {}
614682
}
615683

684+
get requestConfiguration(): RequestConfiguration {
685+
let ret: RequestConfiguration = {};
686+
const config = com.google.android.gms.ads.MobileAds.getRequestConfiguration();
687+
688+
switch (config.getTagForChildDirectedTreatment()) {
689+
case com.google.android.gms.ads.RequestConfiguration.TAG_FOR_CHILD_DIRECTED_TREATMENT_TRUE:
690+
ret.tagForChildDirectedTreatment = true;
691+
break;
692+
case com.google.android.gms.ads.RequestConfiguration.TAG_FOR_CHILD_DIRECTED_TREATMENT_FALSE:
693+
ret.tagForChildDirectedTreatment = false;
694+
break;
695+
default:
696+
// noop
697+
break;
698+
}
699+
700+
switch (config.getTagForUnderAgeOfConsent()) {
701+
case com.google.android.gms.ads.RequestConfiguration.TAG_FOR_UNDER_AGE_OF_CONSENT_TRUE:
702+
ret.tagForUnderAgeOfConsent = true;
703+
break;
704+
case com.google.android.gms.ads.RequestConfiguration.TAG_FOR_UNDER_AGE_OF_CONSENT_FALSE:
705+
ret.tagForUnderAgeOfConsent = false;
706+
break;
707+
default:
708+
// noop
709+
break;
710+
}
711+
712+
switch (config.getMaxAdContentRating()) {
713+
case com.google.android.gms.ads.RequestConfiguration.MAX_AD_CONTENT_RATING_G:
714+
ret.maxAdContentRating = MaxAdContentRating.G;
715+
break;
716+
case com.google.android.gms.ads.RequestConfiguration.MAX_AD_CONTENT_RATING_MA:
717+
ret.maxAdContentRating = MaxAdContentRating.MA;
718+
break;
719+
case com.google.android.gms.ads.RequestConfiguration.MAX_AD_CONTENT_RATING_PG:
720+
ret.maxAdContentRating = MaxAdContentRating.PG;
721+
break;
722+
case com.google.android.gms.ads.RequestConfiguration.MAX_AD_CONTENT_RATING_T:
723+
ret.maxAdContentRating = MaxAdContentRating.T;
724+
break;
725+
default:
726+
// noop
727+
break;
728+
}
729+
730+
ret.testDevices = [];
731+
732+
const devices = config.getTestDeviceIds();
733+
if (devices) {
734+
const count = devices.size();
735+
for (let i = 0; i < count; i++) {
736+
ret.testDevices.push(devices.get(i));
737+
}
738+
}
739+
return ret;
740+
}
741+
742+
setRequestConfiguration(requestConfiguration: RequestConfiguration) {
743+
this.requestConfiguration = requestConfiguration;
744+
}
745+
getRequestConfiguration(): RequestConfiguration {
746+
return this.requestConfiguration;
747+
}
748+
616749
get app(): FirebaseApp {
617750
if (!this.#app) {
618751
// @ts-ignore

packages/firebase-admob/index.d.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,20 @@ export { MaxAdContentRating, AdEventType, RewardedAdEventType };
88
export * from './adsconsent';
99
export * from './nativead';
1010

11+
export declare class AdRequest {
12+
readonly contentUrl: string;
13+
14+
readonly keywords: string[];
15+
16+
readonly neighboringContentUrls: string[];
17+
18+
readonly native: any;
19+
readonly android: any;
20+
readonly ios: any;
21+
22+
isTestDevice(): boolean;
23+
}
24+
1125
export declare class InterstitialAd implements IInterstitialAd {
1226
static createForAdRequest(adUnitId: string): InterstitialAd;
1327
static createForAdRequest(adUnitId: string, requestOptions?: RequestOptions): InterstitialAd;
@@ -127,7 +141,17 @@ export declare class Admob implements IAdmob {
127141

128142
static init(): Promise<{ [key: string]: AdapterStatus }>;
129143

144+
requestConfiguration: RequestConfiguration;
145+
146+
/**
147+
* @deprecated Use requestConfiguration
148+
*/
130149
setRequestConfiguration(requestConfiguration: RequestConfiguration);
150+
151+
/**
152+
* @deprecated Use requestConfiguration
153+
*/
154+
getRequestConfiguration(requestConfiguration: RequestConfiguration);
131155
}
132156

133157
declare module '@nativescript/firebase-core' {

0 commit comments

Comments
 (0)