user15322469
user15322469

Reputation: 909

how can i get token from firebase?

i want to get fcm token from getToken in firebase

but if i use my code token comes out like this

enter image description here

How can i get ?

this is my code

    const App = ({}) => {
      const dispatch = useDispatch();

      useEffect(() => {
        const fcmtoken = messaging()
          .getToken()
          .then((mainToken) => {
            return mainToken;
          });

        console.log('fcmtoken:::', fcmtoken);
      }, []);

Upvotes: 0

Views: 2260

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598807

The getToken() call is asynchronous, and so it returns a promise. That's the structure your console.log actually outputs.

To get the actual value from a promise, you have to hook its then() callback, same as you already do for getToken().

So, this would work:

const fcmtoken = messaging()
  .getToken()
  .then((mainToken) => {
    return mainToken;
  });

fcmtoken.then((token) => console.log('fcmtoken:::', token));

Typically you'll want to dispatch the token when you've gotten it, meaning you'll call dispatch inside the callback, where you now do return mainToken. Just dispatch the token there, and don't return anything.

Upvotes: 1

Related Questions