UNPKG

6.95 kBJavaScriptView Raw
1import { __awaiter } from 'tslib';
2import * as i0 from '@angular/core';
3import { InjectionToken, PLATFORM_ID, Injectable, Inject, Optional, NgModule } from '@angular/core';
4import { of, EMPTY, Observable, concat } from 'rxjs';
5import { subscribeOn, observeOn, switchMap, map, shareReplay, switchMapTo, catchError, mergeMap, defaultIfEmpty } from 'rxjs/operators';
6import * as i1 from '@angular/fire';
7import { VERSION } from '@angular/fire';
8import { ɵfirebaseAppFactory, ɵcacheInstance, ɵlazySDKProxy, FIREBASE_OPTIONS, FIREBASE_APP_NAME, ɵapplyMixins } from '@angular/fire/compat';
9import { isSupported } from 'firebase/messaging';
10import firebase from 'firebase/compat/app';
11
12// DO NOT MODIFY, this file is autogenerated by tools/build.ts
13// Export a null object with the same keys as firebase/compat/messaging, so Proxy can work with proxy-polyfill in Internet Explorer
14const proxyPolyfillCompat = {
15 deleteToken: null,
16 getToken: null,
17 onMessage: null,
18 onBackgroundMessage: null,
19};
20
21const VAPID_KEY = new InjectionToken('angularfire2.messaging.vapid-key');
22const SERVICE_WORKER = new InjectionToken('angularfire2.messaging.service-worker-registeration');
23class 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}
54AngularFireMessaging.ɵ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 });
55AngularFireMessaging.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "12.1.3", ngImport: i0, type: AngularFireMessaging, providedIn: 'any' });
56i0.ɵɵ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
85class AngularFireMessagingModule {
86 constructor() {
87 firebase.registerVersion('angularfire', VERSION.full, 'fcm-compat');
88 }
89}
90AngularFireMessagingModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "12.1.3", ngImport: i0, type: AngularFireMessagingModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
91AngularFireMessagingModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "12.0.0", version: "12.1.3", ngImport: i0, type: AngularFireMessagingModule });
92AngularFireMessagingModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "12.1.3", ngImport: i0, type: AngularFireMessagingModule, providers: [AngularFireMessaging] });
93i0.ɵɵ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 * Generated bundle index. Do not edit.
102 */
103
104export { AngularFireMessaging, AngularFireMessagingModule, SERVICE_WORKER, VAPID_KEY };
105//# sourceMappingURL=angular-fire-compat-messaging.js.map