Skip to content

Persist FID registration and refresh weekly#9796

Open
zwu52 wants to merge 8 commits intofeat/messaging-api-seriesfrom
feat/messaging-phase4
Open

Persist FID registration and refresh weekly#9796
zwu52 wants to merge 8 commits intofeat/messaging-api-seriesfrom
feat/messaging-phase4

Conversation

@zwu52
Copy link
Copy Markdown
Member

@zwu52 zwu52 commented Mar 31, 2026

1. Enhanced Token Persistence & Lifecycle

  • Persistent Tracking: The timestamp of the last successful Firebase Installation ID (FID) registration is now stored in IndexedDB.
  • Scheduled Refresh: Implemented a 7-day rotation policy to refresh backend registration. This ensures the backend remains synchronized even if the FID itself has not changed.

2. Robust Database Migration & Fallback

  • Resilient Upgrades: Added a safety mechanism when migrating from DB v1 to v2. If the upgrade or opening of v2 fails, the system automatically falls back to v1.
  • Data Preservation: This fallback ensures that existing token data remains readable and prevents service disruptions during schema updates.
  • Verified Reliability: This logic is strictly enforced and guarded by a new suite of unit tests.

3. Expanded Test Coverage

  • Registration Logic: Extended the register() test suite to validate the new weekly refresh behavior.
  • Event Handling: Confirmed that the periodic refresh correctly updates the backend without redundantly re-triggering the onRegistered event.

Summary of Changes

Feature Change Description
Storage Added lastRegistrationTime to IndexedDB schema.
Logic Added a check to trigger registration if currentTime - lastRegistrationTime > 7 days.
Migration Implemented try-catch on DB v2 initialization with a v1 recovery path.
Validation Mocked clock in tests to verify 7-day cadence and onRegistered suppression.

- Persist the last successful FID registration time in IndexedDB and refresh backend registration on a 7-day cadence even when the FID is unchanged
- Fall back to opening DB v1 when opening/upgrading to v2 fails, so existing token data remains readable (guarded by a unit test)
- Extend register() tests to cover weekly refresh without re-firing onRegistered

Made-with: Cursor
@zwu52 zwu52 requested a review from Doris-Ge March 31, 2026 17:44
@zwu52 zwu52 self-assigned this Mar 31, 2026
@zwu52 zwu52 requested a review from a team as a code owner March 31, 2026 17:44
@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Mar 31, 2026

⚠️ No Changeset found

Latest commit: 7ae9b20

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request implements a weekly refresh mechanism for FCM registration by tracking the last registration time in IndexedDB. It includes a database schema update to version 2 with a new object store and a fallback mechanism for failed upgrades. Review feedback identified a race condition and missing multi-tab 'blocking' handlers in the database initialization logic, along with a bug in the database deletion process that could prevent recovery from a corrupted state.

Comment thread packages/messaging/src/internals/idb-manager.ts
Comment thread packages/messaging/src/internals/idb-manager.ts Outdated
zwu52 added 2 commits March 31, 2026 11:01
If the cached dbPromise is rejected (e.g. corrupted IndexedDB), dbDelete previously threw before calling deleteDB, preventing recovery. Always attempt deleteDB and add a regression test.
Comment thread packages/messaging/src/api/register.ts Outdated
Comment thread packages/messaging/src/api/register.ts Outdated
Comment thread packages/messaging/src/internals/idb-manager.ts Outdated
Comment thread packages/messaging/src/internals/idb-manager.ts
Comment thread packages/messaging/src/internals/idb-manager.ts Outdated
@zwu52 zwu52 requested a review from Doris-Ge April 14, 2026 17:22
Comment thread packages/messaging/src/api/register.ts Outdated
Comment thread packages/messaging/src/internals/register-fid.ts Outdated
Comment thread packages/messaging/src/internals/register-fid.ts Outdated
Comment thread packages/messaging/src/internals/requests.ts
Comment thread packages/messaging/src/internals/requests.ts Outdated
Comment thread packages/messaging/src/internals/idb-manager.ts
@zwu52 zwu52 requested a review from Doris-Ge April 15, 2026 22:37
Comment thread packages/messaging/src/internals/register-fid.ts
Comment thread packages/messaging/src/internals/requests.ts Outdated
Comment thread packages/messaging/src/internals/requests.test.ts Outdated
Comment thread packages/messaging/src/api/register.test.ts
Comment thread packages/messaging/src/api/register.test.ts
@zwu52 zwu52 requested a review from Doris-Ge April 16, 2026 23:47
'CreateRegistration succeeded but response body is not valid JSON'
});
}
const fid = data.name;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

name is not just a fid. See my previous comment. You can address this in your next PR if you prefer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants