@@ -187,16 +187,16 @@ export class SwController implements FirebaseMessaging, FirebaseService {
187
187
}
188
188
189
189
// 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
+ } ;
195
195
196
196
// MessagePayload is only passed to `onBackgroundMessage`. Skip passing MessagePayload for
197
197
// the legacy `setBackgroundMessageHandler` to preserve the SDK behaviors.
198
198
if (
199
- isNotificationShown === true &&
199
+ // isNotificationShown === true &&
200
200
this . isOnBackgroundMessageUsed === false
201
201
) {
202
202
return ;
@@ -205,11 +205,34 @@ export class SwController implements FirebaseMessaging, FirebaseService {
205
205
if ( ! ! this . bgMessageHandler ) {
206
206
const payload = externalizePayload ( internalPayload ) ;
207
207
208
+ let bgMessageHandlerResult : any = null ;
209
+
208
210
if ( typeof this . bgMessageHandler === 'function' ) {
209
- this . bgMessageHandler ( payload ) ;
211
+ bgMessageHandlerResult = this . bgMessageHandler ( payload ) ;
210
212
} else {
211
- this . bgMessageHandler . next ( payload ) ;
213
+ bgMessageHandlerResult = this . bgMessageHandler . next ( payload ) ;
212
214
}
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 ( ) ;
213
236
}
214
237
}
215
238
0 commit comments