/** * @license * Copyright 2017 Google LLC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import { ERROR_FACTORY, ErrorCode } from './util/errors'; import { FirebaseApp, _getProvider, getApp } from '@firebase/app'; import { GetTokenOptions, MessagePayload, Messaging, RegisterOptions } from './interfaces/public-types'; import { NextFn, Observer, Unsubscribe, getModularInstance } from '@firebase/util'; import { isSwSupported, isWindowSupported } from './api/isSupported'; import { MessagingService } from './messaging-service'; import { deleteToken as _deleteToken } from './api/deleteToken'; import { getToken as _getToken } from './api/getToken'; import { onBackgroundMessage as _onBackgroundMessage } from './api/onBackgroundMessage'; import { onMessage as _onMessage } from './api/onMessage'; import { onRegistered as _onRegistered } from './api/onRegistered'; import { onUnregistered as _onUnregistered } from './api/onUnregistered'; import { register as _register } from './api/register'; import { unregister as _unregister } from './api/unregister'; import { _setDeliveryMetricsExportedToBigQueryEnabled } from './api/setDeliveryMetricsExportedToBigQueryEnabled'; /** * Retrieves a Firebase Cloud Messaging instance. * * @returns The Firebase Cloud Messaging instance associated with the provided firebase app. * * @public */ export function getMessagingInWindow(app: FirebaseApp = getApp()): Messaging { // Conscious decision to make this async check non-blocking during the messaging instance // initialization phase for performance consideration. An error would be thrown latter for // developer's information. Developers can then choose to import and call `isSupported` for // special handling. isWindowSupported().then( isSupported => { // If `isWindowSupported()` resolved, but returned false. if (!isSupported) { throw ERROR_FACTORY.create(ErrorCode.UNSUPPORTED_BROWSER); } }, _ => { // If `isWindowSupported()` rejected. throw ERROR_FACTORY.create(ErrorCode.INDEXED_DB_UNSUPPORTED); } ); return _getProvider(getModularInstance(app), 'messaging').getImmediate(); } /** * Retrieves a Firebase Cloud Messaging instance. * * @returns The Firebase Cloud Messaging instance associated with the provided firebase app. * * @public */ export function getMessagingInSw(app: FirebaseApp = getApp()): Messaging { // Conscious decision to make this async check non-blocking during the messaging instance // initialization phase for performance consideration. An error would be thrown latter for // developer's information. Developers can then choose to import and call `isSupported` for // special handling. isSwSupported().then( isSupported => { // If `isSwSupported()` resolved, but returned false. if (!isSupported) { throw ERROR_FACTORY.create(ErrorCode.UNSUPPORTED_BROWSER); } }, _ => { // If `isSwSupported()` rejected. throw ERROR_FACTORY.create(ErrorCode.INDEXED_DB_UNSUPPORTED); } ); return _getProvider(getModularInstance(app), 'messaging-sw').getImmediate(); } /** * Subscribes the {@link Messaging} instance to push notifications. Returns a Firebase Cloud * Messaging registration token that can be used to send push messages to that {@link Messaging} * instance. * * If notification permission isn't already granted, this method asks the user for permission. The * returned promise rejects if the user does not allow the app to show notifications. * * @param messaging - The {@link Messaging} instance. * @param options - Provides an optional vapid key and an optional service worker registration. * * @returns The promise resolves with an FCM registration token. * * @deprecated Use {@link register} together with {@link onRegistered} for Firebase * Installation ID-based messaging instead of retrieving an FCM registration token with this API. * * @public */ export async function getToken( messaging: Messaging, options?: GetTokenOptions ): Promise { messaging = getModularInstance(messaging); return _getToken(messaging as MessagingService, options); } /** * Deletes the registration token associated with this {@link Messaging} instance and unsubscribes * the {@link Messaging} instance from the push subscription. * * If there is no legacy registration token but the client has FID-based registration metadata * (from {@link register}), this deletes that registration on the server, clears local metadata, and * invokes {@link onUnregistered} with the removed FID when successful. * * @param messaging - The {@link Messaging} instance. * * @returns The promise resolves when the token has been successfully deleted. * * @deprecated Use {@link onUnregistered} to observe when the client is no longer * registered and update your backend accordingly, instead of explicitly deleting the * registration token with this API. * * @public */ export function deleteToken(messaging: Messaging): Promise { messaging = getModularInstance(messaging); return _deleteToken(messaging as MessagingService); } /** * When a push message is received and the user is currently on a page for your origin, the * message is passed to the page and an `onMessage()` event is dispatched with the payload of * the push message. * * * @param messaging - The {@link Messaging} instance. * @param nextOrObserver - This function, or observer object with `next` defined, * is called when a message is received and the user is currently viewing your page. * @returns To stop listening for messages execute this returned function. * * @public */ export function onMessage( messaging: Messaging, nextOrObserver: NextFn | Observer ): Unsubscribe { messaging = getModularInstance(messaging); return _onMessage(messaging as MessagingService, nextOrObserver); } /** * Called when a message is received while the app is in the background. An app is considered to be * in the background if no active window is displayed. * * @param messaging - The {@link Messaging} instance. * @param nextOrObserver - This function, or observer object with `next` defined, is called when a * message is received and the app is currently in the background. * * @returns To stop listening for messages execute this returned function. * * @public */ export function onBackgroundMessage( messaging: Messaging, nextOrObserver: NextFn | Observer ): Unsubscribe { messaging = getModularInstance(messaging); return _onBackgroundMessage(messaging as MessagingService, nextOrObserver); } /** * Registers the app instance with FCM using its Firebase Installation ID (FID). The FID is * delivered via the {@link onRegistered} callback, not as a return value. Call this to establish * an FID-based identity; once {@link onRegistered} provides an FID, instruct your backend to * remove any legacy token previously associated with this instance. The backend send API * supports FID as a target. * * @param messaging - The {@link Messaging} instance. * @param options - Optional. VAPID key and/or service worker registration (same as getToken). * @returns Promise that resolves when registration has been initiated; FID is delivered via onRegistered. * * @public */ export async function register( messaging: Messaging, options?: RegisterOptions ): Promise { messaging = getModularInstance(messaging); return _register(messaging as MessagingService, options); } /** * Unregisters the app instance from FCM by deleting its FID-based registration. * On success, triggers {@link onUnregistered} (if registered) with the unregistered FID. * * @param messaging - The {@link Messaging} instance. * * @public */ export async function unregister(messaging: Messaging): Promise { messaging = getModularInstance(messaging); return _unregister(messaging as MessagingService); } /** * Subscribes to an event that the app instance is registered with FCM via Firebase Installation ID (FID). * Use the FID passed to the callback to upload it to your application server. When you receive an FID * after calling {@link register}, instruct your backend to remove any legacy token for this instance. * * @param messaging - The {@link Messaging} instance. * @param nextOrObserver - A function or observer object called when an FID is registered. * @returns Unsubscribe function to stop listening. * * @public */ export function onRegistered( messaging: Messaging, nextOrObserver: NextFn | Observer ): Unsubscribe { messaging = getModularInstance(messaging); return _onRegistered(messaging as MessagingService, nextOrObserver); } /** * Subscribes to an event that the app instance is unregistered from FCM (FID no longer active). * Use this to notify your backend to remove this FID to prevent 404 errors on send. * * @param messaging - The {@link Messaging} instance. * @param nextOrObserver - A function or observer object called with the unregistered FID. * @returns Unsubscribe function to stop listening. * * @public */ export function onUnregistered( messaging: Messaging, nextOrObserver: NextFn | Observer ): Unsubscribe { messaging = getModularInstance(messaging); return _onUnregistered(messaging as MessagingService, nextOrObserver); } /** * Enables or disables Firebase Cloud Messaging message delivery metrics export to BigQuery. By * default, message delivery metrics are not exported to BigQuery. Use this method to enable or * disable the export at runtime. * * @param messaging - The `FirebaseMessaging` instance. * @param enable - Whether Firebase Cloud Messaging should export message delivery metrics to * BigQuery. * * @public */ export function experimentalSetDeliveryMetricsExportedToBigQueryEnabled( messaging: Messaging, enable: boolean ): void { messaging = getModularInstance(messaging); return _setDeliveryMetricsExportedToBigQueryEnabled(messaging, enable); }