1 | import { __awaiter } from 'tslib';
|
2 | import * as i0 from '@angular/core';
|
3 | import { InjectionToken, Injectable, Inject, Optional, PLATFORM_ID, NgZone, NgModule } from '@angular/core';
|
4 | import firebase from 'firebase/app';
|
5 | import { of, EMPTY, throwError, Observable, concat } from 'rxjs';
|
6 | import { subscribeOn, observeOn, switchMap, map, shareReplay, switchMapTo, catchError, mergeMap, defaultIfEmpty } from 'rxjs/operators';
|
7 | import * as i1 from '@angular/fire';
|
8 | import { ɵAngularFireSchedulers, ɵfirebaseAppFactory, ɵfetchInstance, ɵlazySDKProxy, FIREBASE_OPTIONS, FIREBASE_APP_NAME, ɵapplyMixins } from '@angular/fire';
|
9 | import { isPlatformServer } from '@angular/common';
|
10 |
|
11 | const proxyPolyfillCompat = {
|
12 | deleteToken: null,
|
13 | getToken: null,
|
14 | onMessage: null,
|
15 | onBackgroundMessage: null,
|
16 | onTokenRefresh: null,
|
17 | requestPermission: null,
|
18 | setBackgroundMessageHandler: null,
|
19 | useServiceWorker: null,
|
20 | usePublicVapidKey: null,
|
21 | };
|
22 |
|
23 | const VAPID_KEY = new InjectionToken('angularfire2.messaging.vapid-key');
|
24 | const SERVICE_WORKER = new InjectionToken('angularfire2.messaging.service-worker-registeration');
|
25 |
|
26 | const firebaseLTv8 = parseInt(firebase.SDK_VERSION, 10) < 8;
|
27 | class AngularFireMessaging {
|
28 | constructor(options, nameOrConfig,
|
29 | // tslint:disable-next-line:ban-types
|
30 | platformId, zone, vapidKey, _serviceWorker) {
|
31 | const schedulers = new ɵAngularFireSchedulers(zone);
|
32 | const serviceWorker = _serviceWorker;
|
33 | const messaging = of(undefined).pipe(subscribeOn(schedulers.outsideAngular), observeOn(schedulers.insideAngular), switchMap(() => isPlatformServer(platformId) ? EMPTY : import('firebase/messaging')), map(() => ɵfirebaseAppFactory(options, zone, nameOrConfig)), switchMap(app => ɵfetchInstance(`${app.name}.messaging`, 'AngularFireMessaging', app, () => __awaiter(this, void 0, void 0, function* () {
|
34 | const messaging = app.messaging();
|
35 | if (firebaseLTv8) {
|
36 | if (vapidKey) {
|
37 | messaging.usePublicVapidKey(vapidKey);
|
38 | }
|
39 | if (serviceWorker) {
|
40 | messaging.useServiceWorker(yield serviceWorker);
|
41 | }
|
42 | }
|
43 | return messaging;
|
44 | }), [vapidKey, serviceWorker])), shareReplay({ bufferSize: 1, refCount: false }));
|
45 | this.requestPermission = messaging.pipe(subscribeOn(schedulers.outsideAngular), observeOn(schedulers.insideAngular),
|
46 |
|
47 | switchMap(messaging => firebase.messaging.isSupported() ? messaging.requestPermission() : throwError('Not supported.')));
|
48 | this.getToken = messaging.pipe(subscribeOn(schedulers.outsideAngular), observeOn(schedulers.insideAngular), switchMap((messaging) => __awaiter(this, void 0, void 0, function* () {
|
49 | if (firebase.messaging.isSupported() && Notification.permission === 'granted') {
|
50 | if (firebaseLTv8) {
|
51 | return yield messaging.getToken();
|
52 | }
|
53 | else {
|
54 | const serviceWorkerRegistration = serviceWorker ? yield serviceWorker : null;
|
55 | return yield messaging.getToken({ vapidKey, serviceWorkerRegistration });
|
56 | }
|
57 | }
|
58 | else {
|
59 | return null;
|
60 | }
|
61 | })));
|
62 | const notificationPermission$ = new Observable(emitter => {
|
63 | navigator.permissions.query({ name: 'notifications' }).then(notificationPerm => {
|
64 | notificationPerm.onchange = () => emitter.next();
|
65 | });
|
66 | });
|
67 | const tokenChange$ = messaging.pipe(subscribeOn(schedulers.outsideAngular), observeOn(schedulers.insideAngular), switchMapTo(notificationPermission$), switchMapTo(this.getToken));
|
68 | this.tokenChanges = messaging.pipe(subscribeOn(schedulers.outsideAngular), observeOn(schedulers.insideAngular), switchMap(() => firebase.messaging.isSupported() ? concat(this.getToken, tokenChange$) : EMPTY));
|
69 | this.messages = messaging.pipe(subscribeOn(schedulers.outsideAngular), observeOn(schedulers.insideAngular), switchMap(messaging => firebase.messaging.isSupported() ? new Observable(emitter => messaging.onMessage(next => emitter.next(next), err => emitter.error(err), () => emitter.complete())) : EMPTY));
|
70 | this.requestToken = of(undefined).pipe(subscribeOn(schedulers.outsideAngular), observeOn(schedulers.insideAngular), switchMap(() => this.requestPermission), catchError(() => of(null)), mergeMap(() => this.tokenChanges));
|
71 |
|
72 | this.deleteToken = (token) => messaging.pipe(subscribeOn(schedulers.outsideAngular), observeOn(schedulers.insideAngular), switchMap(messaging => messaging.deleteToken(token || undefined)), defaultIfEmpty(false));
|
73 | return ɵlazySDKProxy(this, messaging, zone);
|
74 | }
|
75 | }
|
76 | AngularFireMessaging.ɵprov = i0.ɵɵdefineInjectable({ factory: function AngularFireMessaging_Factory() { return new AngularFireMessaging(i0.ɵɵinject(i1.FIREBASE_OPTIONS), i0.ɵɵinject(i1.FIREBASE_APP_NAME, 8), i0.ɵɵinject(i0.PLATFORM_ID), i0.ɵɵinject(i0.NgZone), i0.ɵɵinject(VAPID_KEY, 8), i0.ɵɵinject(SERVICE_WORKER, 8)); }, token: AngularFireMessaging, providedIn: "any" });
|
77 | AngularFireMessaging.decorators = [
|
78 | { type: Injectable, args: [{
|
79 | providedIn: 'any'
|
80 | },] }
|
81 | ];
|
82 |
|
83 | AngularFireMessaging.ctorParameters = () => [
|
84 | { type: undefined, decorators: [{ type: Inject, args: [FIREBASE_OPTIONS,] }] },
|
85 | { type: undefined, decorators: [{ type: Optional }, { type: Inject, args: [FIREBASE_APP_NAME,] }] },
|
86 | { type: Object, decorators: [{ type: Inject, args: [PLATFORM_ID,] }] },
|
87 | { type: NgZone },
|
88 | { type: String, decorators: [{ type: Optional }, { type: Inject, args: [VAPID_KEY,] }] },
|
89 | { type: undefined, decorators: [{ type: Optional }, { type: Inject, args: [SERVICE_WORKER,] }] }
|
90 | ];
|
91 | ɵapplyMixins(AngularFireMessaging, [proxyPolyfillCompat]);
|
92 |
|
93 | class AngularFireMessagingModule {
|
94 | }
|
95 | AngularFireMessagingModule.decorators = [
|
96 | { type: NgModule, args: [{
|
97 | providers: [AngularFireMessaging]
|
98 | },] }
|
99 | ];
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 | export { AngularFireMessaging, AngularFireMessagingModule, SERVICE_WORKER, VAPID_KEY };
|
106 |
|