【问题标题】:React native firebase getToken() not working反应本机firebase getToken()不起作用
【发布时间】:2020-02-03 12:33:21
【问题描述】:

刚刚升级到 react native 0.61.5 和 react native firebase (v5.5.7) getToken() 函数似乎没有运行或者它似乎只是挂在 await 上。

我有这个功能:

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

依次调用:

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

如果我关注https://rnfirebase.io/docs/v5.x.x/messaging/device-token,我似乎无法从服务器取回令牌。如果我尝试console.log(await getToken()),我不会在控制台中得到任何东西。如果我console.log(FCM().getToken()) 我会在控制台中得到一个承诺。我究竟做错了什么?还是我需要升级到最新版本的 react native firebase?

我也检查了权限,并且设备上的权限已打开,因此返回 true。

我确实有一个检查权限的功能,但这是在应用程序的早期完成的,而不是在我正在做的时候。

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

【问题讨论】:

  • 你用的是什么版本的 rnfirebase?
  • 我使用的是 5.5.7 版

标签: javascript react-native react-native-firebase


【解决方案1】:

最终在 iOS 上为我们工作的是将 react 本机 firebase 库更新为 v5.6.0(因为我们还不能更新到 v6)。

这要求我们按照指南将底层 Podfile Firebase 版本更新为 ~6.13.0。做了一个pod install,看起来getToken 函数又可以工作了。

【讨论】:

  • Podfile Firebase 版本 ~6.13.0 包含 UIWebView 您将从 Apple 收到此消息-“MS-90809:已弃用的 API 使用 - Apple 将不再接受从 4 月 30 日起提交使用 UIWebView 的新应用程序, 2020 和截至 2020 年 12 月使用 UIWebView 的应用更新。改为使用 WKWebView 以提高安全性和可靠性”。
【解决方案2】:

请更新 request() && getToken() 之类的函数

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

【讨论】:

  • 我的问题仍然是我没有从 getToken 得到任何回报。它什么也没做。
猜你喜欢
  • 2016-12-10
  • 2022-12-18
  • 2016-07-20
  • 2018-04-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多