尝试使用 React Native 0.63 设置 firebase 时出错

Error when trying to set up firebase with react native 0.63

我是 React Native 和 firebase 的新手,我正在尝试创建一个使用 firebase 在 iOS 内发送后台通知的应用程序。

当我 运行 我的应用程序时,我似乎收到以下错误消息并且我的应用程序不会 运行。

TypeError: (0, _app2.messaging) is not a function. (In '(0, _app2.messaging)()', '(0, _app2.messaging)' is undefined)

这是我的 firebase 代码。

import {name as appName} from './app.json';
import {AppRegistry} from 'react-native';

import {messaging} from '@react-native-firebase/app';

requestUserPermission = async () => {
  const authStatus = await messaging().requestPermission();
  const enabled =
    authStatus === messaging.AuthorizationStatus.AUTHORIZED ||
    authStatus === messaging.AuthorizationStatus.PROVISIONAL;

  if (enabled) {
    console.log('Authorization status:', authStatus);
  }
};

  // Register background handler
   messaging().setBackgroundMessageHandler(async (remoteMessage) => {
   console.log('Messaage handled in the background!', remoteMessage);
 });

 HeadlessCheck = ({ isHeadless }) => {
  if (isHeadless) {
    // App has beeen launched in the background by iOS, ignore
    return null;
  }
  return <App />;
 }

AppRegistry.registerComponent(appName, () => HeadlessCheck);

我可能写错了我的 firebase 代码,因为我是 firebase 的新手,我仍然不确定我在做什么,只是想遵循一些在线教程。

我遇到了 google 这个问题,但似乎没有人遇到过。我尝试再次删除 node_modules 文件夹和 运行 纱线,但这并没有解决问题。

似乎消息传递不存在,我注意到您正在从 @react-native-firebase/app 导入消息传递。按照文档 (https://rnfirebase.io/messaging/usage) 看起来你需要一个不同的模块。

安装消息模块:

# Install the messaging module
yarn add @react-native-firebase/messaging

# If you're developing your app using iOS, run this command
cd ios/ && pod install

正在导入 messaging 以供使用

import messaging from '@react-native-firebase/messaging';