Skip to content

Commit 26f7613

Browse files
authored
feat(messaging): AngularFireMessaing (#1749)
* feat(messaging): AngularFireMessaing * Zone.js and DI on top of this work, also fixing a couple small things * Cleanup the build
1 parent f2bfda9 commit 26f7613

14 files changed

+218
-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: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { NgModule } from '@angular/core';
2+
import { AngularFireModule, FirebaseApp } from 'angularfire2';
3+
import { AngularFireMessaging } from './messaging';
4+
import 'firebase/messaging';
5+
6+
@NgModule({
7+
providers: [ AngularFireMessaging ]
8+
})
9+
export class AngularFireMessagingModule { }

src/messaging/messaging.ts

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import { Injectable, Inject, Optional, NgZone, PLATFORM_ID } from '@angular/core';
2+
import { isPlatformBrowser } from '@angular/common';
3+
import { messaging } from 'firebase';
4+
import { requestPermission } from './observable/request-permission';
5+
import { Observable, empty, from, of, throwError } from 'rxjs';
6+
import { mergeMap, catchError } from 'rxjs/operators';
7+
import { FirebaseOptions, FirebaseAppConfig } from 'angularfire2';
8+
import { FirebaseOptionsToken, FirebaseNameOrConfigToken, _firebaseAppFactory, FirebaseZoneScheduler } from 'angularfire2';
9+
10+
@Injectable()
11+
export class AngularFireMessaging {
12+
messaging: messaging.Messaging;
13+
requestPermission: Observable<void>;
14+
getToken: Observable<string|null>;
15+
tokenChanges: Observable<string|null>;
16+
messages: Observable<{}>;
17+
requestToken: Observable<string|null>;
18+
deleteToken: (string) => Observable<boolean>;
19+
20+
constructor(
21+
@Inject(FirebaseOptionsToken) options:FirebaseOptions,
22+
@Optional() @Inject(FirebaseNameOrConfigToken) nameOrConfig:string|FirebaseAppConfig|undefined,
23+
@Inject(PLATFORM_ID) platformId: Object,
24+
zone: NgZone
25+
) {
26+
const scheduler = new FirebaseZoneScheduler(zone, platformId);
27+
this.messaging = zone.runOutsideAngular(() => {
28+
const app = _firebaseAppFactory(options, nameOrConfig);
29+
return app.messaging();
30+
});
31+
32+
if (isPlatformBrowser(platformId)) {
33+
34+
this.requestPermission = scheduler.runOutsideAngular(
35+
requestPermission(this.messaging)
36+
);
37+
38+
this.getToken = scheduler.runOutsideAngular(
39+
from(this.messaging.getToken())
40+
);
41+
42+
this.tokenChanges = scheduler.runOutsideAngular(
43+
new Observable(subscriber => {
44+
this.messaging.getToken().then(t => subscriber.next(t));
45+
this.messaging.onTokenRefresh(subscriber.next);
46+
})
47+
);
48+
49+
this.messages = scheduler.runOutsideAngular(
50+
new Observable(subscriber => {
51+
this.messaging.onMessage(subscriber.next);
52+
})
53+
);
54+
55+
this.requestToken = this.requestPermission.pipe(
56+
catchError(() => of(null)),
57+
mergeMap(() => this.tokenChanges),
58+
);
59+
60+
} else {
61+
62+
this.requestPermission = throwError('Not available on server platform.');
63+
this.getToken = of(null);
64+
this.tokenChanges = of(null);
65+
this.messages = empty();
66+
this.requestToken = of(null);
67+
68+
}
69+
70+
this.deleteToken = (token: string) => scheduler.runOutsideAngular(
71+
from(this.messaging.deleteToken(token))
72+
);
73+
}
74+
75+
}
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+
}

0 commit comments

Comments
 (0)