首页 > 解决方案 > 尝试使用 react native 0.63 设置 firebase 时出错

问题描述

我是对原生和 firebase 做出反应的新手,我正在尝试创建一个使用 firebase 在 iOS 中发送后台通知的应用程序。

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

TypeError: (0, _app2.messaging) 不是函数。(在 '(0, _app2.messaging)()' 中,'(0, _app2.messaging)' 未定义)

这是我的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的新手,我仍然不确定我在做什么,只是想遵循一些在线教程。

我确实谷歌了这个问题,但似乎没有人遇到它。我试图删除我的 node_modules 文件夹并再次运行 yarn,但这并没有解决问题。

标签: firebasereact-nativefirebase-cloud-messagingreact-native-firebase

解决方案


似乎消息不存在,我注意到您正在从@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';

推荐阅读