diff --git a/docs/adr/0040-handle-multiple-attachment-actions b/docs/adr/0040-handle-multiple-attachment-actions.md similarity index 100% rename from docs/adr/0040-handle-multiple-attachment-actions rename to docs/adr/0040-handle-multiple-attachment-actions.md diff --git a/docs/adr/0042-fix-error-no-active-serivce-worker-in-firebase-when-get-token-on-web.md b/docs/adr/0042-fix-error-no-active-serivce-worker-in-firebase-when-get-token-on-web.md new file mode 100644 index 000000000..9e4a8085e --- /dev/null +++ b/docs/adr/0042-fix-error-no-active-serivce-worker-in-firebase-when-get-token-on-web.md @@ -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. diff --git a/lib/features/push_notification/presentation/services/fcm_receiver.dart b/lib/features/push_notification/presentation/services/fcm_receiver.dart index 1f80ea5fe..66ff6fdab 100644 --- a/lib/features/push_notification/presentation/services/fcm_receiver.dart +++ b/lib/features/push_notification/presentation/services/fcm_receiver.dart @@ -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) {