首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >@react本机-firebase/消息传递错误:找不到符号FirebaseInstanceId

@react本机-firebase/消息传递错误:找不到符号FirebaseInstanceId
EN

Stack Overflow用户
提问于 2021-05-14 15:18:58
回答 1查看 2.4K关注 0票数 2

无法在我的React本机项目中解决此错误,该项目使用Firebase Cloud消息传递模块@react本机-火基/消息传递:

代码语言:javascript
复制
BUILD FAILED in 9s

error Failed to install the app. Make sure you have the Android development environment set up: https://reactnative.dev/docs/environment-setup.
Error: Command failed: gradlew.bat app:installDebug -PreactNativeDevServerPort=8081
C:\Users\...\node_modules\@react-native-firebase\messaging\android\src\main\java\io\invertase\firebase\messaging\ReactNativeFirebaseMessagingModule.java:34: error: cannot find symbol
import com.google.firebase.iid.FirebaseInstanceId;
                              ^
  symbol:   class FirebaseInstanceId
  location: package com.google.firebase.iid
C:\Users\...\node_modules\@react-native-firebase\messaging\android\src\main\java\io\invertase\firebase\messaging\ReactNativeFirebaseMessagingModule.java:121: error: cannot find symbol
      .call(getExecutor(), () -> FirebaseInstanceId.getInstance().getToken(authorizedEntity, scope))
                                 ^
  symbol:   variable FirebaseInstanceId
  location: class ReactNativeFirebaseMessagingModule
C:\Users\...\node_modules\@react-native-firebase\messaging\android\src\main\java\io\invertase\firebase\messaging\ReactNativeFirebaseMessagingModule.java:135: error: cannot find symbol
        FirebaseInstanceId.getInstance().deleteToken(authorizedEntity, scope);
        ^
  symbol:   variable FirebaseInstanceId
  location: class ReactNativeFirebaseMessagingModule
3 errors

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':react-native-firebase_messaging:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

试过:

7.0.1.

  • different google-services版本4.3.5,4.3.6,4.3.7

  • applying google-services位于build.gradle app

  • different gradle 版本4.1.3、4.2.0的顶部和底部。4.2.1

  • deleting和重新安装所有node_modules

  • cleaning构建,使用梯度清理,并在每个版本更改

之后重新构建

下面是我的npm列表输出:

代码语言:javascript
复制
├── @babel/core@7.14.2
├── @babel/runtime@7.14.0
├── @react-native-community/cli-platform-android@5.0.1-alpha.1
├── @react-native-community/eslint-config@2.0.0
├── @react-native-firebase/analytics@11.5.0
├── @react-native-firebase/app@11.5.0
├── @react-native-firebase/crashlytics@11.5.0
├── @react-native-firebase/functions@11.5.0
├── @react-native-firebase/messaging@11.5.0
├── babel-jest@26.6.3
├── eslint@7.26.0
├── fast-html-parser@1.0.1
├── jest@26.6.3
├── metro-react-native-babel-preset@0.66.0
├── react-native-keep-awake@4.0.0
├── react-native-mmkv-storage@0.5.8
├── react-native-print@0.7.0
├── react-native-sound-player@0.10.8
├── react-native-webview@11.4.4
├── react-native@0.64.1
├── react-test-renderer@17.0.2
├── react@17.0.2
└── util@0.12.3

我要使用最新的BOM 28.0.1。还尝试了默认@react本机-firebase 26.8.0版本,然后应用程序在开始时崩溃,在build或metro上没有错误输出。App拥有安卓/app的google-services.json,并在firebase注册。目前使用gradle 6.9、plugin 4.1.3和Google-Services4.3.5。

第一次在index.js使用消息时,侦听后台推送消息,如https://rnfirebase.io/messaging/usage的文档中所述

代码语言:javascript
复制
import {AppRegistry} from 'react-native';
import App from './App';
import {name as appName} from './app.json';
import messaging from '@react-native-firebase/messaging';

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

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

稍后,尝试获取设备令牌并调用云功能:

代码语言:javascript
复制
 var switchFunction = functions().httpsCallable(toggleFunctionName);
  messaging()
    .getToken()
    .then((token) => {
      switchFunction({
        organization: encodeURI(parsedLocalData.organizationName),
        user_email: encodeURI(parsedLocalData.userMai),
        device_token: token,
      })
        .then((result) => {
          // Read result of the Cloud Function.
          var sanitizedMessage = result.data.text;
          console.log('Firebase: ' + sanitizedMessage);
        })
        .catch((error) => {
          // Getting the Error details.
          console.log('Firebase error: ' + error);
        });
    });
EN

回答 1

Stack Overflow用户

发布于 2021-05-17 19:35:03

将其添加到依赖项下的app/build.gradle中:

代码语言:javascript
复制
implementation 'com.google.firebase:firebase-messaging:21.1.0'
implementation 'com.google.firebase:firebase-iid'

在ext {}下的顶层build.gradle

代码语言:javascript
复制
firebaseMessagingVersion = "21.1.0"

另外,如果您还没有这样做,那么在顶级build.gradle中的依赖项下:

代码语言:javascript
复制
classpath 'com.google.gms:google-services:4.3.8'

最后,在app/build.gradle的主题上:

代码语言:javascript
复制
apply plugin: "com.android.application"
apply plugin: 'com.google.gms.google-services'
票数 -2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67536765

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档