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