Skip to content

Commit 853bcb3

Browse files
author
Ivan Chalyk
committed
don't show default bg notification if promise returned from handler
1 parent 602ec18 commit 853bcb3

File tree

2 files changed

+32
-9
lines changed

2 files changed

+32
-9
lines changed

common/api-review/functions-exp.api.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export function getFunctions(app: FirebaseApp, regionOrCustomDomain?: string): F
1616
export function httpsCallable(functionsInstance: Functions, name: string, options?: HttpsCallableOptions): HttpsCallable;
1717

1818
// @public
19-
export function useFunctionsEmulator(functionsInstance: Functions, origin: string): void;
19+
export function useFunctionsEmulator(functionsInstance: Functions, host: string, port: number): void;
2020

2121

2222
// (No @packageDocumentation comment for this package)

packages/messaging/src/controllers/sw-controller.ts

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -187,16 +187,16 @@ export class SwController implements FirebaseMessaging, FirebaseService {
187187
}
188188

189189
// background handling: display and pass to onBackgroundMessage hook
190-
let isNotificationShown = false;
191-
if (!!internalPayload.notification) {
192-
await showNotification(wrapInternalPayload(internalPayload));
193-
isNotificationShown = true;
194-
}
190+
const showDefaultNotification = async function () {
191+
if (!!internalPayload.notification) {
192+
await showNotification(wrapInternalPayload(internalPayload));
193+
}
194+
};
195195

196196
// MessagePayload is only passed to `onBackgroundMessage`. Skip passing MessagePayload for
197197
// the legacy `setBackgroundMessageHandler` to preserve the SDK behaviors.
198198
if (
199-
isNotificationShown === true &&
199+
// isNotificationShown === true &&
200200
this.isOnBackgroundMessageUsed === false
201201
) {
202202
return;
@@ -205,11 +205,34 @@ export class SwController implements FirebaseMessaging, FirebaseService {
205205
if (!!this.bgMessageHandler) {
206206
const payload = externalizePayload(internalPayload);
207207

208+
let bgMessageHandlerResult: any = null;
209+
208210
if (typeof this.bgMessageHandler === 'function') {
209-
this.bgMessageHandler(payload);
211+
bgMessageHandlerResult = this.bgMessageHandler(payload);
210212
} else {
211-
this.bgMessageHandler.next(payload);
213+
bgMessageHandlerResult = this.bgMessageHandler.next(payload);
212214
}
215+
216+
// Show default notification if no response from bgMessageHandler
217+
if (
218+
!bgMessageHandlerResult ||
219+
!(bgMessageHandlerResult instanceof Promise)
220+
) {
221+
await showDefaultNotification();
222+
} else if (bgMessageHandlerResult instanceof Promise) {
223+
const result = await bgMessageHandlerResult;
224+
// Check if result of Promise is undefined
225+
// because docs (https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerRegistration/showNotification)
226+
// says that promise resolves undefined
227+
// then we should skip our default notification
228+
// otherwise show our
229+
if (result !== undefined) {
230+
await showDefaultNotification();
231+
}
232+
}
233+
} else {
234+
// Show default if no bgMessageHandler
235+
await showDefaultNotification();
213236
}
214237
}
215238

0 commit comments

Comments
 (0)