|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2017 Google LLC |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +import { ComponentContainer, Provider } from '@firebase/component'; |
| 19 | +import { FirebaseApp, _FirebaseService } from '@firebase/app-types-exp'; |
| 20 | +import { |
| 21 | + FirebaseMessaging, |
| 22 | + MessagePayload |
| 23 | +} from '@firebase/messaging-types-exp'; |
| 24 | +import { NextFn, Observer, Unsubscribe } from '@firebase/util'; |
| 25 | + |
| 26 | +import { FirebaseInternalDependencies } from './interfaces/internal-dependencies'; |
| 27 | +import { SwController } from './controllers/sw-controller'; |
| 28 | +import { WindowController } from './controllers/window-controller'; |
| 29 | +import { _getProvider } from '@firebase/app-exp'; |
| 30 | +import { extractAppConfig } from './helpers/extract-app-config'; |
| 31 | + |
| 32 | +export function getMessaging(app: FirebaseApp): FirebaseMessaging { |
| 33 | + const messagingProvider: Provider<'messaging'> = _getProvider( |
| 34 | + app, |
| 35 | + 'messaging' |
| 36 | + ); |
| 37 | + |
| 38 | + return messagingProvider.getImmediate(); |
| 39 | +} |
| 40 | + |
| 41 | +export function getToken( |
| 42 | + messaging: FirebaseMessaging, |
| 43 | + options?: { vapidKey?: string; swReg?: ServiceWorkerRegistration } |
| 44 | +): Promise<string> { |
| 45 | + return messaging.getToken(options); |
| 46 | +} |
| 47 | + |
| 48 | +export function deleteToken(messaging: FirebaseMessaging): Promise<boolean> { |
| 49 | + return messaging.deleteToken(); |
| 50 | +} |
| 51 | + |
| 52 | +export function onMessage( |
| 53 | + messaging: FirebaseMessaging, |
| 54 | + nextOrObserver: NextFn<MessagePayload> | Observer<MessagePayload> |
| 55 | +): Unsubscribe { |
| 56 | + return messaging.onMessage(nextOrObserver); |
| 57 | +} |
| 58 | + |
| 59 | +export function onBackgroundMessage( |
| 60 | + messaging: FirebaseMessaging, |
| 61 | + nextOrObserver: NextFn<MessagePayload> | Observer<MessagePayload> |
| 62 | +): Unsubscribe { |
| 63 | + return messaging.onBackgroundMessage(nextOrObserver); |
| 64 | +} |
| 65 | + |
| 66 | +export class MessagingService implements _FirebaseService { |
| 67 | + app!: FirebaseApp; |
| 68 | + readonly windowController: WindowController | null = null; |
| 69 | + readonly swController: SwController | null = null; |
| 70 | + |
| 71 | + constructor(container: ComponentContainer) { |
| 72 | + const app = container.getProvider('app-exp').getImmediate(); |
| 73 | + const appConfig = extractAppConfig(app); |
| 74 | + const installations = container.getProvider('installations').getImmediate(); |
| 75 | + const analyticsProvider = container.getProvider('analytics-internal'); |
| 76 | + |
| 77 | + const firebaseDependencies: FirebaseInternalDependencies = { |
| 78 | + app, |
| 79 | + appConfig, |
| 80 | + installations, |
| 81 | + analyticsProvider |
| 82 | + }; |
| 83 | + |
| 84 | + if (self && 'ServiceWorkerGlobalScope' in self) { |
| 85 | + this.swController = new SwController(firebaseDependencies); |
| 86 | + } else { |
| 87 | + this.windowController = new WindowController(firebaseDependencies); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + _delete(): Promise<void> { |
| 92 | + throw new Error('Method not implemented.'); |
| 93 | + } |
| 94 | +} |
0 commit comments