admin管理员组

文章数量:1323739

Just upgraded to react native 0.61.5 and react native firebase (v5.5.7) getToken() function doesn't seem to run or it just seems to hang on the await.

I have this function:

async function updatePushNotificationsEnabled(isEnabled: boolean) {
  return updateUser({ pushNotificationsEnabled: isEnabled, firebaseId: await getToken() })
}

which in turn is calling:

async function getToken() {
  try {
    return FCM().getToken();
  } catch (error) {
    return undefined;
  }
}

If I follow .x.x/messaging/device-token I can't seem to get a token back from the server. If I try console.log(await getToken()) I don't get anything in console. If I console.log(FCM().getToken()) I get a promise in the console. What am I doing wrong? or am I needing to upgrade to the latest version of react native firebase?

I have also checked permissions and the permission on the device is on so returns true.

I do have a function to check for permissions but this is done earlier in the app and not at the point of what I am doing.

async function requestPermissionIfNeeded() {
  try {
    if (await FCM().hasPermission()) {
      return true;
    }
    await FCM().requestPermission();
    return await FCM().hasPermission();
  } catch (error) {
    return false;
  }
}

Just upgraded to react native 0.61.5 and react native firebase (v5.5.7) getToken() function doesn't seem to run or it just seems to hang on the await.

I have this function:

async function updatePushNotificationsEnabled(isEnabled: boolean) {
  return updateUser({ pushNotificationsEnabled: isEnabled, firebaseId: await getToken() })
}

which in turn is calling:

async function getToken() {
  try {
    return FCM().getToken();
  } catch (error) {
    return undefined;
  }
}

If I follow https://rnfirebase.io/docs/v5.x.x/messaging/device-token I can't seem to get a token back from the server. If I try console.log(await getToken()) I don't get anything in console. If I console.log(FCM().getToken()) I get a promise in the console. What am I doing wrong? or am I needing to upgrade to the latest version of react native firebase?

I have also checked permissions and the permission on the device is on so returns true.

I do have a function to check for permissions but this is done earlier in the app and not at the point of what I am doing.

async function requestPermissionIfNeeded() {
  try {
    if (await FCM().hasPermission()) {
      return true;
    }
    await FCM().requestPermission();
    return await FCM().hasPermission();
  } catch (error) {
    return false;
  }
}
Share Improve this question edited Feb 3, 2020 at 14:04 saunders asked Feb 3, 2020 at 12:33 saunderssaunders 9894 gold badges14 silver badges25 bronze badges 2
  • what version of rnfirebase are you using? – Gaurav Roy Commented Feb 3, 2020 at 13:47
  • i am using version 5.5.7 – saunders Commented Feb 3, 2020 at 14:04
Add a ment  | 

2 Answers 2

Reset to default 3

What ended up working for us on iOS was updating the react native firebase library to v5.6.0 (as we aren't in a position to update to v6 yet).

This required us to update the underlying Podfile Firebase versions to ~6.13.0 as per the guide. Did a pod install and it looks like the getToken function is working again.

please update request() && getToken() function like

async requestPermission() {
        try {
          await firebase.messaging().requestPermission();
          this.getToken();
        } catch (error) {
          alert('permission rejected');
        }
    }

async getToken() {
    let fcmToken = await AsyncStorage.getItem('device_token');
    if (!fcmToken) {
        fcmToken = await firebase.messaging().getToken();
        if (fcmToken) {
            await AsyncStorage.setItem('device_token', fcmToken);
        }
    } else {
        // do some work
        console.log('Device_token')
        console.log(fcmToken)
    }
}

本文标签: javascriptReact native firebase getToken() not workingStack Overflow