Skip to content

Commit e04076c

Browse files
committed
feat: admob , firestore & messaging
1 parent b7bf5eb commit e04076c

File tree

234 files changed

+33227
-5183
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

234 files changed

+33227
-5183
lines changed

apps/demo/nativescript.config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { NativeScriptConfig } from '@nativescript/core';
22

33
export default {
4-
id: 'org.nativescript.firebasedemo',
4+
//id: 'org.nativescript.firebasedemo',
5+
id: 'io.github.triniwiz.nativescript.firebasedemo',
56
appResourcesPath: '../../tools/assets/App_Resources',
67
android: {
78
v8Flags: '--expose_gc',

apps/demo/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"@nativescript/firebase-messaging": "file:../../packages/firebase-messaging"
2323
},
2424
"devDependencies": {
25-
"@nativescript/android": "8.0.0",
25+
"@nativescript/android": "8.1.0",
2626
"@nativescript/ios": "8.0.0"
2727
}
2828
}

apps/demo/src/app.ts

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,46 @@
1-
import { Application } from '@nativescript/core';
2-
import { Firebase, FirebaseOptions } from '@nativescript/firebase-core';
1+
import {Application} from '@nativescript/core';
2+
import {Firebase, FirebaseOptions} from '@nativescript/firebase-core';
3+
import '@nativescript/firebase-messaging';
4+
import '@nativescript/firebase-admob';
5+
import '@nativescript/firebase-dynamic-links';
6+
import '@nativescript/firebase-firestore';
7+
8+
import {Messaging} from '@nativescript/firebase-messaging';
9+
import {Admob} from '@nativescript/firebase-admob';
10+
import {DynamicLinks} from '@nativescript/firebase-dynamic-links';
11+
312
Firebase.initializeApp(new FirebaseOptions());
4-
Application.run({ moduleName: 'app-root' });
13+
Admob.init();
14+
15+
const dynamicLinks = Firebase.dynamicLinks() as DynamicLinks;
16+
17+
dynamicLinks.onLink((link) => {
18+
console.log('onLink', link);
19+
});
20+
21+
const messaging = Firebase.messaging() as Messaging;
22+
messaging.onMessage((message) => {
23+
console.log('Firebase onMessage', message);
24+
});
25+
26+
messaging.onNotificationTap((message) => {
27+
console.log('Firebase onNotificationTap', message);
28+
});
29+
30+
messaging.onToken((token) => {
31+
console.log('Firebase onToken', token);
32+
});
33+
34+
messaging
35+
.requestPermission()
36+
.then(() => {
37+
console.log('requestPermission', 'done');
38+
messaging.registerDeviceForRemoteMessages().catch((e) => {
39+
console.error('registerDeviceForRemoteMessages', e);
40+
});
41+
})
42+
.catch((e) => {
43+
console.error('requestPermission', e);
44+
});
45+
46+
Application.run({moduleName: 'app-root'});

apps/demo/src/main-view-model.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ export class MainViewModel extends Observable {
3535
{
3636
name: 'firebase-storage',
3737
},
38+
{
39+
name: 'firebase-messaging',
40+
},
41+
{
42+
name: 'firebase-firestore',
43+
},
3844
];
3945

