Skip to content

re-register on FID change #9759

Merged
zwu52 merged 3 commits intofeat/messaging-api-seriesfrom
feat/messaging-phase3
Mar 31, 2026
Merged

re-register on FID change #9759
zwu52 merged 3 commits intofeat/messaging-api-seriesfrom
feat/messaging-phase3

Conversation

@zwu52
Copy link
Copy Markdown
Member

@zwu52 zwu52 commented Mar 25, 2026

Summary

When the Firebase Installation ID (FID) changes, the Messaging SDK now listens via Firebase Installations onIdChange (including cross-tab updates) and calls register() when an onRegistered handler is set. That re-runs FCM FID registration and delivers the new FID through onRegistered, without requiring the app to poll getId().

Implementation

  • subscribeFidChangeRegistration() uses onIdChange from @firebase/installations.
  • Wired in the window MessagingService factory; unsubscribe stored on MessagingService and cleared in _delete().
  • If no onRegistered handler is registered, the FID-change path does not call register() (avoids invalid-on-registered-handler).
  • Errors from register() in this path are swallowed to avoid unhandled rejections (e.g. revoked permission).

…Change

- Subscribe to Firebase Installations onIdChange when creating window messaging
- On FID change, call register() when onRegistered is set so FCM registration and onRegistered run for the new ID
- Unsubscribe in MessagingService._delete()
- Add unit tests for subscribeFidChangeRegistration

Made-with: Cursor
@zwu52 zwu52 requested a review from a team as a code owner March 25, 2026 21:21
@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Mar 25, 2026

⚠️ No Changeset found

Latest commit: 80e9729

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

@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly enhances the Firebase Messaging SDK by introducing a robust mechanism for automatically updating Firebase Cloud Messaging (FCM) registration when the underlying Firebase Installation ID (FID) changes. By integrating with Firebase Installations' onIdChange event, the SDK now proactively re-registers the FCM token, ensuring that applications always maintain a valid and up-to-date token without requiring developers to implement manual polling or refresh logic. This improves the reliability of message delivery and simplifies the developer experience.

Highlights

  • Automatic FCM Re-registration: The Messaging SDK now automatically re-registers the FCM token when the Firebase Installation ID (FID) changes, ensuring tokens are always current.
  • Firebase Installations Integration: Leveraged Firebase Installations' onIdChange listener to detect FID changes across tabs and trigger the re-registration process.
  • Conditional Registration: The register() function is only called upon FID change if an onRegistered handler is actively set, preventing unnecessary calls.
  • Error Handling: Errors encountered during the re-registration process triggered by an FID change are now gracefully swallowed to prevent unhandled rejections.
  • Lifecycle Management: The FID change subscription is managed within the MessagingService lifecycle, ensuring proper unsubscription when the service is deleted.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Added a TODO comment to refresh the VAPID key weekly.
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 introduces a new mechanism to handle Firebase Installation ID (FID) changes by re-registering FCM tokens. A new helper function, subscribeFidChangeRegistration, is added to listen for FID changes and re-invoke the register function if an onRegisteredHandler is present. The MessagingService is updated to store and clean up the FID change unsubscribe function during service deletion. New tests have been added for this functionality. The review comments suggest improving the test setup in fid-change-registration.test.ts by providing a more robust mock object for installationsApi.Installations instead of casting an empty object, to prevent brittleness.

Comment thread packages/messaging/src/helpers/fid-change-registration.test.ts Outdated
Comment thread packages/messaging/src/helpers/fid-change-registration.test.ts Outdated
Comment thread packages/messaging/src/helpers/register.ts

_delete(): Promise<void> {
if (this._fidChangeUnsubscribe) {
this._fidChangeUnsubscribe();
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Why do we need to call this function before setting it to null?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Ditto above, this callback is only called when messaging instance is bond to be "deleted" (end of the app lifecycle)

}
) as Stub<typeof installationsApi.onIdChange>;

registerStub = stub(registerModule, 'register').resolves() as Stub<
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Is there a way that we can test the whole flow without using a stub? For example, if the fid changes, onRegistered will be invoked with the new fid.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

done

@Doris-Ge Doris-Ge removed their assignment Mar 27, 2026
…register

- Use mutable installations getId and stub requestCreateRegistration only
- Assert onRegistered receives the rotated FID; cover unsubscribe-on-teardown
- Add TODO for periodic refresh in register path

Made-with: Cursor
@zwu52 zwu52 force-pushed the feat/messaging-phase3 branch from efa5bf0 to 80e9729 Compare March 30, 2026 17:21
@zwu52 zwu52 requested a review from Doris-Ge March 30, 2026 17:23
Comment thread packages/messaging/src/helpers/register.ts
@zwu52 zwu52 merged commit b6175af into feat/messaging-api-series Mar 31, 2026
27 of 29 checks passed
@zwu52 zwu52 deleted the feat/messaging-phase3 branch March 31, 2026 16:59
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