Skip to content
This repository was archived by the owner on Apr 4, 2023. It is now read-only.

Commit 70b8851

Browse files
Add a way for the developer to preload AdMob interstitials #934
1 parent 74c0584 commit 70b8851

File tree

15 files changed

+761
-562
lines changed

15 files changed

+761
-562
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ src/platforms/android/include.gradle
1717
!src/references.d.ts
1818
!src/analytics/analytics.d.ts
1919
!src/storage/storage.d.ts
20+
!src/admob/admob.d.ts
2021
!src/messaging/messaging.d.ts
2122
!src/mlkit/*/index.d.ts
2223
!src/mlkit/mlkit-cameraview.d.ts

demo/app/main-page.xml

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@
8585
<TabViewItem.view>
8686
<ScrollView>
8787
<GridLayout columns="*, *"
88-
rows="auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto"
88+
rows="auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto"
8989
horizontalAlignment="stretch"
9090
class="tab-content">
9191

@@ -187,34 +187,37 @@
187187
<Button row="35" col="0" text="show banner" tap="{{ doShowAdMobBanner }}" class="button button-admob"/>
188188
<Button row="35" col="1" text="show interstitial" tap="{{ doShowAdMobInterstitial }}" class="button button-admob"/>
189189

190-
<Button row="36" colSpan="2" text="hide banner" tap="{{ doHideAdMobBanner }}" class="button button-admob"/>
190+
<Button row="36" col="0" text="preload interstitial" tap="{{ doPreloadAdMobInterstitial }}" class="button button-admob"/>
191+
<Button row="36" col="1" text="show preloaded" tap="{{ doShowPreloadedAdMobInterstitial }}" class="button button-admob"/>
191192

192-
<Label row="37" colSpan="2" text="Firebase Cloud Messaging" class="subtitle"/>
193+
<Button row="37" colSpan="2" text="hide banner" tap="{{ doHideAdMobBanner }}" class="button button-admob"/>
193194

194-
<Button row="38" col="0" text="add push handlers" tap="{{ messaging.doRegisterPushHandlers }}" class="button button-messaging"/>
195-
<Button row="38" col="1" text="get current token" tap="{{ messaging.doGetCurrentPushToken }}" class="button button-messaging"/>
195+
<Label row="38" colSpan="2" text="Firebase Cloud Messaging" class="subtitle"/>
196196

197-
<Button row="39" col="0" text="topic subscribe" tap="{{ messaging.doSubscribeToTopic }}" class="button button-messaging"/>
198-
<Button row="39" col="1" text="topic unsubscribe" tap="{{ messaging.doUnsubscribeFromTopic }}" class="button button-messaging"/>
197+
<Button row="39" col="0" text="add push handlers" tap="{{ messaging.doRegisterPushHandlers }}" class="button button-messaging"/>
198+
<Button row="39" col="1" text="get current token" tap="{{ messaging.doGetCurrentPushToken }}" class="button button-messaging"/>
199+
200+
<Button row="40" col="0" text="topic subscribe" tap="{{ messaging.doSubscribeToTopic }}" class="button button-messaging"/>
201+
<Button row="40" col="1" text="topic unsubscribe" tap="{{ messaging.doUnsubscribeFromTopic }}" class="button button-messaging"/>
199202
<iOS>
200-
<Button row="40" colSpan="2" text="register interactive push" tap="{{ messaging.doRegisterForInteractivePush }}" class="button button-messaging"/>
203+
<Button row="41" colSpan="2" text="register interactive push" tap="{{ messaging.doRegisterForInteractivePush }}" class="button button-messaging"/>
201204
</iOS>
202-
<Button row="41" colSpan="2" text="are notifications enabled" tap="{{ messaging.doGetAreNotificationsEnabled }}" class="button button-messaging"/>
205+
<Button row="42" colSpan="2" text="are notifications enabled" tap="{{ messaging.doGetAreNotificationsEnabled }}" class="button button-messaging"/>
203206
<!-- <Button row="39" col="1" text="topic unsubscribe" tap="{{ doUnsubscribeFromTopic }}" class="button button-messaging"/> -->
204207

205-
<Label row="42" colSpan="2" text="Firebase Crash Reporing / Crashlytics" class="subtitle"/>
208+
<Label row="43" colSpan="2" text="Firebase Crash Reporing / Crashlytics" class="subtitle"/>
206209

207-
<Button row="43" col="0" text="log message" tap="{{ doLogMessage }}" class="button button-crash"/>
210+
<Button row="44" col="0" text="log message" tap="{{ doLogMessage }}" class="button button-crash"/>
208211
<iOS>
209-
<Button row="43" col="1" text="force crash :)" tap="{{ doForceCrashIOS }}" class="button button-crash"/>
212+
<Button row="44" col="1" text="force crash :)" tap="{{ doForceCrashIOS }}" class="button button-crash"/>
210213
</iOS>
211214
<Android>
212-
<Button row="43" col="1" text="force crash :)" tap="{{ doForceCrashAndroid }}" class="button button-crash"/>
215+
<Button row="44" col="1" text="force crash :)" tap="{{ doForceCrashAndroid }}" class="button button-crash"/>
213216
</Android>
214217

215-
<Label row="44" colSpan="2" text="Firebase Invites" class="subtitle"/>
216-
<Button row="45" col="0" text="send invitation" tap="{{ sendInvitation }}" class="button button-invites"/>
217-
<Button row="45" col="1" text="get invitation" tap="{{ getInvitation }}" class="button button-invites"/>
218+
<Label row="45" colSpan="2" text="Firebase Invites" class="subtitle"/>
219+
<Button row="46" col="0" text="send invitation" tap="{{ sendInvitation }}" class="button button-invites"/>
220+
<Button row="46" col="1" text="get invitation" tap="{{ getInvitation }}" class="button button-invites"/>
218221
</GridLayout>
219222

220223
</ScrollView>

demo/app/main-view-model.ts

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Observable } from "tns-core-modules/data/observable";
22
import { alert, prompt } from "tns-core-modules/ui/dialogs";
33
import { isIOS } from "tns-core-modules/platform";
44
import * as firebase from "nativescript-plugin-firebase";
5-
import { AddEventListenerResult, storage as firebaseStorage, User } from "nativescript-plugin-firebase";
5+
import { AddEventListenerResult, storage as firebaseStorage, admob as firebaseAdMob, User } from "nativescript-plugin-firebase";
66
import * as fs from "tns-core-modules/file-system";
77
import { MessagingViewModel } from './messaging-view-model';
88

@@ -546,8 +546,8 @@ export class HelloWorldModel extends Observable {
546546
}
547547

548548
public doShowAdMobBanner(): void {
549-
firebase.admob.showBanner({
550-
size: firebase.admob.AD_SIZE.SMART_BANNER,
549+
firebaseAdMob.showBanner({
550+
size: firebaseAdMob.AD_SIZE.SMART_BANNER,
551551
margins: {
552552
bottom: isIOS ? 50 : 0
553553
},
@@ -602,6 +602,41 @@ export class HelloWorldModel extends Observable {
602602
);
603603
}
604604

605+
public doPreloadAdMobInterstitial(): void {
606+
firebaseAdMob.preloadInterstitial({
607+
iosInterstitialId: "ca-app-pub-9517346003011652/6938836122",
608+
androidInterstitialId: "ca-app-pub-9517346003011652/6938836122",
609+
testing: true,
610+
// Android automatically adds the connected device as test device with testing:true, iOS does not
611+
iosTestDeviceIds: [
612+
"45d77bf513dfabc2949ba053da83c0c7b7e87715", // Eddy's iPhone 6s
613+
"fee4cf319a242eab4701543e4c16db89c722731f" // Eddy's iPad Pro
614+
]
615+
}).then(
616+
() => console.log("AdMob interstitial preloaded"),
617+
errorMessage => {
618+
alert({
619+
title: "AdMob error",
620+
message: errorMessage,
621+
okButtonText: "Hmmkay"
622+
});
623+
}
624+
);
625+
}
626+
627+
public doShowPreloadedAdMobInterstitial(): void {
628+
firebaseAdMob.showInterstitial().then(
629+
() => console.log("AdMob interstitial showing"),
630+
errorMessage => {
631+
alert({
632+
title: "AdMob error",
633+
message: errorMessage,
634+
okButtonText: "Hmmkay"
635+
});
636+
}
637+
);
638+
}
639+
605640
/**
606641
* Note that an interstitial is supposed to be hidden by clicking the close button,
607642
* so there's no function to do it programmatically.

demo/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
}
1010
},
1111
"dependencies": {
12-
"nativescript-plugin-firebase": "file:../publish/package/nativescript-plugin-firebase-7.1.0.tgz",
12+
"nativescript-plugin-firebase": "file:../publish/package/nativescript-plugin-firebase-7.1.1.tgz",
1313
"nativescript-theme-core": "^1.0.4",
1414
"nativescript-unit-test-runner": "^0.3.4",
1515
"tns-core-modules": "^4.2.1"

docs/ADMOB.md

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
## Enabling AdMob
44
Since plugin version 3.10.0 you can use Firebase _AdMob_ features.
55

6-
_AdMob_ lets you show banners or interstitials (full screen ads) in your app so you can earn a little money.
6+
_AdMob_ lets you show banners or interstitials (full screen ads) in your app so you can earn some money.
77

88
### iOS
99

@@ -81,8 +81,13 @@ This is a fullscreen ad, so you can earn extra credit on the eternal ladder of a
8181

8282
Note that an interstitial is supposed to be hidden by clicking the close button, so there's no function to do it programmatically.
8383

84+
There's two ways how you can use this function:
85+
86+
* RECOMMENDED: without arguments, and after the Promise of 'preloadInterstitial' resolves. This will show the interstitial immediately.
87+
* DEPRECATED: with arguments (same as 'preloadInterstitial'). This will preload and _then_ show the interstitial, so a delay will be noticable by the user, which is against Google's policies.
88+
8489
```js
85-
firebase.admob.showInterstitial({
90+
firebase.admob.preloadInterstitial({
8691
iosInterstitialId: "ca-app-pub-9517346003011652/6938836122",
8792
androidInterstitialId: "ca-app-pub-9517346003011652/6938836122",
8893
testing: true, // when not running in production set this to true, Google doesn't like it any other way
@@ -92,7 +97,24 @@ Note that an interstitial is supposed to be hidden by clicking the close button,
9297
]
9398
}).then(
9499
function () {
95-
console.log("AdMob interstitial showing");
100+
console.log("AdMob interstitial preloaded, you can now call 'showInterstitial' at any time to show it without delay.");
101+
},
102+
function (errorMessage) {
103+
dialogs.alert({
104+
title: "AdMob error",
105+
message: errorMessage,
106+
okButtonText: "Hmmkay"
107+
});
108+
}
109+
);
110+
```
111+
112+
After the preload Promise resolved successfully, you can show the interstitial at any time you want:
113+
114+
```js
115+
firebase.admob.showInterstitial().then(
116+
function () {
117+
console.log("AdMob interstitial showing.");
96118
},
97119
function (errorMessage) {
98120
dialogs.alert({

src/admob/admob-common.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
export const AD_SIZE = {
2+
SMART_BANNER: "SMART",
3+
LARGE_BANNER: "LARGE",
4+
BANNER: "BANNER",
5+
MEDIUM_RECTANGLE: "MEDIUM",
6+
FULL_BANNER: "FULL",
7+
LEADERBOARD: "LEADERBOARD",
8+
SKYSCRAPER: "SKYSCRAPER",
9+
FLUID: "FLUID"
10+
};
11+
12+
export const BANNER_DEFAULTS = {
13+
margins: {
14+
top: -1,
15+
bottom: -1
16+
},
17+
testing: false,
18+
size: "SMART",
19+
view: undefined
20+
};

0 commit comments

Comments
 (0)