简体   繁体   中英

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

在此处输入图像描述

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);
      }, []);

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM