Skip to content

Implement Unregister #9928

Merged
zwu52 merged 3 commits into
feat/messaging-api-seriesfrom
impl-unregister
May 11, 2026
Merged

Implement Unregister #9928
zwu52 merged 3 commits into
feat/messaging-api-seriesfrom
impl-unregister

Conversation

@zwu52
Copy link
Copy Markdown
Member

@zwu52 zwu52 commented May 6, 2026

  • unregister is backward compat. Calling it only deletes fid if applicable. If token exist is in local db. this method doesn't touch it.

@zwu52 zwu52 requested a review from a team as a code owner May 6, 2026 18:29
@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented May 6, 2026

⚠️ No Changeset found

Latest commit: d5b5eeb

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 introduces the unregister functionality to the Firebase Messaging package, allowing app instances to delete their FID-based registrations from both the server and local IndexedDB storage. The implementation includes new API exports, internal request handling, and comprehensive unit tests. Feedback from the review highlights the need to serialize the unregister operation using _registerNotifyChain to prevent race conditions with concurrent registration calls. Additionally, it is recommended to treat HTTP 404 responses as successful unregistrations and to simplify the error-handling logic in the network request layer.

I am having trouble creating individual review comments. Click here to see my feedback.

packages/messaging/src/api/unregister.ts (33-68)

high

The unregister operation should be serialized using messaging._registerNotifyChain to prevent race conditions with concurrent register calls. This ensures that state updates (like lastNotifiedFid) and event notifications (onUnregistered) occur in the correct order relative to registration events. Additionally, the chain should be kept healthy by catching errors so that subsequent operations are not blocked.

export async function unregister(messaging: MessagingService): Promise<void> {
  if (!navigator) {
    throw ERROR_FACTORY.create(ErrorCode.AVAILABLE_IN_WINDOW);
  }

  const task = messaging._registerNotifyChain.then(async () => {
    // Prefer the last successfully registered FID from local metadata when available.
    const stored = await dbGetFidRegistration(
      messaging.firebaseDependencies
    ).catch(() => undefined);
    const fid =
      stored?.fid ??
      (await messaging.firebaseDependencies.installations.getId());

    await requestDeleteRegistration(messaging.firebaseDependencies, fid);

    // Best-effort local cleanup; still resolve even if schema is unavailable.
    try {
      await dbRemoveFidRegistration(messaging.firebaseDependencies);
    } catch {
      // Ignore.
    }

    if (messaging.lastNotifiedFid === fid) {
      messaging.lastNotifiedFid = null;
    }

    const handler = messaging.onUnregisteredHandler;
    if (handler) {
      if (typeof handler === 'function') {
        handler(fid);
      } else {
        handler.next(fid);
      }
    }
  });

  messaging._registerNotifyChain = task.catch(() => {});
  return task;
}

packages/messaging/src/internals/requests.ts (195-212)

medium

For DELETE requests, a 404 Not Found response should be treated as a success, as it indicates the registration is already gone on the server. This allows the client to proceed with local cleanup. Also, the error handling can be simplified to avoid throwing strings and catching them immediately.

  if (response.ok || response.status === 404) {
    return;
  }

  // Best-effort parse error details; surface uniform error code.
  let errorInfo: string;
  try {
    const responseData = (await response.json()) as ApiResponse;
    errorInfo = responseData.error?.message ?? response.statusText;
  } catch (err) {
    errorInfo = response.statusText || (err as Error)?.toString();
  }

  throw ERROR_FACTORY.create(ErrorCode.FID_UNREGISTER_FAILED, { errorInfo });

@zwu52 zwu52 requested a review from a team as a code owner May 6, 2026 18:36
@zwu52 zwu52 requested a review from Doris-Ge May 6, 2026 18:37
Comment thread packages/messaging/src/api/unregister.ts Outdated
Comment thread packages/messaging/src/api/unregister.ts Outdated
Comment thread packages/messaging/src/api/unregister.ts
Comment thread packages/messaging/src/api/unregister.test.ts Outdated
@zwu52 zwu52 requested a review from Doris-Ge May 7, 2026 19:13
Copy link
Copy Markdown

@Doris-Ge Doris-Ge left a comment

Choose a reason for hiding this comment

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

Can we add two unit tests to verify the changes?

  1. when register() -> deleteToken() -> register() happens, the second register() will still make a call to backend.
  2. when getToken() -> unregister() -> getToken() happens, the second getToken() will make a call to backend.

I assume the changes to invalidate the token/fid cache during register()/getToken() will be included in a separate PR, is that correct?

Comment thread packages/messaging/src/api/unregister.ts
Comment thread packages/messaging/src/internals/token-manager.ts
@Doris-Ge
Copy link
Copy Markdown

Doris-Ge commented May 8, 2026

After this PR is submitted, could you manually verify that a call to unregister() when the app is not registered will still succeed? I'd like to confirm that a delete request to our backend does not error out in this case.

@zwu52
Copy link
Copy Markdown
Member Author

zwu52 commented May 11, 2026

Can we add two unit tests to verify the changes?

  1. when register() -> deleteToken() -> register() happens, the second register() will still make a call to backend.
  2. when getToken() -> unregister() -> getToken() happens, the second getToken() will make a call to backend.

I assume the changes to invalidate the token/fid cache during register()/getToken() will be included in a separate PR, is that correct?

  • added tests
  • yes. following this one

@zwu52
Copy link
Copy Markdown
Member Author

zwu52 commented May 11, 2026

After this PR is submitted, could you manually verify that a call to unregister() when the app is not registered will still succeed? I'd like to confirm that a delete request to our backend does not error out in this case.

noted.

@zwu52 zwu52 merged commit d82390d into feat/messaging-api-series May 11, 2026
30 checks passed
@zwu52 zwu52 deleted the impl-unregister branch May 11, 2026 22:04
@zwu52
Copy link
Copy Markdown
Member Author

zwu52 commented May 11, 2026

ops. merged before commit & sync the fixes. creating a follow up pr

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