Add adr for fix error no active Service Worker of firebase on web

This commit is contained in:
dab246
2024-04-02 02:32:00 +07:00
committed by Dat H. Pham
parent b62588b163
commit abd2b56088
3 changed files with 26 additions and 3 deletions
@@ -0,0 +1,26 @@
# 42. Fix error `Failed to execute 'subscribe' on 'PushManager': Subscription failed - no active Service Worker` when get token on web
Date: 2024-04-02
## Status
- Issues:
- https://github.com/firebase/firebase-js-sdk/issues/7575
- https://github.com/firebase/firebase-js-sdk/issues/7693
## Context
- Sometimes calling messaging `FirebaseMessaging.onMessage.getToken()` returns the error `Failed to execute 'subscribe' on 'PushManager': Subscription failed - no active Service Worker`.
- It seems to occur on the first page load and clear storage with unregister service worker, when the user refreshes the notifications work and there's no error anymore.
## Decision
- There is currently no official solution from the [Firebase](https://github.com/firebase/firebase-js-sdk) team.
- So we will use the following workaround:
- If `FirebaseMessaging.onMessage.getToken()` throw `DomException` then call again get token.
- Try again 3 times with `FirebaseMessaging.onMessage.getToken()`
## Consequences
- We got the token right after trying again.
- Receiving new messages on `foreground` and `background` works fine.
@@ -92,9 +92,6 @@ class FcmReceiver {
return token;
} catch (e) {
logError('FcmReceiver::_getInitialToken: TYPE = ${e.runtimeType} | Exception = $e');
/// Workaround to fix error `no active Service Worker` on Firebase
/// Related Issue: https://github.com/firebase/firebase-js-sdk/issues/7575
/// Related Issue: https://github.com/firebase/firebase-js-sdk/issues/7693
if (PlatformInfo.isWeb
&& e is html.DomException
&& _countRetryToGetFcmToken < MAX_COUNT_RETRY_TO_GET_FCM_TOKEN) {