diff --git a/common/api-review/functions-exp.api.md b/common/api-review/functions-exp.api.md index 947534f6548..4f719b67983 100644 --- a/common/api-review/functions-exp.api.md +++ b/common/api-review/functions-exp.api.md @@ -16,7 +16,7 @@ export function getFunctions(app: FirebaseApp, regionOrCustomDomain?: string): F export function httpsCallable(functionsInstance: Functions, name: string, options?: HttpsCallableOptions): HttpsCallable; // @public -export function useFunctionsEmulator(functionsInstance: Functions, origin: string): void; +export function useFunctionsEmulator(functionsInstance: Functions, host: string, port: number): void; // (No @packageDocumentation comment for this package) diff --git a/packages/messaging/src/controllers/sw-controller.ts b/packages/messaging/src/controllers/sw-controller.ts index 86a4d05af54..80ce966aecc 100644 --- a/packages/messaging/src/controllers/sw-controller.ts +++ b/packages/messaging/src/controllers/sw-controller.ts @@ -187,16 +187,16 @@ export class SwController implements FirebaseMessaging, FirebaseService { } // background handling: display and pass to onBackgroundMessage hook - let isNotificationShown = false; - if (!!internalPayload.notification) { - await showNotification(wrapInternalPayload(internalPayload)); - isNotificationShown = true; - } + const showDefaultNotification = async function () { + if (!!internalPayload.notification) { + await showNotification(wrapInternalPayload(internalPayload)); + } + }; // MessagePayload is only passed to `onBackgroundMessage`. Skip passing MessagePayload for // the legacy `setBackgroundMessageHandler` to preserve the SDK behaviors. if ( - isNotificationShown === true && + // isNotificationShown === true && this.isOnBackgroundMessageUsed === false ) { return; @@ -205,11 +205,34 @@ export class SwController implements FirebaseMessaging, FirebaseService { if (!!this.bgMessageHandler) { const payload = externalizePayload(internalPayload); + let bgMessageHandlerResult: any = null; + if (typeof this.bgMessageHandler === 'function') { - this.bgMessageHandler(payload); + bgMessageHandlerResult = this.bgMessageHandler(payload); } else { - this.bgMessageHandler.next(payload); + bgMessageHandlerResult = this.bgMessageHandler.next(payload); } + + // Show default notification if no response from bgMessageHandler + if ( + !bgMessageHandlerResult || + !(bgMessageHandlerResult instanceof Promise) + ) { + await showDefaultNotification(); + } else if (bgMessageHandlerResult instanceof Promise) { + const result = await bgMessageHandlerResult; + // Check if result of Promise is undefined + // because docs (https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerRegistration/showNotification) + // says that promise resolves undefined + // then we should skip our default notification + // otherwise show our + if (result !== undefined) { + await showDefaultNotification(); + } + } + } else { + // Show default if no bgMessageHandler + await showDefaultNotification(); } }