From 8f3fc5d90fde8d990a0819d74c08723d5906cff5 Mon Sep 17 00:00:00 2001 From: David East Date: Mon, 5 Mar 2018 12:29:47 -0700 Subject: [PATCH 1/4] feat(messaging): add library infrastructure --- package.json | 2 +- src/messaging/index.spec.ts | 0 src/messaging/index.ts | 0 src/messaging/messaging.module.ts | 24 ++++++++++++++ src/messaging/messaging.spec.ts | 5 +++ src/messaging/messaging.ts | 18 ++++++++++ .../observable/request-permission.ts | 7 ++++ src/messaging/package.json | 31 +++++++++++++++++ src/messaging/public_api.ts | 3 ++ src/messaging/test-config.ts | 7 ++++ src/messaging/tsconfig-build.json | 33 +++++++++++++++++++ src/messaging/tsconfig-esm.json | 19 +++++++++++ src/messaging/tsconfig-test.json | 13 ++++++++ tools/build.js | 19 ++++++++--- 14 files changed, 176 insertions(+), 5 deletions(-) create mode 100644 src/messaging/index.spec.ts create mode 100644 src/messaging/index.ts create mode 100644 src/messaging/messaging.module.ts create mode 100644 src/messaging/messaging.spec.ts create mode 100644 src/messaging/messaging.ts create mode 100644 src/messaging/observable/request-permission.ts create mode 100644 src/messaging/package.json create mode 100644 src/messaging/public_api.ts create mode 100644 src/messaging/test-config.ts create mode 100644 src/messaging/tsconfig-build.json create mode 100644 src/messaging/tsconfig-esm.json create mode 100644 src/messaging/tsconfig-test.json diff --git a/package.json b/package.json index 0846e47cf..c8844f8d9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "angularfire2", - "version": "5.0.0-rc.6", + "version": "5.0.0-rc.7", "description": "The official library of Firebase and Angular.", "private": true, "scripts": { diff --git a/src/messaging/index.spec.ts b/src/messaging/index.spec.ts new file mode 100644 index 000000000..e69de29bb diff --git a/src/messaging/index.ts b/src/messaging/index.ts new file mode 100644 index 000000000..e69de29bb diff --git a/src/messaging/messaging.module.ts b/src/messaging/messaging.module.ts new file mode 100644 index 000000000..bfd7f5f95 --- /dev/null +++ b/src/messaging/messaging.module.ts @@ -0,0 +1,24 @@ +import { NgModule } from '@angular/core'; +import { AngularFireModule, FirebaseApp } from 'angularfire2'; +import { AngularFireMessaging } from './messaging'; +import '@firebase/storage'; + +export function _getAngularFireMessaging(app: FirebaseApp) { + return new AngularFireMessaging(app); +} + +export const AngularFireMessagingProvider = { + provide: AngularFireMessaging, + useFactory: _getAngularFireMessaging, + deps: [ FirebaseApp ] +}; + +export const STORAGE_PROVIDERS = [ + AngularFireMessagingProvider, +]; + +@NgModule({ + imports: [ AngularFireModule ], + providers: [ STORAGE_PROVIDERS ] +}) +export class AngularFireMessagingModule { } diff --git a/src/messaging/messaging.spec.ts b/src/messaging/messaging.spec.ts new file mode 100644 index 000000000..1facd1e4c --- /dev/null +++ b/src/messaging/messaging.spec.ts @@ -0,0 +1,5 @@ +import { COMMON_CONFIG } from './test-config'; + +describe('AngularFireMessaging', () => { + +}); diff --git a/src/messaging/messaging.ts b/src/messaging/messaging.ts new file mode 100644 index 000000000..bfee9fb91 --- /dev/null +++ b/src/messaging/messaging.ts @@ -0,0 +1,18 @@ +import { Injectable } from '@angular/core'; +import { Observable } from 'rxjs'; +import { FirebaseApp } from 'angularfire2'; +import { FirebaseMessaging } from '@firebase/messaging-types'; +import { requestPermission } from './observable/request-permission'; + +@Injectable() +export class AngularFireMessaging { + messaging: FirebaseMessaging; + requestPermission: Observable; + + constructor(public app: FirebaseApp) { + this.messaging = app.messaging(); + + this.requestPermission = requestPermission(this.messaging); + } + +} \ No newline at end of file diff --git a/src/messaging/observable/request-permission.ts b/src/messaging/observable/request-permission.ts new file mode 100644 index 000000000..c4f5c5ff5 --- /dev/null +++ b/src/messaging/observable/request-permission.ts @@ -0,0 +1,7 @@ +import { Observable } from 'rxjs'; +import { from } from 'rxjs/observable/from'; +import { FirebaseMessaging } from '@firebase/messaging-types'; + +export function requestPermission(messaging: FirebaseMessaging): Observable { + return from(messaging.requestPermission()!); +} diff --git a/src/messaging/package.json b/src/messaging/package.json new file mode 100644 index 000000000..360fc5520 --- /dev/null +++ b/src/messaging/package.json @@ -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" +} diff --git a/src/messaging/public_api.ts b/src/messaging/public_api.ts new file mode 100644 index 000000000..0c09cbd56 --- /dev/null +++ b/src/messaging/public_api.ts @@ -0,0 +1,3 @@ +export * from './observable/request-permission'; +export * from './messaging'; +export * from './messaging.module'; diff --git a/src/messaging/test-config.ts b/src/messaging/test-config.ts new file mode 100644 index 000000000..4b69c98dd --- /dev/null +++ b/src/messaging/test-config.ts @@ -0,0 +1,7 @@ + +export const COMMON_CONFIG = { + apiKey: "AIzaSyBVSy3YpkVGiKXbbxeK0qBnu3-MNZ9UIjA", + authDomain: "angularfire2-test.firebaseapp.com", + databaseURL: "https://angularfire2-test.firebaseio.com", + storageBucket: "angularfire2-test.appspot.com", +}; diff --git a/src/messaging/tsconfig-build.json b/src/messaging/tsconfig-build.json new file mode 100644 index 000000000..749fa92b6 --- /dev/null +++ b/src/messaging/tsconfig-build.json @@ -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 + } +} + diff --git a/src/messaging/tsconfig-esm.json b/src/messaging/tsconfig-esm.json new file mode 100644 index 000000000..0b3d05820 --- /dev/null +++ b/src/messaging/tsconfig-esm.json @@ -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" + } +} diff --git a/src/messaging/tsconfig-test.json b/src/messaging/tsconfig-test.json new file mode 100644 index 000000000..fc0dc883b --- /dev/null +++ b/src/messaging/tsconfig-test.json @@ -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" + ] +} diff --git a/tools/build.js b/tools/build.js index d0c6dc535..b350fa30a 100644 --- a/tools/build.js +++ b/tools/build.js @@ -84,6 +84,7 @@ const VERSIONS = { FIREBASE_FIRESTORE_VERSION: pkg.dependencies['@firebase/firestore'], FIREBASE_AUTH_VERSION: pkg.dependencies['@firebase/auth'], FIREBASE_STORAGE_VERSION: pkg.dependencies['@firebase/storage'], + FIREBASE_MESSAGING_VERSION: pkg.dependencies['@firebase/messaging'], RXJS_VERSION: pkg.dependencies['rxjs'], ZONEJS_VERSION: pkg.dependencies['zone.js'], ANGULARFIRE2_VERSION: pkg.version, @@ -100,7 +101,8 @@ const MODULE_NAMES = { database: 'angularfire2.database', "database-deprecated": 'angularfire2.database_deprecated', firestore: 'angularfire2.firestore', - storage: 'angularfire2.storage' + storage: 'angularfire2.storage', + messaging: 'angularfire2.messaging' }; const ENTRIES = { @@ -109,7 +111,8 @@ const ENTRIES = { database: `${process.cwd()}/dist/packages-dist/database/index.js`, "database-deprecated": `${process.cwd()}/dist/packages-dist/database-deprecated/index.js`, firestore: `${process.cwd()}/dist/packages-dist/firestore/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 = { @@ -119,7 +122,8 @@ const SRC_PKG_PATHS = { "database-deprecated": `${process.cwd()}/src/database-deprecated/package.json`, firestore: `${process.cwd()}/src/firestore/package.json`, "firebase-node": `${process.cwd()}/src/firebase-node/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 = { @@ -129,7 +133,8 @@ const DEST_PKG_PATHS = { "database-deprecated": `${process.cwd()}/dist/packages-dist/database-deprecated/package.json`, firestore: `${process.cwd()}/dist/packages-dist/firestore/package.json`, "firebase-node": `${process.cwd()}/dist/packages-dist/firebase-node/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`, }; const FIREBASE_FEATURE_MODULES = { @@ -138,6 +143,7 @@ const FIREBASE_FEATURE_MODULES = { database: `${process.cwd()}/node_modules/@firebase/database/dist/esm/index.js`, firestore: `${process.cwd()}/node_modules/@firebase/firestore/dist/esm/index.js`, storage: `${process.cwd()}/node_modules/@firebase/storage/dist/esm/index.js`, + messaging: `${process.cwd()}/node_modules/@firebase/messaging/dist/esm/index.js`, util: `${process.cwd()}/node_modules/@firebase/util/dist/esm/index.js`, }; @@ -298,6 +304,7 @@ function getVersions() { getDestPackageFile('firestore'), getDestPackageFile('firebase-node'), getDestPackageFile('storage'), + getDestPackageFile('messaging'), getDestPackageFile('database-deprecated') ]; return paths @@ -336,6 +343,7 @@ function buildModules(globals) { const db$ = buildModule('database', globals); const firestore$ = buildModule('firestore', globals); const storage$ = buildModule('storage', globals); + const messaging$ = buildModule('messaging', globals); const dbdep$ = buildModule('database-deprecated', globals); return Observable .forkJoin(core$, Observable.from(copyRootTest())) @@ -343,6 +351,7 @@ function buildModules(globals) { .switchMapTo(db$) .switchMapTo(firestore$) .switchMapTo(storage$) + .switchMapTo(messaging$) .switchMapTo(dbdep$); } @@ -362,6 +371,7 @@ function buildLibrary(globals) { const dbStats = measure('database'); const fsStats = measure('firestore'); const storageStats = measure('storage'); + const messagingStats = measure('messaging'); const dbdepStats = measure('database-deprecated'); console.log(` core.umd.js - ${coreStats.size}, ${coreStats.gzip} @@ -369,6 +379,7 @@ function buildLibrary(globals) { database.umd.js - ${dbStats.size}, ${dbStats.gzip} firestore.umd.js - ${fsStats.size}, ${fsStats.gzip} storage.umd.js - ${storageStats.size}, ${storageStats.gzip} + messaging.umd.js - ${messagingStats.size}, ${messagingStats.gzip} database-deprecated.umd.js - ${dbdepStats.size}, ${dbdepStats.gzip} `); verifyVersions(); From b98224fad162af4a7c52faf570638766ea4996dd Mon Sep 17 00:00:00 2001 From: David East Date: Mon, 5 Mar 2018 13:23:56 -0700 Subject: [PATCH 2/4] feat(messaging): add test setup --- karma.conf.js | 1 + src/messaging/index.spec.ts | 1 + src/messaging/index.ts | 1 + src/messaging/messaging.spec.ts | 35 ++++++++++++++++++++++++++++++++- src/messaging/messaging.ts | 12 +++++++++++ src/messaging/test-config.ts | 1 + src/root.spec.js | 1 + src/tsconfig.json | 1 + tools/build.js | 3 ++- 9 files changed, 54 insertions(+), 2 deletions(-) diff --git a/karma.conf.js b/karma.conf.js index 6244a67db..4fc95b5fd 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -32,6 +32,7 @@ module.exports = function(config) { 'dist/packages-dist/bundles/database.umd.{js,map}', 'dist/packages-dist/bundles/firestore.umd.{js,map}', 'dist/packages-dist/bundles/storage.umd.{js,map}', + 'dist/packages-dist/bundles/messaging.umd.{js,map}', 'dist/packages-dist/bundles/database-deprecated.umd.{js,map}', 'dist/packages-dist/bundles/test.umd.{js,map}', ], diff --git a/src/messaging/index.spec.ts b/src/messaging/index.spec.ts index e69de29bb..d37e336c9 100644 --- a/src/messaging/index.spec.ts +++ b/src/messaging/index.spec.ts @@ -0,0 +1 @@ +export * from './messaging.spec'; diff --git a/src/messaging/index.ts b/src/messaging/index.ts index e69de29bb..4aaf8f92e 100644 --- a/src/messaging/index.ts +++ b/src/messaging/index.ts @@ -0,0 +1 @@ +export * from './public_api'; diff --git a/src/messaging/messaging.spec.ts b/src/messaging/messaging.spec.ts index 1facd1e4c..bba3fc56e 100644 --- a/src/messaging/messaging.spec.ts +++ b/src/messaging/messaging.spec.ts @@ -1,5 +1,38 @@ import { COMMON_CONFIG } from './test-config'; +import { FirebaseApp as FBApp } from '@firebase/app-types'; +import { Observable } from 'rxjs/Observable' +import { forkJoin } from 'rxjs/observable/forkJoin'; +import { TestBed, inject } from '@angular/core/testing'; +import { FirebaseApp, FirebaseAppConfig, AngularFireModule } from 'angularfire2'; +import { AngularFireMessaging, AngularFireMessagingModule } from 'angularfire2/messaging'; -describe('AngularFireMessaging', () => { +fdescribe('AngularFireMessaging', () => { + let app: FBApp; + let afMessaging: AngularFireMessaging; + + beforeEach(() => { + TestBed.configureTestingModule({ + imports: [ + AngularFireModule.initializeApp(COMMON_CONFIG), + AngularFireMessagingModule + ] + }); + inject([FirebaseApp, AngularFireMessaging], (app_: FirebaseApp, _messaging: AngularFireMessaging) => { + app = app_; + afMessaging = _messaging; + })(); + }); + + afterEach(done => { + app.delete().then(done, done.fail); + }); + + it('should exist', () => { + expect(afMessaging instanceof AngularFireMessaging).toBe(true); + }); + + it('should have the Firebase messaging instance', () => { + expect(afMessaging.messaging).toBeDefined(); + }); }); diff --git a/src/messaging/messaging.ts b/src/messaging/messaging.ts index bfee9fb91..1d3be1c20 100644 --- a/src/messaging/messaging.ts +++ b/src/messaging/messaging.ts @@ -3,16 +3,28 @@ import { Observable } from 'rxjs'; import { FirebaseApp } from 'angularfire2'; import { FirebaseMessaging } from '@firebase/messaging-types'; import { requestPermission } from './observable/request-permission'; +import { from } from 'rxjs/observable/from'; @Injectable() export class AngularFireMessaging { messaging: FirebaseMessaging; requestPermission: Observable; + getToken: Observable; + tokenChanges: Observable; constructor(public app: FirebaseApp) { this.messaging = app.messaging(); this.requestPermission = requestPermission(this.messaging); + this.getToken = from(this.messaging.getToken()!); + this.tokenChanges = new Observable(subscriber => { + this.messaging.onTokenRefresh(subscriber); + }); + + } + + deleteToken(token: string) { + return from(this.messaging.deleteToken(token)!); } } \ No newline at end of file diff --git a/src/messaging/test-config.ts b/src/messaging/test-config.ts index 4b69c98dd..58f9efc32 100644 --- a/src/messaging/test-config.ts +++ b/src/messaging/test-config.ts @@ -4,4 +4,5 @@ export const COMMON_CONFIG = { authDomain: "angularfire2-test.firebaseapp.com", databaseURL: "https://angularfire2-test.firebaseio.com", storageBucket: "angularfire2-test.appspot.com", + messagingSenderId: "920323787688" }; diff --git a/src/root.spec.js b/src/root.spec.js index b60264afb..d747356bc 100644 --- a/src/root.spec.js +++ b/src/root.spec.js @@ -12,6 +12,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 diff --git a/src/tsconfig.json b/src/tsconfig.json index 2a0a351e4..6027275dc 100644 --- a/src/tsconfig.json +++ b/src/tsconfig.json @@ -15,6 +15,7 @@ "angularfire2/database": ["./database"], "angularfire2/firestore": ["./firestore"], "angularfire2/storage": ["./storage"], + "angularfire2/messaging": ["./messaging"], "angularfire2/database-deprecated": ["./database-deprecated"] }, "rootDir": ".", diff --git a/tools/build.js b/tools/build.js index b350fa30a..bb745570c 100644 --- a/tools/build.js +++ b/tools/build.js @@ -73,7 +73,8 @@ const GLOBALS = { 'angularfire2/database': 'angularfire2.database', 'angularfire2/database-deprecated': 'angularfire2.database_deprecated', 'angularfire2/firestore': 'angularfire2.firestore', - 'angularfire2/storage': 'angularfire2.storage' + 'angularfire2/storage': 'angularfire2.storage', + 'angularfire2/messaging': 'angularfire2.messaging', }; // Map of dependency versions across all packages From 01c9a6f2920494064246a5bad223ce2f0cf23d9a Mon Sep 17 00:00:00 2001 From: David East Date: Mon, 5 Mar 2018 14:44:29 -0700 Subject: [PATCH 3/4] feat(messaging): broken service worker test registration --- firebase-messaging-sw.js | 10 ++++++++ karma.conf.js | 8 +++++++ src/messaging/messaging.spec.ts | 42 ++++++++++++++++++++++++--------- src/messaging/messaging.ts | 14 +++++++++++ 4 files changed, 63 insertions(+), 11 deletions(-) create mode 100644 firebase-messaging-sw.js diff --git a/firebase-messaging-sw.js b/firebase-messaging-sw.js new file mode 100644 index 000000000..1244006c4 --- /dev/null +++ b/firebase-messaging-sw.js @@ -0,0 +1,10 @@ +// This "base" path is for Karma testing +importScripts('/base/node_modules/firebase/firebase.js'); +var COMMON_CONFIG = { + apiKey: "AIzaSyBVSy3YpkVGiKXbbxeK0qBnu3-MNZ9UIjA", + authDomain: "angularfire2-test.firebaseapp.com", + databaseURL: "https://angularfire2-test.firebaseio.com", + storageBucket: "angularfire2-test.appspot.com", + messagingSenderId: "920323787688" +}; +firebase.initializeApp(COMMON_CONFIG); \ No newline at end of file diff --git a/karma.conf.js b/karma.conf.js index 4fc95b5fd..03fec0de6 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -27,6 +27,13 @@ module.exports = function(config) { 'karma-test-shim.js', 'node_modules/firebase/firebase.js', 'node_modules/firebase/firebase-firestore.js', + + // This service worker is loaded by a service worker registration + { + pattern: 'firebase-messaging-sw.js', + included: false + }, + 'dist/packages-dist/bundles/core.umd.{js,map}', 'dist/packages-dist/bundles/auth.umd.{js,map}', 'dist/packages-dist/bundles/database.umd.{js,map}', @@ -35,6 +42,7 @@ module.exports = function(config) { 'dist/packages-dist/bundles/messaging.umd.{js,map}', 'dist/packages-dist/bundles/database-deprecated.umd.{js,map}', 'dist/packages-dist/bundles/test.umd.{js,map}', + ], port: 9876, diff --git a/src/messaging/messaging.spec.ts b/src/messaging/messaging.spec.ts index bba3fc56e..819a67587 100644 --- a/src/messaging/messaging.spec.ts +++ b/src/messaging/messaging.spec.ts @@ -5,22 +5,34 @@ import { forkJoin } from 'rxjs/observable/forkJoin'; import { TestBed, inject } from '@angular/core/testing'; import { FirebaseApp, FirebaseAppConfig, AngularFireModule } from 'angularfire2'; import { AngularFireMessaging, AngularFireMessagingModule } from 'angularfire2/messaging'; +import * as firebase from 'firebase/app'; +import 'firebase/messaging'; fdescribe('AngularFireMessaging', () => { - let app: FBApp; + let app: firebase.app.App; let afMessaging: AngularFireMessaging; - beforeEach(() => { - TestBed.configureTestingModule({ - imports: [ - AngularFireModule.initializeApp(COMMON_CONFIG), - AngularFireMessagingModule - ] + beforeAll((done: any) => { + navigator.serviceWorker.register('http://localhost:9876/base/firebase-messaging-sw.js') + .then((registration) => { + app = firebase.initializeApp(COMMON_CONFIG, 'SW-REG'); + app.messaging!().useServiceWorker(registration); + TestBed.configureTestingModule({ + imports: [ + AngularFireModule.initializeApp(COMMON_CONFIG), + AngularFireMessagingModule + ] + }); + inject([FirebaseApp, AngularFireMessaging], (app_: FirebaseApp, _messaging: AngularFireMessaging) => { + // app = app_; + afMessaging = _messaging; + done(); + })(); + }) + .catch(e => { + console.error(e); }); - inject([FirebaseApp, AngularFireMessaging], (app_: FirebaseApp, _messaging: AngularFireMessaging) => { - app = app_; - afMessaging = _messaging; - })(); + }); afterEach(done => { @@ -35,4 +47,12 @@ fdescribe('AngularFireMessaging', () => { expect(afMessaging.messaging).toBeDefined(); }); + it('should give me a token after requesting permission', (done) => { + afMessaging.requestToken.subscribe(token => { + debugger; + expect(token).toBeDefined(); + done(); + }, done.fail); + }); + }); diff --git a/src/messaging/messaging.ts b/src/messaging/messaging.ts index 1d3be1c20..c8e573ed0 100644 --- a/src/messaging/messaging.ts +++ b/src/messaging/messaging.ts @@ -4,6 +4,7 @@ import { FirebaseApp } from 'angularfire2'; import { FirebaseMessaging } from '@firebase/messaging-types'; import { requestPermission } from './observable/request-permission'; import { from } from 'rxjs/observable/from'; +import { mergeMap } from 'rxjs/operators'; @Injectable() export class AngularFireMessaging { @@ -11,16 +12,29 @@ export class AngularFireMessaging { requestPermission: Observable; getToken: Observable; tokenChanges: Observable; + messages: Observable<{}>; + requestToken: Observable; constructor(public app: FirebaseApp) { this.messaging = app.messaging(); this.requestPermission = requestPermission(this.messaging); + this.getToken = from(this.messaging.getToken()!); + this.tokenChanges = new Observable(subscriber => { + this.messaging.getToken()!.then(t => subscriber.next(t)); this.messaging.onTokenRefresh(subscriber); }); + this.messages = new Observable(subscriber => { + this.messaging.onMessage(subscriber); + }); + + this.requestToken = this.requestPermission.pipe( + mergeMap(() => this.tokenChanges) + ); + } deleteToken(token: string) { From 6eb8680e525d0d840bf0720874ecde82b543183a Mon Sep 17 00:00:00 2001 From: David East Date: Sun, 25 Mar 2018 21:50:17 -0600 Subject: [PATCH 4/4] feat(messaging): add messaging tests --- firebase-messaging-sw.js | 18 +++++++------- src/messaging/messaging.module.ts | 2 +- src/messaging/messaging.spec.ts | 39 ++++++++++++++----------------- src/root.spec.js | 12 ++++++++++ 4 files changed, 38 insertions(+), 33 deletions(-) diff --git a/firebase-messaging-sw.js b/firebase-messaging-sw.js index 1244006c4..eaf06c7c2 100644 --- a/firebase-messaging-sw.js +++ b/firebase-messaging-sw.js @@ -1,10 +1,8 @@ -// This "base" path is for Karma testing -importScripts('/base/node_modules/firebase/firebase.js'); -var COMMON_CONFIG = { - apiKey: "AIzaSyBVSy3YpkVGiKXbbxeK0qBnu3-MNZ9UIjA", - authDomain: "angularfire2-test.firebaseapp.com", - databaseURL: "https://angularfire2-test.firebaseio.com", - storageBucket: "angularfire2-test.appspot.com", - messagingSenderId: "920323787688" -}; -firebase.initializeApp(COMMON_CONFIG); \ No newline at end of file +importScripts('https://www.gstatic.com/firebasejs/4.9.0/firebase-app.js'); +importScripts('https://www.gstatic.com/firebasejs/4.9.0/firebase-messaging.js'); + +firebase.initializeApp({ + 'messagingSenderId': '920323787688' +}); + +const messaging = firebase.messaging(); diff --git a/src/messaging/messaging.module.ts b/src/messaging/messaging.module.ts index bfd7f5f95..8ceb24766 100644 --- a/src/messaging/messaging.module.ts +++ b/src/messaging/messaging.module.ts @@ -1,7 +1,7 @@ import { NgModule } from '@angular/core'; import { AngularFireModule, FirebaseApp } from 'angularfire2'; import { AngularFireMessaging } from './messaging'; -import '@firebase/storage'; +import '@firebase/messaging'; export function _getAngularFireMessaging(app: FirebaseApp) { return new AngularFireMessaging(app); diff --git a/src/messaging/messaging.spec.ts b/src/messaging/messaging.spec.ts index 819a67587..a6e6f9530 100644 --- a/src/messaging/messaging.spec.ts +++ b/src/messaging/messaging.spec.ts @@ -8,35 +8,22 @@ import { AngularFireMessaging, AngularFireMessagingModule } from 'angularfire2/m import * as firebase from 'firebase/app'; import 'firebase/messaging'; -fdescribe('AngularFireMessaging', () => { +xdescribe('AngularFireMessaging', () => { let app: firebase.app.App; let afMessaging: AngularFireMessaging; beforeAll((done: any) => { - navigator.serviceWorker.register('http://localhost:9876/base/firebase-messaging-sw.js') + navigator.serviceWorker.register('/base/firebase-messaging-sw.js') .then((registration) => { app = firebase.initializeApp(COMMON_CONFIG, 'SW-REG'); app.messaging!().useServiceWorker(registration); - TestBed.configureTestingModule({ - imports: [ - AngularFireModule.initializeApp(COMMON_CONFIG), - AngularFireMessagingModule - ] - }); - inject([FirebaseApp, AngularFireMessaging], (app_: FirebaseApp, _messaging: AngularFireMessaging) => { - // app = app_; - afMessaging = _messaging; - done(); - })(); + afMessaging = new AngularFireMessaging(app as any); + done(); }) .catch(e => { console.error(e); - }); - - }); - - afterEach(done => { - app.delete().then(done, done.fail); + done.fail(); + }) }); it('should exist', () => { @@ -47,12 +34,20 @@ fdescribe('AngularFireMessaging', () => { expect(afMessaging.messaging).toBeDefined(); }); - it('should give me a token after requesting permission', (done) => { + it('should get request permission and get a token', (done) => { afMessaging.requestToken.subscribe(token => { - debugger; - expect(token).toBeDefined(); + expect(typeof token === 'string').toBe(true); done(); }, done.fail); }); + it('should give me a token', (done) => { + afMessaging.getToken.subscribe(token => { + expect(typeof token === 'string').toBe(true); + done(); + }, done.fail); + }); + + + }); diff --git a/src/root.spec.js b/src/root.spec.js index d747356bc..12f4a8c9d 100644 --- a/src/root.spec.js +++ b/src/root.spec.js @@ -14,6 +14,18 @@ export * from './packages-dist/database/list/audit-trail.spec'; export * from './packages-dist/storage/storage.spec'; export * from './packages-dist/messaging/messaging.spec'; +// navigator.serviceWorker.register('http://localhost:9876/base/firebase-messaging-sw.js') +// .then(registration => { +// debugger; +// const app = firebase.initializeApp({ +// 'messagingSenderId': '920323787688' +// }, 'MESSAGING'); +// app.messaging().useServiceWorker(registration); +// }) +// .catch(e => { +// console.error(e); +// }) + // // 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 // export * from './packages-dist/database-deprecated/firebase_list_factory.spec';