Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

deleteToken does not support serviceWorkerRegistration option like getToken #8621

Open
minht11 opened this issue Oct 30, 2024 · 4 comments
Open

Comments

@minht11
Copy link

minht11 commented Oct 30, 2024

Operating System

all

Environment (if applicable)

all

Firebase SDK Version

10.14.1

Firebase SDK Product(s)

Messaging

Project Tooling

n/a

Detailed Problem Description

When using getToken your function you can provide your own service worker registration file, not needing it to be name firebase-messaging-sw.js it all works fine with getToken, but if you use deleteToken firebase throw an error:

Failed to delete Firebase token FirebaseError: Messaging: We are unable to register the default service worker. Failed to register a ServiceWorker for

with no way to pass the same options.

Steps and code to reproduce issue

Same as description

@minht11 minht11 added new A new issue that hasn't be categoirzed as question, bug or feature request question labels Oct 30, 2024
@google-oss-bot
Copy link
Contributor

I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.

@rizafran rizafran added api: messaging and removed needs-triage new A new issue that hasn't be categoirzed as question, bug or feature request labels Oct 31, 2024
@hsubox76
Copy link
Contributor

I glanced through the code briefly and it looks like the way it's supposed to go is:

  1. When getToken() is called with a custom service worker registration, it adds that registration to the MessagingService instance as messaging.swRegistration.
  2. When deleteToken() is called, it checks to see if messaging.swRegistration is populated, and if not, it tries to register the default service worker.
    if (!messaging.swRegistration) {

It seems like if getToken() is called before deleteToken(), then messaging.swRegistration should exist, and it should skip this conditional.

Is deleteToken() being called in your code without having called getToken() first? Is there a use case for that?

@minht11
Copy link
Author

minht11 commented Nov 1, 2024

Is deleteToken() being called in your code without having called getToken() first? Is there a use case for that?

Likely.
I am calling getToken when initially asking for notifications and when user logins and deleteToken when logouting. Should that be done on each page load instead?

@hsubox76
Copy link
Contributor

hsubox76 commented Nov 1, 2024

Yes, I think that you probably should call getToken() every time your web app is loaded, otherwise you instantiate a messaging instance with no awareness of the registration. It shouldn't make an additional network call if it still has a valid token, if you're worried about that - it should be able to retrieve the token locally from indexedDB.

I think this should fix your problem for now, I'll leave it up to the FCM team if they think a serviceWorkerRegistration option on deleteToken is a feature they think should be added.

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

No branches or pull requests

5 participants