Skip to content

Commit 8792b79

Browse files
committed
fix(admob): native ad
1 parent c6b4c91 commit 8792b79

File tree

25 files changed

+1530
-1502
lines changed

25 files changed

+1530
-1502
lines changed

apps/demo/src/plugin-demos/firebase-admob.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import { Observable, EventData, Page } from '@nativescript/core';
1+
import { Observable, EventData, Page, View, Label } from '@nativescript/core';
22
import { DemoSharedFirebaseAdmob } from '@demo/shared';
3-
import { firebase, Firebase } from '@nativescript/firebase-core';
3+
import { firebase } from '@nativescript/firebase-core';
4+
import '@nativescript/firebase-admob';
45
import { AdEventType, InterstitialAd, RewardedInterstitialAd, RewardedAd, BannerAd, BannerAdSize, Admob, AdsConsent, NativeAd, NativeAdLoader, NativeAdView } from '@nativescript/firebase-admob';
5-
import { AdChoicesPlacement, NativeAdEventType } from '@nativescript/firebase-admob/nativead/common';
6+
import { AdChoicesPlacement, NativeAdEventType } from '@nativescript/firebase-admob';
67

78
export function navigatingTo(args: EventData) {
89
const page = <Page>args.object;
@@ -18,13 +19,11 @@ export class DemoModel extends DemoSharedFirebaseAdmob {
1819
}
1920
}
2021

