Skip to content

Commit 14fcef6

Browse files
committed
feat(messaging): AngularFireMessaing
1 parent 3152fc4 commit 14fcef6

14 files changed

+201
-6
lines changed

src/messaging/index.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './messaging.spec';

src/messaging/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './public_api';

src/messaging/messaging.module.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { NgModule } from '@angular/core';
2+
import { AngularFireModule, FirebaseApp } from 'angularfire2';
3+
import { AngularFireMessaging } from './messaging';
4+
import 'firebase/messaging';
5+
6+
export function _getAngularFireMessaging(app: FirebaseApp) {
7+
return new AngularFireMessaging(app);
8+
}
9+
10+
export const AngularFireMessagingProvider = {
11+
provide: AngularFireMessaging,
12+
useFactory: _getAngularFireMessaging,
13+
deps: [ FirebaseApp ]
14+
};
15+
16+
export const STORAGE_PROVIDERS = [
17+
AngularFireMessagingProvider,
18+
];
19+
20+
@NgModule({
21+
imports: [ AngularFireModule ],
22+
providers: [ STORAGE_PROVIDERS ]
23+
})
24+
export class AngularFireMessagingModule { }

src/messaging/messaging.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { Injectable } from '@angular/core';
2+
import { Observable } from 'rxjs';
3+
import { FirebaseApp } from 'angularfire2';
4+
import { messaging } from 'firebase';
5+
import { requestPermission } from './observable/request-permission';
6+
import { from } from 'rxjs';
7+
import { mergeMap } from 'rxjs/operators';
8+
9+
@Injectable()
10+
export class AngularFireMessaging {
11+
messaging: messaging.Messaging;
12+
requestPermission: Observable<void>;
13+
getToken: Observable<string|null>;
14+
tokenChanges: Observable<string|null>;
15+
messages: Observable<{}>;
16+
requestToken: Observable<string|null>;
17+
18+
constructor(public app: FirebaseApp) {
19+
this.messaging = app.messaging();
20+
21+
this.requestPermission = requestPermission(this.messaging);
22+
23+
this.getToken = from(this.messaging.getToken());
24+
25+
this.tokenChanges = new Observable(subscriber => {
26+
this.messaging.getToken().then(t => subscriber.next(t));
27+
this.messaging.onTokenRefresh(subscriber);
28+
});
29+
30+
this.messages = new Observable(subscriber => {
31+
this.messaging.onMessage(subscriber);
32+
});
33+
34+
this.requestToken = this.requestPermission.pipe(
35+
mergeMap(() => this.tokenChanges)
36+
);
37+
38+
}
39+
40+
deleteToken(token: string) {
41+
return from(this.messaging.deleteToken(token)!);
42+
}
43+
44+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { Observable, from } from 'rxjs';
2+
import { messaging } from 'firebase';
3+
4+
export function requestPermission(messaging: messaging.Messaging): Observable<void> {
5+
return from(messaging.requestPermission()!);
6+
}

src/messaging/package.json

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"name": "angularfire2/messaging",
3+
"version": "ANGULARFIRE2_VERSION",
4+
"description": "The messaging module",
5+
"main": "../bundles/messaging.umd.js",
6+
"module": "index.js",
7+
"es2015": "./es2015/index.js",
8+
"keywords": [
9+
"angular",
10+
"firebase",
11+
"rxjs"
12+
],
13+
"repository": {
14+
"type": "git",
15+
"url": "git+https://github.com/angular/angularfire2.git"
16+
},
17+
"author": "angular,firebase",
18+
"license": "MIT",
19+
"peerDependencies": {
20+
"angularfire2": "ANGULARFIRE2_VERSION",
21+
"@angular/common": "ANGULAR_VERSION",
22+
"@angular/core": "ANGULAR_VERSION",
23+
"@angular/platform-browser": "ANGULAR_VERSION",
24+
"@angular/platform-browser-dynamic": "ANGULAR_VERSION",
25+
"@firebase/app": "FIREBASE_APP_VERSION",
26+
"@firebase/messaging": "FIREBASE_MESSAGING_VERSION",
27+
"rxjs": "RXJS_VERSION",
28+
"zone.js": "ZONEJS_VERSION"
29+
},
30+
"typings": "index.d.ts"
31+
}