4046
viewDemo(args) {
Lines changed: 109 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,119 @@
11
import { Observable, EventData, Page } from '@nativescript/core';
22
import { DemoSharedFirebaseAdmob } from '@demo/shared';
3-
import { } from '@nativescript/firebase-admob';
3+
import { Firebase } from '@nativescript/firebase-core';
4+
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';
46

57
export function navigatingTo(args: EventData) {
68
const page = <Page>args.object;
79
page.bindingContext = new DemoModel();
810
}
911

1012
export class DemoModel extends DemoSharedFirebaseAdmob {
11-
13+
get bannerAdUnit() {
14+
if (global.isAndroid) {
15+
return 'ca-app-pub-3940256099942544/6300978111';
16+
} else {
17+
return 'ca-app-pub-3940256099942544/2934735716';
18+
}
19+
}
20+
21+
admob: Admob;
22+
constructor() {
23+
super();
24+
//this.interstitial();
25+
//this.rewardedInterstitial();
26+
//this.rewarded();
27+
this.admob = Firebase.admob();
28+
// ATTrackingManager.requestTrackingAuthorizationWithCompletionHandler((status) => {
29+
// console.log('ATTrackingManager', status);
30+
// });
31+
32+
const testDevices = [];
33+
if (global.isIOS) {
34+
testDevices.push('b70c144077402b91cf8ecc5b93ac7791');
35+
} else {
36+
testDevices.push('EMULATOR');
37+
}
38+
this.admob.setRequestConfiguration({
39+
testDevices,
40+
});
41+
}
42+
43+
nativeAdLoaded(event) {
44+
const view = event.object;
45+
const loader = new NativeAdLoader('ca-app-pub-3940256099942544/3986624511', null , {
46+
nativeAdOptions: {
47+
adChoicesPlacement: AdChoicesPlacement.TOP_RIGHT
48+
}
49+
});
50+
loader.onAdEvent((event, error, data) => {
51+
if (event === NativeAdEventType.LOADED) {
52+
const ad = data as NativeAd;
53+
const hlv = view.getViewById('headLineView');
54+
hlv.text = ad.headline;
55+
const mv = view.getViewById('mediaView');
56+
mv.mediaContent = ad.mediaContent;
57+
const bv = view.getViewById('bodyView');
58+
bv.text = ad.body;
59+
view.nativeAd = ad;
60+
console.log('nativead loaded');
61+
} else if (event === 'adFailedToLoad') {
62+
console.log('nativead failed to load', error);
63+
}
64+
});
65+
loader.load();
66+
}
67+
68+
bannerLoaded(event) {
69+
const bannerAd = event.object as BannerAd;
70+
71+
bannerAd.on('adLoaded', (args) => {
72+
console.log('bannerad loaded');
73+
});
74+
bannerAd.on('adFailedToLoad', (args) => {
75+
console.log('bannerad failed to load', args.error);
76+
});
77+
bannerAd.size = BannerAdSize.createInLineAdaptiveBanner(100);
78+
bannerAd.load();
79+
}
80+
81+
interstitial() {
82+
const ad = InterstitialAd.createForAdRequest('ca-app-pub-3940256099942544/4411468910');
83+
ad.onAdEvent((event, error, data) => {
84+
if (event === AdEventType.LOADED) {
85+
console.log('loaded');
86+
ad.show();
87+
} else if (event === AdEventType.FAILED_TO_LOAD_EVENT) {
88+
console.error('loading error', error);
89+
}
90+
});
91+
ad.load();
92+
}
93+
94+
rewardedInterstitial() {
95+
const ad = RewardedInterstitialAd.createForAdRequest('ca-app-pub-3940256099942544/6978759866');
96+
ad.onAdEvent((event, error, data) => {
97+
if (event === AdEventType.LOADED) {
98+
console.log('loaded');
99+
ad.show();
100+
} else if (event === AdEventType.FAILED_TO_LOAD_EVENT) {
101+
console.error('loading error', error);
102+
}
103+
});
104+
ad.load();
105+
}
106+
107+
rewarded() {
108+
const ad = RewardedAd.createForAdRequest('ca-app-pub-3940256099942544/1712485313');
109+
ad.onAdEvent((event, error, data) => {
110+
if (event === AdEventType.LOADED) {
111+
console.log('rewarded', 'loaded');
112+
ad.show();
113+
} else if (event === AdEventType.FAILED_TO_LOAD_EVENT) {
114+
console.error('loading error', error);
115+
}
116+
});
117+
ad.load();
118+
}
12119
}
Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
1-
<Page xmlns="http://schemas.nativescript.org/tns.xsd" navigatingTo="navigatingTo" class="page">
1+
<Page xmlns="http://schemas.nativescript.org/tns.xsd" navigatingTo="navigatingTo" class="page"
2+
xmlns:ui="@nativescript/firebase-admob"
3+
>
24
<Page.actionBar>
35
<ActionBar title="firebase-admob" icon="" class="action-bar">
46
</ActionBar>
57
</Page.actionBar>
68
<StackLayout class="p-20">
7-
<ScrollView class="h-full">
8-
<StackLayout>
9-
<Button text="Test firebase-admob" tap="{{ testIt }}" class="btn btn-primary"/>
10-
11-
</StackLayout>
12-
</ScrollView>
9+
<!-- <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"/>
15+
</GridLayout>
16+
</ui:NativeAdView>
1317
</StackLayout>
1418
</Page>

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,14 @@ export class DemoModel extends DemoSharedFirebaseCrashlytics {
3737
}
3838

3939
this.crashlytics.sendUnsentReports();
40-
this.crashlytics.checkForUnsentReports()
41-
.then(val =>{
42-
console.log('checkForUnsentReports',val);
43-
})
44-
.catch(e =>{
45-
console.error('checkForUnsentReports', e);
46-
})
40+
this.crashlytics
41+
.checkForUnsentReports()
42+
.then((val) => {
43+
console.log('checkForUnsentReports', val);
44+
})
45+
.catch((e) => {
46+
console.error('checkForUnsentReports', e);
47+
});
4748
}
4849