21-
admob: Admob;
2222
constructor() {
2323
super();
2424
//this.interstitial();
2525
//this.rewardedInterstitial();
2626
//this.rewarded();
27-
this.admob = firebase().admob();
2827
// ATTrackingManager.requestTrackingAuthorizationWithCompletionHandler((status) => {
2928
// console.log('ATTrackingManager', status);
3029
// });
@@ -35,24 +34,33 @@ export class DemoModel extends DemoSharedFirebaseAdmob {
3534
} else {
3635
testDevices.push('EMULATOR');
3736
}
38-
this.admob.setRequestConfiguration({
37+
const admob = firebase().admob();
38+
admob.setRequestConfiguration({
3939
testDevices,
4040
});
4141
}
4242

43+
nativeAdLayoutChanged(event) {
44+
const view = event.object;
45+
const hlv = view.getViewById('headLineView') as Label;
46+
const mv = view.getViewById('mediaView');
47+
const bv = view.getViewById('bodyView');
48+
console.log('nativeAdLayoutChanged', hlv.nativeView, mv.nativeView, bv.nativeView);
49+
}
4350
nativeAdLoaded(event) {
4451
const view = event.object;
45-
const loader = new NativeAdLoader('ca-app-pub-3940256099942544/3986624511', null , {
52+
const loader = new NativeAdLoader('ca-app-pub-3940256099942544/3986624511', null, {
4653
nativeAdOptions: {
47-
adChoicesPlacement: AdChoicesPlacement.TOP_RIGHT
48-
}
54+
adChoicesPlacement: AdChoicesPlacement.TOP_RIGHT,
55+
},
4956
});
5057
loader.onAdEvent((event, error, data) => {
5158
if (event === NativeAdEventType.LOADED) {
5259
const ad = data as NativeAd;
53-
const hlv = view.getViewById('headLineView');
60+
const hlv = view.getViewById('headLineView') as Label;
5461
hlv.text = ad.headline;
5562
const mv = view.getViewById('mediaView');
63+
view.mediaView = mv;
5664
mv.mediaContent = ad.mediaContent;
5765
const bv = view.getViewById('bodyView');
5866
bv.text = ad.body;

apps/demo/src/plugin-demos/firebase-admob.xml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ xmlns:ui="@nativescript/firebase-admob"
77
</Page.actionBar>
88
<StackLayout class="p-20">
99
<!-- <ui:BannerAd height="100" width="100" unitId="{{bannerAdUnit}}" layoutChanged="{{bannerLoaded}}"/> -->
10-
<ui:NativeAdView height="400" loaded="{{nativeAdLoaded}}">
11-
<GridLayout height="300" width="300">
12-
<Label id="headLineView"/>
13-
<ui:MediaView id="mediaView"/>
14-
<Label id="bodyView"/>
10+
<ui:NativeAdView height="400" loaded="{{nativeAdLoaded}}" layoutChanged="{{nativeAdLayoutChanged}}">
11+
<GridLayout rows="auto,auto,auto" height="300" width="300">
12+
<Label id="headLineView" text="First"/>
13+
<ui:MediaView row="1" id="mediaView" height="100%"/>
14+
<Label row="2" id="bodyView"/>
1515
</GridLayout>
1616
</ui:NativeAdView>
1717
</StackLayout>

packages/firebase-admob/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ Through the use of NativeAdListener, you can listen for lifecycle events, such a
309309
<ui:NativeAdView height="400" loaded="{{nativeAdLoaded}}">
310310
<GridLayout height="300" width="300">
311311
<Label id="headLineView" />
312-
<ui:MediaView id="mediaView" />
312+
<ui:MediaView id="mediaView" height="100%"/>
313313
<Label id="bodyView" />
314314
</GridLayout>
315315
</ui:NativeAdView>
@@ -324,6 +324,7 @@ loader.onAdEvent((event, error, data) => {
324324
const hlv = view.getViewById('headLineView');
325325
hlv.text = ad.headline;
326326
const mv = view.getViewById('mediaView');
327+
view.mediaView = mv;
327328
mv.mediaContent = ad.mediaContent;
328329
const bv = view.getViewById('bodyView');
329330
bv.text = ad.body;

packages/firebase-admob/common.ts

Lines changed: 106 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -1,173 +1,183 @@
1-
import {FirebaseApp} from '@nativescript/firebase-core';
2-
import {CSSType, Property, View} from '@nativescript/core';
1+
import { FirebaseApp } from '@nativescript/firebase-core';
2+
import { CSSType, Property, View } from '@nativescript/core';
33

44
export interface IMobileAd {
5-
adUnitId: string;
6-
loaded: boolean;
5+
adUnitId: string;
6+
loaded: boolean;
77

8-
load(): void;
8+
load(): void;
99

10-
onAdEvent(listener?: AdEventListener);
10+
onAdEvent(listener?: AdEventListener);
1111

12-
show(showOptions?: AdShowOptions);
12+
show(showOptions?: AdShowOptions);
1313
}
1414

1515
export interface IRewardedInterstitialAd extends IInterstitialAd {
16-
setServerSideVerificationOptions(options: ServerSideVerificationOptions): void;
16+
setServerSideVerificationOptions(options: ServerSideVerificationOptions): void;
1717
}
1818

1919
export interface ServerSideVerificationOptions {
20-
userId: string;
21-
customData: string;
20+
userId: string;
21+
customData: string;
2222
}
2323

24-
export interface IInterstitialAd extends IMobileAd {
25-
}
24+
export interface IInterstitialAd extends IMobileAd {}
2625

2726
export interface IRewardedAd extends IMobileAd {
28-
setServerSideVerificationOptions(options: ServerSideVerificationOptions): void;
27+
setServerSideVerificationOptions(options: ServerSideVerificationOptions): void;
2928
}
3029

3130
export abstract class BannerAdSizeBase {
32-
static get BANNER(): BannerAdSizeBase {
33-
throw new Error('unimplemented');
34-
}
31+
static get BANNER(): BannerAdSizeBase {
32+
throw new Error('unimplemented');
33+
}
3534

36-
static get FULL_BANNER(): BannerAdSizeBase {
37-
throw new Error('unimplemented');
38-
}
35+
static get FULL_BANNER(): BannerAdSizeBase {
36+
throw new Error('unimplemented');
37+
}
3938

40-
static get LARGE_BANNER(): BannerAdSizeBase {
41-
throw new Error('unimplemented');
42-
}
39+
static get LARGE_BANNER(): BannerAdSizeBase {
40+
throw new Error('unimplemented');
41+
}
4342

44-
static get LEADERBOARD(): BannerAdSizeBase {
45-
throw new Error('unimplemented');
46-
}
43+
static get LEADERBOARD(): BannerAdSizeBase {
44+
throw new Error('unimplemented');
45+
}
4746

48-
static get MEDIUM_RECTANGLE(): BannerAdSizeBase {
49-
throw new Error('unimplemented');
50-
}
47+
static get MEDIUM_RECTANGLE(): BannerAdSizeBase {
48+
throw new Error('unimplemented');
49+
}
5150

52-
static get SMART_BANNER(): BannerAdSizeBase {
53-
throw new Error('unimplemented');
54-
}
51+
static get SMART_BANNER(): BannerAdSizeBase {
52+
throw new Error('unimplemented');
53+
}
5554

56-
static get ADAPTIVE_BANNER(): BannerAdSizeBase {
57-
throw new Error('unimplemented');
58-
}
55+
static get ADAPTIVE_BANNER(): BannerAdSizeBase {
56+
throw new Error('unimplemented');
57+
}
5958

60-
static get FLUID(): BannerAdSizeBase {
61-
throw new Error('unimplemented');
62-
}
59+
static get FLUID(): BannerAdSizeBase {
60+
throw new Error('unimplemented');
61+
}
6362

64-
static get WIDE_SKYSCRAPER(): BannerAdSizeBase {
65-
throw new Error('unimplemented');
66-
}
63+
static get WIDE_SKYSCRAPER(): BannerAdSizeBase {
64+
throw new Error('unimplemented');
65+
}
6766

68-
static get INVALID(): BannerAdSizeBase {
69-
throw new Error('unimplemented');
70-
}
67+
static get INVALID(): BannerAdSizeBase {
68+
throw new Error('unimplemented');
69+
}
7170

72-
static get SEARCH(): BannerAdSizeBase {
73-
throw new Error('unimplemented');
74-
}
71+
static get SEARCH(): BannerAdSizeBase {
72+
throw new Error('unimplemented');
73+
}
7574
}
7675

7776
export const sizeProperty = new Property<BannerAdBase, BannerAdSizeBase>({
78-
name: 'size',
77+
name: 'size',
7978
});
8079

8180
export const unitIdProperty = new Property<BannerAdBase, BannerAdSizeBase>({
82-
name: 'unitId',
81+
name: 'unitId',
8382
});
8483

8584
@CSSType('BannerAd')
8685
export abstract class BannerAdBase extends View {
87-
static onAdClickedEvent = 'adClicked';
88-
static onAdClosedEvent = 'adClosed';
89-
static onAdFailedToLoadEvent = 'adFailedToLoad';
90-
static onAdLoadedEvent = 'adLoaded';
91-
static onAdOpenedEvent = 'adOpened';
92-
static onAdImpression = 'adImpression';
93-
static onAdFailedToShowFullScreenContent = 'adFailedToShowFullScreenContent';
94-
size: BannerAdSizeBase;
95-
unitId: BannerAdSizeBase;
86+
static onAdClickedEvent = 'adClicked';
87+
static onAdClosedEvent = 'adClosed';
88+
static onAdFailedToLoadEvent = 'adFailedToLoad';
89+
static onAdLoadedEvent = 'adLoaded';
90+
static onAdOpenedEvent = 'adOpened';
91+
static onAdImpression = 'adImpression';
92+
static onAdFailedToShowFullScreenContent = 'adFailedToShowFullScreenContent';
93+
size: BannerAdSizeBase;
94+
unitId: BannerAdSizeBase;
9695

97-
abstract load(options?: RequestOptions);
96+
abstract load(options?: RequestOptions);
9897

99-
abstract isLoading(): boolean;
98+
abstract isLoading(): boolean;
10099
}
101100

102101
sizeProperty.register(BannerAdBase);
103102
unitIdProperty.register(BannerAdBase);
104103

105104
export enum MaxAdContentRating {
106-
G = 'G',
107-
MA = 'MA',
108-
PG = 'PG',
109-
T = 'T',
110-
UNSPECIFIED = 'UNSPECIFIED',
105+
G = 'G',
106+
MA = 'MA',
107+
PG = 'PG',
108+
T = 'T',
109+
UNSPECIFIED = 'UNSPECIFIED',
111110
}
112111

113112
export enum AdEventType {
114-
CLICKED = 'adClicked',
115-
CLOSED = 'adClosed',
116-
LOADED = 'adLoaded',
117-
OPENED = 'adOpened',
118-
IMPRESSION = 'adImpression',
119-
FAILED_TO_SHOW_FULL_SCREEN_CONTENT = 'adFailedToShowFullScreenContent',
120-
FAILED_TO_LOAD_EVENT = 'adFailedToLoad',
113+
CLICKED = 'adClicked',
114+
CLOSED = 'adClosed',
115+
LOADED = 'adLoaded',
116+
OPENED = 'adOpened',
117+
IMPRESSION = 'adImpression',
118+
FAILED_TO_SHOW_FULL_SCREEN_CONTENT = 'adFailedToShowFullScreenContent',
119+
FAILED_TO_LOAD_EVENT = 'adFailedToLoad',
121120
}
122121

123122
export interface AdShowOptions {
124-
immersiveModeEnabled: undefined | false | true;
123+
immersiveModeEnabled: undefined | false | true;
125124
}
126125

127126
export interface RequestConfiguration {
128-
maxAdContentRating?: MaxAdContentRating.G | MaxAdContentRating.PG | MaxAdContentRating.T | MaxAdContentRating.MA;
129-
tagForChildDirectedTreatment?: undefined | false | true;
130-
tagForUnderAgeOfConsent?: undefined | false | true;
131-
testDevices?: string[];
127+
maxAdContentRating?: MaxAdContentRating.G | MaxAdContentRating.PG | MaxAdContentRating.T | MaxAdContentRating.MA;
128+
tagForChildDirectedTreatment?: undefined | false | true;
129+
tagForUnderAgeOfConsent?: undefined | false | true;
130+
testDevices?: string[];
132131
}
133132

134133
export interface RequestOptions {
135-
contentUrl?: undefined | string;
136-
keywords?: string[];
137-
location?: [number, number];
138-
locationAccuracy?: undefined | number;
139-
networkExtras?: undefined | { [key: string]: string };
140-
requestAgent?: undefined | string;
141-
requestNonPersonalizedAdsOnly?: undefined | false | true;
134+
contentUrl?: undefined | string;
135+
keywords?: string[];
136+
location?: [number, number];
137+
locationAccuracy?: undefined | number;
138+
networkExtras?: undefined | { [key: string]: string };
139+
requestAgent?: undefined | string;
140+
requestNonPersonalizedAdsOnly?: undefined | false | true;
142141
}
143142

144143
export interface ManagerRequestOptions extends RequestOptions {
145-
publisherProvidedId?: string;
146-
customTargeting?: { [key: string]: string | string[] };
147-
categoryExclusions?: string[];
148-
adString?: string;
144+
publisherProvidedId?: string;
145+
customTargeting?: { [key: string]: string | string[] };
146+
categoryExclusions?: string[];
147+
adString?: string;
149148
}
150149

151150
export enum RewardedAdEventType {
152-
EARNED_REWARD = 'rewarded_earned_reward',
153-
LOADED = 'rewarded_loaded',
151+
EARNED_REWARD = 'rewarded_earned_reward',
152+
LOADED = 'rewarded_loaded',
154153
}
155154

156155
export interface IRewardedItem {
157-
amount: number;
158-
type: string;
156+
amount: number;
157+
type: string;
159158
}
160159

161160
export interface TestIds {
162-
BANNER: string;
163-
INTERSTITIAL: string;
164-
REWARDED: string;
161+
BANNER: string;
162+
INTERSTITIAL: string;
163+
REWARDED: string;
164+
}
165+
166+
export enum AdapterStatusState {
167+
NOT_READY,
168+
READY,
169+
}
170+
171+
export interface AdapterStatus {
172+
description: string;
173+
latency: number;
174+
initializationState: AdapterStatusState;
165175
}
166176

167177
export type AdEventListener = (type: AdEventType | RewardedAdEventType, error?: Error, data?: any | IRewardedItem) => void;
168178

169179
export interface IAdmob {
170-
app: FirebaseApp;
180+
app: FirebaseApp;
171181

172-
setRequestConfiguration(requestConfiguration: RequestConfiguration);
182+
setRequestConfiguration(requestConfiguration: RequestConfiguration);
173183
}

0 commit comments

Comments
 (0)