Skip to content

don't show default bg notification if promise returned from handler #3976

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion common/api-review/functions-exp.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
39 changes: 31 additions & 8 deletions packages/messaging/src/controllers/sw-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
}
}

Expand Down