Tal Shani
Tal Shani

Reputation: 690

What is the different between onTokenRefresh to getToken in push notification with firebase?

I'm new with firebase and react-native so im sorry about the questions but I want to know

  1. what is the different between onTokenRefresh to getToken in push notification with firebase?

  2. why not use only with getToken?

  3. why onTokenRefresh is always occurs in this code, I thought it will run this function only if the token actually changed...

    componentDidMount() {
    
    kittensApp.onReady().then(app => {
     app.messaging().getToken()
    .then(fcmToken => {
    if (fcmToken) {
        console.log('fcmtokenApp', fcmToken)
    } else {
      console.log('here 2')
     } 
      })
    
    app.messaging().onTokenRefresh(token => {
    console.log('refreshtoken', token)
      })
         })}
    
  4. another question is in case I want to get the token and send it to the server to save it in the database for pushing notification in the future in what function I should use with (the get token or the refresh)? and how can I make sure that in case the token change (by updating the app or reinstall) it will send the new token to the server? or there is any way to keep the token new without make the user open the app?

Thank you for the help!

Upvotes: 3

Views: 6033

Answers (1)

Robson
Robson

Reputation: 1277

  1. onTokenRefresh is fired with last registered token when new token is generated, but getToken is for register token messaging(if you have registered token, you will get the console.log from your else statement
  2. when new token will be generated you don't get the correct latest token
  3. onTokenRefresh gives you always correct and latest token
  4. Both, on getToken(if you haven't registered token and get one) and on refresh(if its changed), you have to compare it on your server if received token is the same on database and app. Good option is to save also user data to for example redux store from your server and if firebase refresh give you different token, make a request with this new, if not do nothing because it's correct.

Hope it will help.

Upvotes: 1

Related Questions