4950
crash() {

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import { Observable, EventData, Page } from '@nativescript/core';
22
import { DemoSharedFirebaseDatabase } from '@demo/shared';
33
import { Firebase } from '@nativescript/firebase-core';
4-
import '@nativescript/firebase-auth';
5-
import '@nativescript/firebase-database';
64
import { Database } from '@nativescript/firebase-database';
75
import { Auth } from '@nativescript/firebase-auth';
86
export function navigatingTo(args: EventData) {
Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,41 @@
1-
import { Observable, EventData, Page } from '@nativescript/core';
2-
import { DemoSharedFirebaseFirestore } from '@demo/shared';
3-
import { } from '@nativescript/firebase-firestore';
1+
import {Observable, EventData, Page} from '@nativescript/core';
2+
import {DemoSharedFirebaseFirestore} from '@demo/shared';
3+
import {Firestore, GeoPoint} from '@nativescript/firebase-firestore';
4+
import {Firebase} from "@nativescript/firebase-core";
45

56
export function navigatingTo(args: EventData) {
6-
const page = <Page>args.object;
7-
page.bindingContext = new DemoModel();
7+
const page = <Page>args.object;
8+
page.bindingContext = new DemoModel();
89
}
910

1011
export class DemoModel extends DemoSharedFirebaseFirestore {
11-
12+
firestore: Firestore;
13+
14+
constructor() {
15+
super();
16+
this.firestore = Firebase.firestore();
17+
this.firestore.collection('users')
18+
.add({
19+
first: "Ada",
20+
last: "Lovelace",
21+
born: 1815
22+
}).then((docRef) => {
23+
console.log("Document written with ID: ", docRef.id);
24+
})
25+
.catch((error) => {
26+
console.error("Error adding document: ", error);
27+
});
28+
29+
const geo = new GeoPoint(10, -10);
30+
this.firestore.collection('geo')
31+
.add({
32+
thing: 'it',
33+
geo
34+
}).then((docRef) => {
35+
console.log("Geo Document written with ID: ", docRef.id);
36+
})
37+
.catch((error) => {
38+
console.error("Error adding geo document: ", error);
39+
});
40+
}
1241
}
Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
import { Observable, EventData, Page } from '@nativescript/core';
22
import { DemoSharedFirebaseMessaging } from '@demo/shared';
3-
import { } from '@nativescript/firebase-messaging';
3+
import { Firebase } from '@nativescript/firebase-core';
4+
import { Messaging } from '@nativescript/firebase-messaging';
45

56
export function navigatingTo(args: EventData) {
67
const page = <Page>args.object;
78
page.bindingContext = new DemoModel();
89
}
910

1011
export class DemoModel extends DemoSharedFirebaseMessaging {
11-
12+
constructor(){
13+
super();
14+
(<Messaging>Firebase.messaging()).getToken().then(token =>{
15+
console.log('getToken', token);
16+
}).catch(e =>{
17+
console.log('getToken', e);
18+
})
19+
}
1220
}

0 commit comments

Comments
 (0)