src/messaging/public_api.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export * from './observable/request-permission';
2+
export * from './messaging';
3+
export * from './messaging.module';

src/messaging/test-config.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
export const COMMON_CONFIG = {
3+
apiKey: "AIzaSyBVSy3YpkVGiKXbbxeK0qBnu3-MNZ9UIjA",
4+
authDomain: "angularfire2-test.firebaseapp.com",
5+
databaseURL: "https://angularfire2-test.firebaseio.com",
6+
storageBucket: "angularfire2-test.appspot.com",
7+
messagingSenderId: "920323787688"
8+
};

src/messaging/tsconfig-build.json

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"compilerOptions": {
3+
"baseUrl": ".",
4+
"experimentalDecorators": true,
5+
"emitDecoratorMetadata": true,
6+
"module": "es2015",
7+
"target": "es2015",
8+
"noImplicitAny": false,
9+
"outDir": "../../dist/packages-dist/messaging/es2015",
10+
"rootDir": ".",
11+
"sourceMap": true,
12+
"inlineSources": true,
13+
"declaration": false,
14+
"removeComments": true,
15+
"strictNullChecks": true,
16+
"lib": ["es2015", "dom", "es2015.promise", "es2015.collection", "es2015.iterable"],
17+
"skipLibCheck": true,
18+
"moduleResolution": "node",
19+
"paths": {
20+
"angularfire2": ["../../dist/packages-dist"]
21+
}
22+
},
23+
"files": [
24+
"index.ts",
25+
"../../node_modules/zone.js/dist/zone.js.d.ts"
26+
],
27+
"angularCompilerOptions": {
28+
"skipTemplateCodegen": true,
29+
"strictMetadataEmit": true,
30+
"enableSummariesForJit": false
31+
}
32+
}
33+

src/messaging/tsconfig-esm.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"extends": "./tsconfig-build.json",
3+
"compilerOptions": {
4+
"target": "es5",
5+
"outDir": "../../dist/packages-dist/messaging",
6+
"declaration": true
7+
},
8+
"files": [
9+
"public_api.ts",
10+
"../../node_modules/zone.js/dist/zone.js.d.ts"
11+
],
12+
"angularCompilerOptions": {
13+
"skipTemplateCodegen": true,
14+
"strictMetadataEmit": true,
15+
"enableSummariesForJit": false,
16+
"flatModuleOutFile": "index.js",
17+
"flatModuleId": "angularfire2/messaging"
18+
}
19+
}

src/messaging/tsconfig-test.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"extends": "./tsconfig-esm.json",
3+
"compilerOptions": {
4+
"baseUrl": ".",
5+
"paths": {
6+
"angularfire2": ["../../dist/packages-dist"]
7+
}
8+
},
9+
"files": [
10+
"index.spec.ts",
11+
"../../node_modules/zone.js/dist/zone.js.d.ts"
12+
]
13+
}

src/root.spec.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export * from './packages-dist/database/list/snapshot-changes.spec';
1313
export * from './packages-dist/database/list/state-changes.spec';
1414
export * from './packages-dist/database/list/audit-trail.spec';
1515
export * from './packages-dist/storage/storage.spec';
16+
export * from './packages-dist/messaging/messaging.spec';
1617

1718
// // Since this a deprecated API, we run on it on manual tests only
1819
// // It needs a network connection to run which makes it flaky on Travis

src/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"angularfire2/firestore": ["./firestore"],
1717
"angularfire2/functions": ["./functions"],
1818
"angularfire2/storage": ["./storage"],
19+
"angularfire2/messaging": ["./messaging"],
1920
"angularfire2/database-deprecated": ["./database-deprecated"]
2021
},
2122
"rootDir": ".",

tools/build.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ const GLOBALS = {
2929
'angularfire2/database-deprecated': 'angularfire2.database_deprecated',
3030
'angularfire2/firestore': 'angularfire2.firestore',
3131
'angularfire2/functions': 'angularfire2.functions',
32-
'angularfire2/storage': 'angularfire2.storage'
32+
'angularfire2/storage': 'angularfire2.storage',
33+
'angularfire2/messaging': 'angularfire2.messaging',
3334
};
3435

