TypeError(RN 0.61.2):_​​firebaseApp.default.messaging不是函数

时间:2019-10-29 08:58:31

标签: react-native react-native-android react-native-firebase

我将我的应用程序升级到react-native-0.61.2,所以我不得不将react-native-firebase升级到6.0.3,我首先添加了@react-native-firebase/app@react-native-firebase/messaging软件包。

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

// pluck values from your `GoogleService-Info.plist` you created on the firebase console
const iosConfig = {
  clientId: '...',
  appId: '...',
  apiKey: '...',
  databaseURL: '...',
  storageBucket: '...',
  messagingSenderId: '...',
  projectId: '...',
  persistence: true,
}

// pluck values from your `google-services.json` file you created on the firebase console
const androidConfig = {
  clientId: '...',
  appId: '...',
  apiKey: '...',
  databaseURL: '...',
  storageBucket: '...',
  messagingSenderId: '...',
  projectId: '...',
  persistence: true,
}

const firebaseApp = firebase.initializeApp(
  // use platform-specific firebase config
  Platform.OS === 'ios' ? iosConfig : androidConfig,
  // name of this app
  'myApp'
)

export default firebaseApp

我正在执行此操作

import firebaseApp from '...path to/firebaseApp'
  .
  .
  .
  async checkPermission() {
    console.warn('checkPermission firebaseApp : ', firebaseApp)
    const enabled = await firebaseApp.messaging().hasPermission()
    if (!enabled) {
      this.requestPermission()
    }
    this.syncToken()
  }
  .
  .
  .

我得到下一个警告

警告#1:

 checkPermission firebaseApp :  {"_40": 0, "_55": {"_automaticDataCollectionEnabled": false, "_deleteApp": [Function bound deleteApp], "_deleted": false, "_initialized": false, "_intialized": true, "_name": "...", "_nativeInitialized": false, "_options": {"apiKey": "...", "appId": "...", "clientId": "...", "databaseURL": "...", "debug": true, "messagingSenderId": "...", "projectId": "...", "storageBucket": "..."}}, "_65": 1, "_72": null}

警告2:

Possible Unhandled Promise Rejection (id: 0):
TypeError: _firebaseApp.default.messaging is not a function. (In '_firebaseApp.default.messaging()', '_firebaseApp.default.messaging' is undefined)

我做错了什么,有什么帮助吗?

谢谢大家。

2 个答案:

答案 0 :(得分:1)

我应该使用默认应用 因此在我的initializeApp中,我删除了第二个选项myApp 和 此方法更改为

async checkPermission() {
    console.warn('checkPermission firebaseApp : ', firebaseApp)
    firebaseApp
    const enabled = await firebase.messaging().hasPermission()
    if (!enabled) {
      this.requestPermission()
    }
    this.syncToken()
  }

读取Firebase
   import firebase from '@react-native-firebase/app'
   import '@react-native-firebase/messaging'

答案 1 :(得分:0)

来自本机0.60.0。由于React Native引入了Autolinking库功能,因此无需在android和ios中手动链接库或npm,但是我们使用了一些旧的软件包和库。那些npm(react-native-firebase)不会根据react native的新版本进行更新,因此我们需要手动链接这些库和软件包。

您需要手动检查Firebase npm链接过程。

1。将软件包名称导入MainActivity.java文件

enter image description here

2。在appbuild.gradle文件中添加以下依赖项

enter image description here

3。在classpathproject文件中添加build.gradle

enter image description here