简体   繁体   中英

React native firebase getToken() not working

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.xx/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;
  }
}

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)
    }
}

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