Skip to content

feat(messaging): AngularFireMessaing #1749

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/messaging/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './messaging.spec';
1 change: 1 addition & 0 deletions src/messaging/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './public_api';
9 changes: 9 additions & 0 deletions src/messaging/messaging.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { NgModule } from '@angular/core';
import { AngularFireModule, FirebaseApp } from 'angularfire2';
import { AngularFireMessaging } from './messaging';
import 'firebase/messaging';

@NgModule({
providers: [ AngularFireMessaging ]
})
export class AngularFireMessagingModule { }
75 changes: 75 additions & 0 deletions src/messaging/messaging.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { Injectable, Inject, Optional, NgZone, PLATFORM_ID } from '@angular/core';
import { isPlatformBrowser } from '@angular/common';
import { messaging } from 'firebase';
import { requestPermission } from './observable/request-permission';
import { Observable, empty, from, of, throwError } from 'rxjs';
import { mergeMap, catchError } from 'rxjs/operators';
import { FirebaseOptions, FirebaseAppConfig } from 'angularfire2';
import { FirebaseOptionsToken, FirebaseNameOrConfigToken, _firebaseAppFactory, FirebaseZoneScheduler } from 'angularfire2';

@Injectable()
export class AngularFireMessaging {
messaging: messaging.Messaging;
requestPermission: Observable<void>;
getToken: Observable<string|null>;
tokenChanges: Observable<string|null>;
messages: Observable<{}>;
requestToken: Observable<string|null>;
deleteToken: (string) => Observable<boolean>;

constructor(
@Inject(FirebaseOptionsToken) options:FirebaseOptions,
@Optional() @Inject(FirebaseNameOrConfigToken) nameOrConfig:string|FirebaseAppConfig|undefined,
@Inject(PLATFORM_ID) platformId: Object,
zone: NgZone
) {
const scheduler = new FirebaseZoneScheduler(zone, platformId);
this.messaging = zone.runOutsideAngular(() => {
const app = _firebaseAppFactory(options, nameOrConfig);
return app.messaging();
});

if (isPlatformBrowser(platformId)) {

this.requestPermission = scheduler.runOutsideAngular(
requestPermission(this.messaging)
);

this.getToken = scheduler.runOutsideAngular(
from(this.messaging.getToken())
);

this.tokenChanges = scheduler.runOutsideAngular(
new Observable(subscriber => {
this.messaging.getToken().then(t => subscriber.next(t));
this.messaging.onTokenRefresh(subscriber.next);
})
);

this.messages = scheduler.runOutsideAngular(
new Observable(subscriber => {
this.messaging.onMessage(subscriber.next);
})
);

this.requestToken = this.requestPermission.pipe(
catchError(() => of(null)),
mergeMap(() => this.tokenChanges),
);

} else {

this.requestPermission = throwError('Not available on server platform.');
this.getToken = of(null);
this.tokenChanges = of(null);
this.messages = empty();
this.requestToken = of(null);

}

this.deleteToken = (token: string) => scheduler.runOutsideAngular(
from(this.messaging.deleteToken(token))
);
}

}
6 changes: 6 additions & 0 deletions src/messaging/observable/request-permission.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { Observable, from } from 'rxjs';
import { messaging } from 'firebase';

export function requestPermission(messaging: messaging.Messaging): Observable<void> {
return from(messaging.requestPermission()!);
}
31 changes: 31 additions & 0 deletions src/messaging/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "angularfire2/messaging",
"version": "ANGULARFIRE2_VERSION",
"description": "The messaging module",
"main": "../bundles/messaging.umd.js",
"module": "index.js",
"es2015": "./es2015/index.js",
"keywords": [
"angular",
"firebase",
"rxjs"
],
"repository": {
"type": "git",
"url": "git+https://github.com/angular/angularfire2.git"
},
"author": "angular,firebase",
"license": "MIT",
"peerDependencies": {
"angularfire2": "ANGULARFIRE2_VERSION",
"@angular/common": "ANGULAR_VERSION",
"@angular/core": "ANGULAR_VERSION",
"@angular/platform-browser": "ANGULAR_VERSION",
"@angular/platform-browser-dynamic": "ANGULAR_VERSION",
"@firebase/app": "FIREBASE_APP_VERSION",
"@firebase/messaging": "FIREBASE_MESSAGING_VERSION",
"rxjs": "RXJS_VERSION",
"zone.js": "ZONEJS_VERSION"
},
"typings": "index.d.ts"
}
3 changes: 3 additions & 0 deletions src/messaging/public_api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './observable/request-permission';
export * from './messaging';
export * from './messaging.module';
8 changes: 8 additions & 0 deletions src/messaging/test-config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

