Skip to content

Commit 54a46f8

Browse files
authored
Set 1s timeout for onBackgroundMessage Hook (#3780)
* await onBackgroundMessage hook * Create fluffy-panthers-hide.md * block in onPush to let onBackgroundMessage to execute * polish wording * Update changeset to be more specific * Update fluffy-panthers-hide.md * Clarify PR is about a bug fix * Update fluffy-panthers-hide.md
1 parent f68ce24 commit 54a46f8

File tree

3 files changed

+32
-8
lines changed

3 files changed

+32
-8
lines changed

.changeset/fluffy-panthers-hide.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@firebase/messaging': patch
3+
---
4+
5+
Adds a timeout for `onBackgroundMessage` hook so that silent-push warnings won't show if `showNotification` is called inside the hook within 1s.
6+
This fixes the issue where the silent-push warning is displayed along with the message shown with [ServiceWorkerRegistration.showNotification](https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerRegistration/showNotification).

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,13 @@
1515
* limitations under the License.
1616
*/
1717

18-
import { DEFAULT_VAPID_KEY, FCM_MSG, TAG } from '../util/constants';
18+
import {
19+
BACKGROUND_HANDLE_EXECUTION_TIME_LIMIT_MS,
20+
DEFAULT_VAPID_KEY,
21+
FCM_MSG,
22+
FOREGROUND_HANDLE_PREPARATION_TIME_MS,
23+
TAG
24+
} from '../util/constants';
1925
import { ERROR_FACTORY, ErrorCode } from '../util/errors';
2026
import { FirebaseMessaging, MessagePayload } from '@firebase/messaging-types';
2127
import {
@@ -47,8 +53,8 @@ export class SwController implements FirebaseMessaging, FirebaseService {
4753
private isOnBackgroundMessageUsed: boolean | null = null;
4854
private vapidKey: string | null = null;
4955
private bgMessageHandler:
50-
| BgMessageHandler
5156
| null
57+
| BgMessageHandler
5258
| NextFn<MessagePayload>
5359
| Observer<MessagePayload> = null;
5460

@@ -211,6 +217,9 @@ export class SwController implements FirebaseMessaging, FirebaseService {
211217
this.bgMessageHandler.next(payload);
212218
}
213219
}
220+
221+
// wait briefly to allow onBackgroundMessage to complete
222+
await sleep(BACKGROUND_HANDLE_EXECUTION_TIME_LIMIT_MS);
214223
}
215224

216225
async onSubChange(event: PushSubscriptionChangeEvent): Promise<void> {
@@ -267,7 +276,7 @@ export class SwController implements FirebaseMessaging, FirebaseService {
267276

268277
// Wait three seconds for the client to initialize and set up the message handler so that it
269278
// can receive the message.
270-
await sleep(3000);
279+
await sleep(FOREGROUND_HANDLE_PREPARATION_TIME_MS);
271280
} else {
272281
client = await client.focus();
273282
}

packages/messaging/src/util/constants.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,21 @@ export const DEFAULT_VAPID_KEY =
2323

2424
export const ENDPOINT = 'https://fcmregistrations.googleapis.com/v1';
2525

26-
/** Key of FCM Payload in Notification's data field. */
26+
// Key of FCM Payload in Notification's data field.
2727
export const FCM_MSG = 'FCM_MSG';
28+
export const TAG = 'FirebaseMessaging: ';
2829

30+
// Set to '1' if Analytics is enabled for the campaign
31+
export const CONSOLE_CAMPAIGN_ANALYTICS_ENABLED = 'google.c.a.e';
2932
export const CONSOLE_CAMPAIGN_ID = 'google.c.a.c_id';
30-
export const CONSOLE_CAMPAIGN_NAME = 'google.c.a.c_l';
3133
export const CONSOLE_CAMPAIGN_TIME = 'google.c.a.ts';
32-
/** Set to '1' if Analytics is enabled for the campaign */
33-
export const CONSOLE_CAMPAIGN_ANALYTICS_ENABLED = 'google.c.a.e';
34-
export const TAG = 'FirebaseMessaging: ';
34+
export const CONSOLE_CAMPAIGN_NAME = 'google.c.a.c_l';
35+
36+
// Due to the fact that onBackgroundMessage can't be awaited (to support rxjs), a silent push
37+
// warning might be shown by the browser if the callback fails to completes by the end of onPush.
38+
// Experiments were ran to determine the majority onBackground message clock time. This brief
39+
// blocking time would allow majority of the onBackgroundMessage callback to finish.
40+
export const BACKGROUND_HANDLE_EXECUTION_TIME_LIMIT_MS = 1000;
41+
42+
// Preparation time for client to initialize and set up the message handler.
43+
export const FOREGROUND_HANDLE_PREPARATION_TIME_MS = 3000;

0 commit comments

Comments
 (0)