Simple Code
Simple Code

Reputation: 2584

How to get different FCM registration token in a javascript application?

I am using FCM for pushing notifications and on my javascript side I have this code to get a new token when the user logs in:

    messaging.getToken().then((currentToken) => {
        //sending token to server to be associated with the loggedin user
    }).catch((err) => {
        console.log('An error occurred while retrieving token. ', err);
    });

And when the user logs out I delete that token form my database, but if another user logged in using a different account on the same application(browser) and depending on FCM is generating the same token every time it returns the same token and now it will be associated with a different account.

So, my question is how to get a different token, or is there any workaround to handle the scenario of having the same application(browser) with multiple users can log in?

Upvotes: 1

Views: 1117

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 599736

Firebase Cloud Messaging has no concept of a user; all it knows is the token for a certain app on a certain device.

It's your application that makes that association from that token to a user. That also means that when you break the association in your app, you must force FCM to generate a new token.

The common way to do this is to delete the token when the user signs out by calling deleteToken. After that, the next time you call getToken(), it will generate a new token.

Upvotes: 1

Related Questions