-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Expand file tree
/
Copy pathmessaging.ts
More file actions
146 lines (127 loc) · 5.44 KB
/
messaging.ts
File metadata and controls
146 lines (127 loc) · 5.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import { Inject, Injectable, InjectionToken, NgZone, Optional, PLATFORM_ID } from '@angular/core';
import firebase from 'firebase/app';
import { concat, EMPTY, Observable, of, throwError, fromEvent } from 'rxjs';
import { catchError, defaultIfEmpty, map, mergeMap, observeOn, switchMap, switchMapTo, shareReplay, filter, subscribeOn } from 'rxjs/operators';
import {
FIREBASE_APP_NAME,
FIREBASE_OPTIONS,
FirebaseAppConfig,
FirebaseOptions,
ɵAngularFireSchedulers,
ɵfirebaseAppFactory,
ɵlazySDKProxy,
ɵPromiseProxy,
ɵapplyMixins
} from '@angular/fire';
import { isPlatformServer } from '@angular/common';
import { proxyPolyfillCompat } from './base';
import { ɵfetchInstance } from '@angular/fire';
export const VAPID_KEY = new InjectionToken<string>('angularfire2.messaging.vapid-key');
export const SERVICE_WORKER = new InjectionToken<Promise<ServiceWorkerRegistration>>('angularfire2.messaging.service-worker-registeration');
// SEMVER(7): drop
const firebaseLTv8 = parseInt(firebase.SDK_VERSION, 10) < 8;
export interface AngularFireMessaging extends Omit<ɵPromiseProxy<firebase.messaging.Messaging>, 'deleteToken' | 'getToken' | 'requestPermission'> {
}
@Injectable({
providedIn: 'any'
})
export class AngularFireMessaging {
public readonly requestPermission: Observable<void>;
public readonly getToken: Observable<string | null>;
public readonly tokenChanges: Observable<string | null>;
public readonly messages: Observable<{}>;
public readonly requestToken: Observable<string | null>;
public readonly deleteToken: (token: string) => Observable<boolean>;
constructor(
@Inject(FIREBASE_OPTIONS) options: FirebaseOptions,
@Optional() @Inject(FIREBASE_APP_NAME) nameOrConfig: string | FirebaseAppConfig | null | undefined,
// tslint:disable-next-line:ban-types
@Inject(PLATFORM_ID) platformId: Object,
zone: NgZone,
@Optional() @Inject(VAPID_KEY) vapidKey: string|null,
@Optional() @Inject(SERVICE_WORKER) _serviceWorker: any,
) {
const schedulers = new ɵAngularFireSchedulers(zone);
const serviceWorker: Promise<ServiceWorkerRegistration> | null = _serviceWorker;
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, async () => {
const messaging = app.messaging();
if (firebaseLTv8) {
if (vapidKey) {
messaging.usePublicVapidKey(vapidKey);
}
if (serviceWorker) {
messaging.useServiceWorker(await serviceWorker);
}
}
return messaging;
}, [vapidKey, serviceWorker])),
shareReplay({ bufferSize: 1, refCount: false })
);
this.requestPermission = messaging.pipe(
subscribeOn(schedulers.outsideAngular),
observeOn(schedulers.insideAngular),
// tslint:disable-next-line
switchMap(messaging => firebase.messaging.isSupported() ? messaging.requestPermission() : throwError('Not supported.'))
);
this.getToken = messaging.pipe(
subscribeOn(schedulers.outsideAngular),
observeOn(schedulers.insideAngular),
switchMap(async messaging => {
if (firebase.messaging.isSupported() && Notification.permission === 'granted') {
if (firebaseLTv8) {
return await messaging.getToken();
} else {
const serviceWorkerRegistration = serviceWorker ? await serviceWorker : null;
return await messaging.getToken({ vapidKey, serviceWorkerRegistration });
}
} else {
return null;
}
})
);
const notificationPermission$ = new Observable<string>(emitter => {
navigator.permissions.query({ name: 'notifications' }).then(notificationPerm => {
notificationPerm.onchange = () => emitter.next();
});
});
const tokenChange$ = messaging.pipe(
subscribeOn(schedulers.outsideAngular),
observeOn(schedulers.insideAngular),
switchMapTo(notificationPermission$),
switchMapTo(this.getToken)
);
this.tokenChanges = messaging.pipe(
subscribeOn(schedulers.outsideAngular),
observeOn(schedulers.insideAngular),
switchMap(() => firebase.messaging.isSupported() ? concat(this.getToken, tokenChange$) : EMPTY)
);
this.messages = messaging.pipe(
subscribeOn(schedulers.outsideAngular),
observeOn(schedulers.insideAngular),
switchMap(messaging => firebase.messaging.isSupported() ? new Observable<string>(emitter =>
messaging.onMessage(next => emitter.next(next), err => emitter.error(err), () => emitter.complete())
) : EMPTY),
);
this.requestToken = of(undefined).pipe(
subscribeOn(schedulers.outsideAngular),
observeOn(schedulers.insideAngular),
switchMap(() => this.requestPermission),
catchError(() => of(null)),
mergeMap(() => this.tokenChanges)
);
// SEMVER(7): drop token
this.deleteToken = (token?: string) => messaging.pipe(
subscribeOn(schedulers.outsideAngular),
observeOn(schedulers.insideAngular),
switchMap(messaging => messaging.deleteToken(token || undefined)),
defaultIfEmpty(false)
);
return ɵlazySDKProxy(this, messaging, zone);
}
}
ɵapplyMixins(AngularFireMessaging, [proxyPolyfillCompat]);