3536
// Map of dependency versions across all packages
@@ -53,7 +54,8 @@ const MODULE_NAMES = {
5354
"database-deprecated": 'angularfire2.database_deprecated',
5455
firestore: 'angularfire2.firestore',
5556
functions: 'angularfire2.functions',
56-
storage: 'angularfire2.storage'
57+
storage: 'angularfire2.storage',
58+
messaging: 'angularfire2.messaging',
5759
};
5860

5961
const ENTRIES = {
@@ -63,7 +65,8 @@ const ENTRIES = {
6365
"database-deprecated": `${process.cwd()}/dist/packages-dist/database-deprecated/index.js`,
6466
firestore: `${process.cwd()}/dist/packages-dist/firestore/index.js`,
6567
functions: `${process.cwd()}/dist/packages-dist/functions/index.js`,
66-
storage: `${process.cwd()}/dist/packages-dist/storage/index.js`
68+
storage: `${process.cwd()}/dist/packages-dist/storage/index.js`,
69+
messaging: `${process.cwd()}/dist/packages-dist/messaging/index.js`,
6770
};
6871

6972
const SRC_PKG_PATHS = {
@@ -74,7 +77,8 @@ const SRC_PKG_PATHS = {
7477
firestore: `${process.cwd()}/src/firestore/package.json`,
7578
"firebase-node": `${process.cwd()}/src/firebase-node/package.json`,
7679
functions: `${process.cwd()}/src/functions/package.json`,
77-
storage: `${process.cwd()}/src/storage/package.json`
80+
storage: `${process.cwd()}/src/storage/package.json`,
81+
messaging: `${process.cwd()}/src/messaging/package.json`,
7882
};
7983

8084
const DEST_PKG_PATHS = {
@@ -85,7 +89,8 @@ const DEST_PKG_PATHS = {
8589
firestore: `${process.cwd()}/dist/packages-dist/firestore/package.json`,
8690
"firebase-node": `${process.cwd()}/dist/packages-dist/firebase-node/package.json`,
8791
functions: `${process.cwd()}/dist/packages-dist/functions/package.json`,
88-
storage: `${process.cwd()}/dist/packages-dist/storage/package.json`
92+
storage: `${process.cwd()}/dist/packages-dist/storage/package.json`,
93+
messaging: `${process.cwd()}/dist/packages-dist/messaging/package.json`,
8994
};
9095

9196
// Constants for running typescript commands
@@ -246,6 +251,7 @@ function getVersions() {
246251
getDestPackageFile('firebase-node'),
247252
getDestPackageFile('functions'),
248253
getDestPackageFile('storage'),
254+
getDestPackageFile('messaging'),
249255
getDestPackageFile('database-deprecated')
250256
];
251257
return paths
@@ -285,14 +291,16 @@ function buildModules(globals) {
285291
const firestore$ = buildModule('firestore', globals);
286292
const functions$ = buildModule('functions', globals);
287293
const storage$ = buildModule('storage', globals);
294+
const messaging$ = buildModule('messaging', globals);
288295
const dbdep$ = buildModule('database-deprecated', globals);
289296
return forkJoin(core$, from(copyRootTest())).pipe(
290297
switchMapTo(auth$),
291298
switchMapTo(db$),
292299
switchMapTo(firestore$),
293300
switchMapTo(functions$),
294301
switchMapTo(storage$),
295-
switchMapTo(dbdep$)
302+
switchMapTo(messaging$),
303+
switchMapTo(dbdep$),
296304
);
297305
}
298306

@@ -312,6 +320,7 @@ function buildLibrary(globals) {
312320
const fsStats = measure('firestore');
313321
const functionsStats = measure('functions');
314322
const storageStats = measure('storage');
323+
const messagingStats = measure('messaging');
315324
const dbdepStats = measure('database-deprecated');
316325
console.log(`
317326
core.umd.js - ${coreStats.size}, ${coreStats.gzip}
@@ -320,6 +329,7 @@ function buildLibrary(globals) {
320329
firestore.umd.js - ${fsStats.size}, ${fsStats.gzip}
321330
functions.umd.js - ${functionsStats.size}, ${functionsStats.gzip}
322331
storage.umd.js - ${storageStats.size}, ${storageStats.gzip}
332+
messaging.umd.js - ${messagingStats.size}, ${messagingStats.gzip}
323333
database-deprecated.umd.js - ${dbdepStats.size}, ${dbdepStats.gzip}
324334
`);
325335
verifyVersions();

0 commit comments

Comments
 (0)