export const COMMON_CONFIG = {
apiKey: "AIzaSyBVSy3YpkVGiKXbbxeK0qBnu3-MNZ9UIjA",
authDomain: "angularfire2-test.firebaseapp.com",
databaseURL: "https://angularfire2-test.firebaseio.com",
storageBucket: "angularfire2-test.appspot.com",
messagingSenderId: "920323787688"
};
33 changes: 33 additions & 0 deletions src/messaging/tsconfig-build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"compilerOptions": {
"baseUrl": ".",
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"module": "es2015",
"target": "es2015",
"noImplicitAny": false,
"outDir": "../../dist/packages-dist/messaging/es2015",
"rootDir": ".",
"sourceMap": true,
"inlineSources": true,
"declaration": false,
"removeComments": true,
"strictNullChecks": true,
"lib": ["es2015", "dom", "es2015.promise", "es2015.collection", "es2015.iterable"],
"skipLibCheck": true,
"moduleResolution": "node",
"paths": {
"angularfire2": ["../../dist/packages-dist"]
}
},
"files": [
"index.ts",
"../../node_modules/zone.js/dist/zone.js.d.ts"
],
"angularCompilerOptions": {
"skipTemplateCodegen": true,
"strictMetadataEmit": true,
"enableSummariesForJit": false
}
}

19 changes: 19 additions & 0 deletions src/messaging/tsconfig-esm.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"extends": "./tsconfig-build.json",
"compilerOptions": {
"target": "es5",
"outDir": "../../dist/packages-dist/messaging",
"declaration": true
},
"files": [
"public_api.ts",
"../../node_modules/zone.js/dist/zone.js.d.ts"
],
"angularCompilerOptions": {
"skipTemplateCodegen": true,
"strictMetadataEmit": true,
"enableSummariesForJit": false,
"flatModuleOutFile": "index.js",
"flatModuleId": "angularfire2/messaging"
}
}
13 changes: 13 additions & 0 deletions src/messaging/tsconfig-test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"extends": "./tsconfig-esm.json",
"compilerOptions": {
"baseUrl": ".",
"paths": {
"angularfire2": ["../../dist/packages-dist"]
}
},
"files": [
"index.spec.ts",
"../../node_modules/zone.js/dist/zone.js.d.ts"
]
}
1 change: 1 addition & 0 deletions src/root.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export * from './packages-dist/database/list/snapshot-changes.spec';
export * from './packages-dist/database/list/state-changes.spec';
export * from './packages-dist/database/list/audit-trail.spec';
export * from './packages-dist/storage/storage.spec';
//export * from './packages-dist/messaging/messaging.spec';

