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

Commit 342887e

Browse files
Add support for AdMob's "Rewarded Video Ads" #1085
1 parent 8bd54e8 commit 342887e

File tree

9 files changed

+481
-47
lines changed

9 files changed

+481
-47
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
- [Firebase iOS SDK Changelog](https://firebase.google.com/support/release-notes/ios)
44
- [Firebase Android SDK Changelog](https://firebase.google.com/support/release-notes/android)
55

6+
## 7.6.1 (2019, January 6)
7+
[Fixes & Enhancements](https://github.com/EddyVerbruggen/nativescript-plugin-firebase/milestone/91?closed=1)
8+
9+
610
## 7.6.0 (2018, December 21)
711
[Fixes & Enhancements](https://github.com/EddyVerbruggen/nativescript-plugin-firebase/milestone/90?closed=1)
812

demo/app/main-page.xml

Lines changed: 36 additions & 33 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, 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, auto, auto, auto, auto, auto, auto, auto, auto, auto"
8989
horizontalAlignment="stretch"
9090
class="tab-content">
9191

@@ -190,50 +190,53 @@
190190
<Button row="36" col="0" text="preload interstitial" tap="{{ doPreloadAdMobInterstitial }}" class="button button-admob"/>
191191
<Button row="36" col="1" text="show preloaded" tap="{{ doShowPreloadedAdMobInterstitial }}" class="button button-admob"/>
192192

193-
<Button row="37" colSpan="2" text="hide banner" tap="{{ doHideAdMobBanner }}" class="button button-admob"/>
193+
<Button row="37" col="0" text="preload rewarded ad" tap="{{ doPreloadRewardedVideoAd }}" class="button button-admob"/>
194+
<Button row="37" col="1" text="show rewarded ad" tap="{{ doShowPreloadedRewardedVideoAd }}" class="button button-admob"/>
194195

195-
<Label row="38" colSpan="2" text="Firebase Cloud Messaging" class="subtitle"/>
196+
<Button row="38" colSpan="2" text="hide banner" tap="{{ doHideAdMobBanner }}" class="button button-admob"/>
196197

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"/>
198+
<Label row="39" colSpan="2" text="Firebase Cloud Messaging" class="subtitle"/>
199199

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"/>
200+
<Button row="40" col="0" text="add push handlers" tap="{{ messaging.doRegisterPushHandlers }}" class="button button-messaging"/>
201+
<Button row="40" col="1" text="get current token" tap="{{ messaging.doGetCurrentPushToken }}" class="button button-messaging"/>
202+
203+
<Button row="41" col="0" text="topic subscribe" tap="{{ messaging.doSubscribeToTopic }}" class="button button-messaging"/>
204+
<Button row="41" col="1" text="topic unsubscribe" tap="{{ messaging.doUnsubscribeFromTopic }}" class="button button-messaging"/>
202205
<iOS>
203-
<Button row="41" colSpan="2" text="register interactive push" tap="{{ messaging.doRegisterForInteractivePush }}" class="button button-messaging"/>
206+
<Button row="42" colSpan="2" text="register interactive push" tap="{{ messaging.doRegisterForInteractivePush }}" class="button button-messaging"/>
204207
</iOS>
205-
<Button row="42" colSpan="2" text="are notifications enabled" tap="{{ messaging.doGetAreNotificationsEnabled }}" class="button button-messaging"/>
208+
<Button row="43" colSpan="2" text="are notifications enabled" tap="{{ messaging.doGetAreNotificationsEnabled }}" class="button button-messaging"/>
206209
<!-- <Button row="39" col="1" text="topic unsubscribe" tap="{{ doUnsubscribeFromTopic }}" class="button button-messaging"/> -->
207210

208-
<Label row="43" colSpan="2" text="Firebase Crash Reporing / Crashlytics" class="subtitle"/>
211+
<Label row="44" colSpan="2" text="Firebase Crash Reporing / Crashlytics" class="subtitle"/>
209212

210-
<Button row="44" col="0" text="log message" tap="{{ doLogMessage }}" class="button button-crash"/>
213+
<Button row="45" col="0" text="log message" tap="{{ doLogMessage }}" class="button button-crash"/>
211214
<iOS>
212-
<Button row="44" col="1" text="force crash :)" tap="{{ doForceCrashIOS }}" class="button button-crash"/>
215+
<Button row="45" col="1" text="force crash :)" tap="{{ doForceCrashIOS }}" class="button button-crash"/>
213216
</iOS>
214217
<Android>
215-
<Button row="44" col="1" text="force crash :)" tap="{{ doForceCrashAndroid }}" class="button button-crash"/>
218+
<Button row="45" col="1" text="force crash :)" tap="{{ doForceCrashAndroid }}" class="button button-crash"/>
216219
</Android>
217-
<Button row="45" col="0" text="log message crashlytics" tap="{{ doLogMessageCrashlytics }}" class="button button-crash"/>
218-
<Button row="45" col="1" text="set string crashlytics" tap="{{ doSetCrashlyticString }}" class="button button-crash"/>
219-
<Button row="46" col="0" text="set bool crashlytics" tap="{{ doSetCrashlyticBool }}" class="button button-crash"/>
220-
<Button row="46" col="1" text="set float crashlytics" tap="{{ doSetCrashlyticFloat }}" class="button button-crash"/>
221-
<Button row="47" col="0" text="set double crashlytics" tap="{{ doSetCrashlyticDouble }}" class="button button-crash"/>
222-
<Button row="47" col="1" text="set int crashlytics" tap="{{ doSetCrashlyticInt }}" class="button button-crash"/>
223-
<Button row="48" colSpan="2" text="SetUserId" tap="{{ doSetUserId }}" class="button button-crash"/>
224-
225-
<Label row="49" colSpan="2" text="Firebase Invites" class="subtitle"/>
226-
<Button row="50" col="0" text="send invitation" tap="{{ sendInvitation }}" class="button button-invites"/>
227-
<Button row="50" col="1" text="get invitation" tap="{{ getInvitation }}" class="button button-invites"/>
228-
229-
<Label row="51" colSpan="2" text="Performance Monitoring" class="subtitle"/>
230-
<Button row="52" col="0" text="start trace" tap="{{ startPerformanceMonitoringTrace }}" class="button button-performance"/>
231-
<Button row="52" col="1" text="stop trace" tap="{{ stopPerformanceMonitoringTrace }}" class="button button-performance"/>
232-
<Button row="53" col="0" text="set value trace" tap="{{ setValuePerformanceMonitoring }}" class="button button-performance"/>
233-
<Button row="53" col="1" text="get value" tap="{{ getValuePerformanceMonitoring }}" class="button button-performance"/>
234-
<Button row="54" col="0" text="get attributes" tap="{{ getAttributesPerformanceMonitoring }}" class="button button-performance"/>
235-
<Button row="54" col="1" text="removeAttribute" tap="{{ removeAttributePerformanceMonitoring }}" class="button button-performance"/>
236-
<Button row="55" colSpan="2" text="increment metric" tap="{{ incrementMetricPerformanceMonitoring }}" class="button button-performance"/>
220+
<Button row="46" col="0" text="log message crashlytics" tap="{{ doLogMessageCrashlytics }}" class="button button-crash"/>
221+
<Button row="46" col="1" text="set string crashlytics" tap="{{ doSetCrashlyticString }}" class="button button-crash"/>
222+
<Button row="47" col="0" text="set bool crashlytics" tap="{{ doSetCrashlyticBool }}" class="button button-crash"/>
223+
<Button row="47" col="1" text="set float crashlytics" tap="{{ doSetCrashlyticFloat }}" class="button button-crash"/>
224+
<Button row="48" col="0" text="set double crashlytics" tap="{{ doSetCrashlyticDouble }}" class="button button-crash"/>
225+
<Button row="48" col="1" text="set int crashlytics" tap="{{ doSetCrashlyticInt }}" class="button button-crash"/>
226+
<Button row="49" colSpan="2" text="SetUserId" tap="{{ doSetUserId }}" class="button button-crash"/>
227+
228+
<Label row="50" colSpan="2" text="Firebase Invites" class="subtitle"/>
229+
<Button row="51" col="0" text="send invitation" tap="{{ sendInvitation }}" class="button button-invites"/>
230+
<Button row="51" col="1" text="get invitation" tap="{{ getInvitation }}" class="button button-invites"/>
231+
232+
<Label row="52" colSpan="2" text="Performance Monitoring" class="subtitle"/>
233+
<Button row="53" col="0" text="start trace" tap="{{ startPerformanceMonitoringTrace }}" class="button button-performance"/>
234+
<Button row="53" col="1" text="stop trace" tap="{{ stopPerformanceMonitoringTrace }}" class="button button-performance"/>
235+
<Button row="54" col="0" text="set value trace" tap="{{ setValuePerformanceMonitoring }}" class="button button-performance"/>
236+
<Button row="54" col="1" text="get value" tap="{{ getValuePerformanceMonitoring }}" class="button button-performance"/>
237+
<Button row="55" col="0" text="get attributes" tap="{{ getAttributesPerformanceMonitoring }}" class="button button-performance"/>
238+
<Button row="55" col="1" text="removeAttribute" tap="{{ removeAttributePerformanceMonitoring }}" class="button button-performance"/>
239+
<Button row="56" colSpan="2" text="increment metric" tap="{{ incrementMetricPerformanceMonitoring }}" class="button button-performance"/>
237240
</GridLayout>
238241

239242
</ScrollView>

demo/app/main-view-model.ts

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,7 @@ export class HelloWorldModel extends Observable {
634634
public doShowAdMobInterstitial(): void {
635635
firebase.admob.showInterstitial({
636636
iosInterstitialId: "ca-app-pub-9517346003011652/6938836122",
637-
androidInterstitialId: "ca-app-pub-9517346003011652/6938836122",
637+
androidInterstitialId: "ca-app-pub-9517346003011652/9225834529",
638638
testing: true,
639639
// Android automatically adds the connected device as test device with testing:true, iOS does not
640640
iosTestDeviceIds: [
@@ -659,7 +659,7 @@ export class HelloWorldModel extends Observable {
659659
public doPreloadAdMobInterstitial(): void {
660660
firebaseAdMob.preloadInterstitial({
661661
iosInterstitialId: "ca-app-pub-9517346003011652/6938836122",
662-
androidInterstitialId: "ca-app-pub-9517346003011652/6938836122",
662+
androidInterstitialId: "ca-app-pub-9517346003011652/9225834529",
663663
testing: true,
664664
// Android automatically adds the connected device as test device with testing:true, iOS does not
665665
iosTestDeviceIds: [
@@ -692,6 +692,52 @@ export class HelloWorldModel extends Observable {
692692
);
693693
}
694694

695+
public doPreloadRewardedVideoAd(): void {
696+
firebaseAdMob.preloadRewardedVideoAd({
697+
iosAdPlacementId: "ca-app-pub-9517346003011652/8586553377",
698+
androidAdPlacementId: "ca-app-pub-9517346003011652/2819097664",
699+
testing: true,
700+
// Android automatically adds the connected device as test device with testing:true, iOS does not
701+
iosTestDeviceIds: [
702+
"45d77bf513dfabc2949ba053da83c0c7b7e87715", // Eddy's iPhone 6s
703+
"fee4cf319a242eab4701543e4c16db89c722731f" // Eddy's iPad Pro
704+
],
705+
keywords: [
706+
"foo",
707+
"bar"
708+
]
709+
}).then(
710+
() => console.log("AdMob rewarded video ad preloaded"),
711+
errorMessage => {
712+
alert({
713+
title: "AdMob error",
714+
message: errorMessage,
715+
okButtonText: "Hmmkay"
716+
});
717+
}
718+
);
719+
}
720+
721+
public doShowPreloadedRewardedVideoAd(): void {
722+
firebaseAdMob.showRewardedVideoAd({
723+
onRewarded: reward => console.log("Rewarded video ad: rewarded. Details: " + JSON.stringify(reward)),
724+
onOpened: () => console.log("Rewarded video ad: opened"),
725+
onStarted: () => console.log("Rewarded video ad: started"),
726+
onCompleted: () => console.log("Rewarded video ad: completed"),
727+
onClosed: () => console.log("Rewarded video ad: closed"),
728+
onLeftApplication: () => console.log("Rewarded video ad: left application")
729+
}).then(
730+
() => console.log("AdMob rewarded video ad showing"),
731+
errorMessage => {
732+
alert({
733+
title: "AdMob error",
734+
message: errorMessage,
735+
okButtonText: "Hmmkay"
736+
});
737+
}
738+
)
739+
}
740+
695741
/**
696742
* Note that an interstitial is supposed to be hidden by clicking the close button,
697743
* so there's no function to do it programmatically.

docs/ADMOB.md

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
<img src="https://raw.githubusercontent.com/EddyVerbruggen/nativescript-plugin-firebase/master/docs/images/features/admob.png" height="85px" alt="AdMob"/>
22

3-
## Enabling AdMob
4-
Since plugin version 3.10.0 you can use Firebase _AdMob_ features.
3+
<img src="https://raw.githubusercontent.com/EddyVerbruggen/nativescript-plugin-firebase/master/docs/images/admob-types.png" height="262px" alt="AdMob Ad Types"/>
4+
5+
_AdMob currently supports these three types of Ads, as does this plugin_
56

6-
_AdMob_ lets you show banners or interstitials (full screen ads) in your app so you can earn some money.
7+
## Enabling AdMob
78

89
### Android
910
> ⚠️ Important! Plugin version 7.4.0+ requires you to do this - or your app will crash on start-up! ⚠️
@@ -143,8 +144,56 @@ After the preload Promise resolved successfully, you can show the interstitial a
143144
);
144145
```
145146

147+
### preloadRewardedVideoAd
148+
Use this for instance while loading your view, so it's ready for the moment you want to actually show it (by calling `showRewardedVideoAd`).
149+
150+
```js
151+
firebase.admob.preloadRewardedVideoAd({
152+
testing: true,
153+
iosAdPlacementId: "ca-app-pub-XXXXXX/YYYYY2", // add your own
154+
androidAdPlacementId: "ca-app-pub-AAAAAAAA/BBBBBB2", // add your own
155+
keywords: ["keyword1", "keyword2"], // add keywords for ad targeting
156+
}).then(
157+
function() {
158+
console.log("RewardedVideoAd preloaded - you can now call 'showRewardedVideoAd' whenever you're ready to do so");
159+
},
160+
function(error) {
161+
console.log("admob preloadRewardedVideoAd error: " + error);
162+
}
163+
)
164+
```
165+
166+
### showRewardedVideoAd
167+
At any moment after `preloadRewardedVideoAd` successfully resolves, you can call `showRewardedVideoAd`.
168+
169+
Note that when you want to use `showRewardedVideoAd` again, you also have to use `preloadRewardedVideoAd` again because those ads can't be reused.
170+
171+
`onRewarded` is probably the only callback you need to worry about.
172+
173+
```js
174+
firebase.admob.showRewardedVideoAd({
175+
onRewarded: (reward) => {
176+
// the properties 'amount' and 'type' correlate to the values set at https://apps.admob.com
177+
console.log("onRewarded called with amount " + reward.amount);
178+
console.log("onRewarded called with type " + reward.type);
179+
},
180+
onLeftApplication: () => console.log("onLeftApplication"),
181+
onClosed: () => console.log("onClosed"),
182+
onOpened: () => console.log("onOpened"),
183+
onStarted: () => console.log("onStarted"),
184+
onCompleted: () => console.log("onCompleted"),
185+
}).then(
186+
function() {
187+
console.log("RewardedVideoAd showing");
188+
},
189+
function(error) {
190+
console.log("showRewardedVideoAd error: " + error);
191+
}
192+
)
193+
```
194+
146195
## What about the nativescript-admob plugin?
147-
There's currently no functional difference between the AdMob features in the Firebase plugin and
196+
There's no functional difference between the AdMob features in the Firebase plugin and
148197
[nativescript-admob](https://github.com/EddyVerbruggen/nativescript-admob).
149198

150199
The main advantage of using the version in the Firebase plugin is to avoid a gradle build conflict

docs/images/admob-types.png

128 KB
Loading

src/admob/admob-common.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { RewardedVideoAdCallbacks, RewardedVideoAdReward } from "./admob";
2+
13
export const AD_SIZE = {
24
SMART_BANNER: "SMART",
35
LARGE_BANNER: "LARGE",
@@ -18,3 +20,20 @@ export const BANNER_DEFAULTS = {
1820
size: "SMART",
1921
view: undefined
2022
};
23+
24+
export const rewardedVideoCallbacks: RewardedVideoAdCallbacks = {
25+
onRewarded: (reward: RewardedVideoAdReward) => console.warn("onRewarded callback not set - the fallback implementation caught this reward: " + JSON.stringify(reward)),
26+
onLeftApplication: () => {
27+
},
28+
onClosed: () => {
29+
},
30+
onOpened: () => {
31+
},
32+
onStarted: () => {
33+
},
34+
onCompleted: () => {
35+
},
36+
onLoaded: () => {
37+
},
38+
onFailedToLoad: (err) => console.warn("onFailedToLoad not set - the fallback implementation caught this error: " + err),
39+
};

0 commit comments

Comments
 (0)