(
@Inject(FIREBASE_OPTIONS) options: FirebaseOptions,
@Optional() @Inject(FIREBASE_APP_NAME) name: string | null | undefined,
// eslint-disable-next-line @typescript-eslint/ban-types
@Inject(PLATFORM_ID) platformId: Object,
zone: NgZone,
schedulers: ɵAngularFireSchedulers,
@Optional() @Inject(VAPID_KEY) vapidKey: string|null,
@Optional() @Inject(SERVICE_WORKER) _serviceWorker: any,
)
| 29 | public readonly deleteToken: (token: string) => Observable<boolean>; |
| 30 | |
| 31 | constructor( |
| 32 | @Inject(FIREBASE_OPTIONS) options: FirebaseOptions, |
| 33 | @Optional() @Inject(FIREBASE_APP_NAME) name: string | null | undefined, |
| 34 | // eslint-disable-next-line @typescript-eslint/ban-types |
| 35 | @Inject(PLATFORM_ID) platformId: Object, |
| 36 | zone: NgZone, |
| 37 | schedulers: ɵAngularFireSchedulers, |
| 38 | @Optional() @Inject(VAPID_KEY) vapidKey: string|null, |
| 39 | @Optional() @Inject(SERVICE_WORKER) _serviceWorker: any, |
| 40 | ) { |
| 41 | const serviceWorker: Promise<ServiceWorkerRegistration> | null = _serviceWorker; |
| 42 | |
| 43 | const messaging = of(undefined).pipe( |
| 44 | subscribeOn(schedulers.outsideAngular), |
| 45 | observeOn(schedulers.insideAngular), |
| 46 | switchMap(isSupported), |
| 47 | switchMap(supported => supported ? import('firebase/compat/messaging') : EMPTY), |
| 48 | map(() => ɵfirebaseAppFactory(options, zone, name)), |
| 49 | switchMap(app => ɵcacheInstance(`${app.name}.messaging`, 'AngularFireMessaging', app.name, () => { |
| 50 | return of(app.messaging()); |
| 51 | }, [])), |
| 52 | shareReplay({ bufferSize: 1, refCount: false }) |
| 53 | ); |
| 54 | |
| 55 | |
| 56 | this.requestPermission = messaging.pipe( |
| 57 | subscribeOn(schedulers.outsideAngular), |
| 58 | observeOn(schedulers.insideAngular), |
| 59 | switchMap(() => Notification.requestPermission()) |
| 60 | ); |
| 61 | |
| 62 | this.getToken = messaging.pipe( |
| 63 | subscribeOn(schedulers.outsideAngular), |
| 64 | observeOn(schedulers.insideAngular), |
| 65 | switchMap(async messaging => { |
| 66 | if (Notification.permission === 'granted') { |
| 67 | // eslint-disable-next-line @typescript-eslint/no-misused-promises |
| 68 | const serviceWorkerRegistration = serviceWorker ? await serviceWorker : null; |
| 69 | return await messaging.getToken({ vapidKey, serviceWorkerRegistration }); |
| 70 | } else { |
| 71 | return null; |
| 72 | } |
| 73 | }) |
| 74 | ); |
| 75 | |
| 76 | const notificationPermission$ = new Observable<void>(emitter => { |
| 77 | navigator.permissions.query({ name: 'notifications' }).then(notificationPerm => { |
| 78 | notificationPerm.onchange = () => emitter.next(); |
| 79 | }); |
| 80 | }); |
| 81 | |
| 82 | |
| 83 | const tokenChange$ = messaging.pipe( |
| 84 | subscribeOn(schedulers.outsideAngular), |
| 85 | observeOn(schedulers.insideAngular), |
| 86 | switchMapTo(notificationPermission$), |
| 87 | switchMapTo(this.getToken) |
| 88 | ); |
nothing calls this directly
no test coverage detected