// // Since this a deprecated API, we run on it on manual tests only
// // It needs a network connection to run which makes it flaky on Travis
Expand Down
1 change: 1 addition & 0 deletions src/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"angularfire2/firestore": ["./firestore"],
"angularfire2/functions": ["./functions"],
"angularfire2/storage": ["./storage"],
"angularfire2/messaging": ["./messaging"],
"angularfire2/database-deprecated": ["./database-deprecated"]
},
"rootDir": ".",
Expand Down
23 changes: 17 additions & 6 deletions tools/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const GLOBALS = {
'firebase/app': 'firebase',
'firebase/auth': 'firebase',
'firebase/database': 'firebase',
'firebase/messaging': 'firebase',
'firebase/firestore': 'firebase',
'firebase/functions': 'firebase',
'firebase/storage': 'firebase',
Expand All @@ -29,7 +30,8 @@ const GLOBALS = {
'angularfire2/database-deprecated': 'angularfire2.database_deprecated',
'angularfire2/firestore': 'angularfire2.firestore',
'angularfire2/functions': 'angularfire2.functions',
'angularfire2/storage': 'angularfire2.storage'
'angularfire2/storage': 'angularfire2.storage',
'angularfire2/messaging': 'angularfire2.messaging',
};

// Map of dependency versions across all packages
Expand All @@ -53,7 +55,8 @@ const MODULE_NAMES = {
"database-deprecated": 'angularfire2.database_deprecated',
firestore: 'angularfire2.firestore',
functions: 'angularfire2.functions',
storage: 'angularfire2.storage'
storage: 'angularfire2.storage',
messaging: 'angularfire2.messaging',
};

const ENTRIES = {
Expand All @@ -63,7 +66,8 @@ const ENTRIES = {
"database-deprecated": `${process.cwd()}/dist/packages-dist/database-deprecated/index.js`,
firestore: `${process.cwd()}/dist/packages-dist/firestore/index.js`,
functions: `${process.cwd()}/dist/packages-dist/functions/index.js`,
storage: `${process.cwd()}/dist/packages-dist/storage/index.js`
storage: `${process.cwd()}/dist/packages-dist/storage/index.js`,
messaging: `${process.cwd()}/dist/packages-dist/messaging/index.js`,
};

const SRC_PKG_PATHS = {
Expand All @@ -74,7 +78,8 @@ const SRC_PKG_PATHS = {
firestore: `${process.cwd()}/src/firestore/package.json`,
"firebase-node": `${process.cwd()}/src/firebase-node/package.json`,
functions: `${process.cwd()}/src/functions/package.json`,
storage: `${process.cwd()}/src/storage/package.json`
storage: `${process.cwd()}/src/storage/package.json`,
messaging: `${process.cwd()}/src/messaging/package.json`,
};

const DEST_PKG_PATHS = {
Expand All @@ -85,7 +90,8 @@ const DEST_PKG_PATHS = {
firestore: `${process.cwd()}/dist/packages-dist/firestore/package.json`,
"firebase-node": `${process.cwd()}/dist/packages-dist/firebase-node/package.json`,
functions: `${process.cwd()}/dist/packages-dist/functions/package.json`,
storage: `${process.cwd()}/dist/packages-dist/storage/package.json`
storage: `${process.cwd()}/dist/packages-dist/storage/package.json`,
messaging: `${process.cwd()}/dist/packages-dist/messaging/package.json`,
};

// Constants for running typescript commands
Expand Down Expand Up @@ -246,6 +252,7 @@ function getVersions() {
getDestPackageFile('firebase-node'),
getDestPackageFile('functions'),
getDestPackageFile('storage'),
getDestPackageFile('messaging'),
getDestPackageFile('database-deprecated')
];
return paths
Expand Down Expand Up @@ -285,14 +292,16 @@ function buildModules(globals) {
const firestore$ = buildModule('firestore', globals);
const functions$ = buildModule('functions', globals);
const storage$ = buildModule('storage', globals);
const messaging$ = buildModule('messaging', globals);
const dbdep$ = buildModule('database-deprecated', globals);
return forkJoin(core$, from(copyRootTest())).pipe(
switchMapTo(auth$),
switchMapTo(db$),
switchMapTo(firestore$),
switchMapTo(functions$),
switchMapTo(storage$),
switchMapTo(dbdep$)
switchMapTo(messaging$),
switchMapTo(dbdep$),
);
}

Expand All @@ -312,6 +321,7 @@ function buildLibrary(globals) {
const fsStats = measure('firestore');
const functionsStats = measure('functions');
const storageStats = measure('storage');
const messagingStats = measure('messaging');
const dbdepStats = measure('database-deprecated');
console.log(`
core.umd.js - ${coreStats.size}, ${coreStats.gzip}
Expand All @@ -320,6 +330,7 @@ function buildLibrary(globals) {
firestore.umd.js - ${fsStats.size}, ${fsStats.gzip}
functions.umd.js - ${functionsStats.size}, ${functionsStats.gzip}
storage.umd.js - ${storageStats.size}, ${storageStats.gzip}
messaging.umd.js - ${messagingStats.size}, ${messagingStats.gzip}
database-deprecated.umd.js - ${dbdepStats.size}, ${dbdepStats.gzip}
`);
verifyVersions();
Expand Down