我在反应原生项目中发生了大多数android / java运行时问题。我不认为react-native与该问题有任何关系,但可能有助于理解所涉及的代码或错误。我一直在收到以下错误:
java.lang.RuntimeException:无法调用RNFIRMessaging.subscribeToTopic
这是来自反应本机框架。方法中的try-catch不会捕获错误,但代码会一直执行到调用FirebaseMessaging.getInstance().subscribeToTopic(topic);
的位置。
这是真正的罪魁祸首的内部错误:
...引起:java.lang.NoSuchMethodError:没有虚拟方法subscribeToTopic(Ljava / lang / String;)Lcom / google / android / gms / tasks / Task;在Lcom / google / firebase / messaging / FirebaseMessaging中;或其超级类('com.google.firebase.messaging.FirebaseMessaging'的声明出现在......
这是与RNFIRMessaging.subscribeToTopic
对应的代码:
public class FIRMessagingModule extends ReactContextBaseJavaModule implements LifecycleEventListener, ActivityEventListener {
...
@Override
public String getName() {
return "RNFIRMessaging";
}
...
public void subscribeToTopic(String topic) {
try {
Log.w("MYFCM", "Trying to call subscribeToTopic");
FirebaseMessaging x = FirebaseMessaging.getInstance();
if (x == null)
Log.w("MYFCM", "hasinstance?: NO");
else {
Log.w("MYFCM", "hasinstance?: YES");
// use reflection to print the methods
Class c = x.getClass();
for (java.lang.reflect.Method method : c.getDeclaredMethods()) {
Log.w("MYFCM", "methods: " + method.getName());
}
}
FirebaseMessaging.getInstance().subscribeToTopic(topic);
} catch(Exception ex) {
Log.e("MYFCM", ex.getMessage());
}
}
...
}
这将从adb logcat
生成以下日志输出。请注意反射似乎认为subscribeToTopic
方法存在。
05-29 12:21:46.760 8188 8261 W MYFCM : Trying to call subscribeToTopic
05-29 12:21:46.760 8188 8261 W MYFCM : Trying hasinstance?: YES
05-29 12:21:46.760 8188 8261 W MYFCM : methods: getInstance
05-29 12:21:46.760 8188 8261 W MYFCM : methods: isAutoInitEnabled
05-29 12:21:46.760 8188 8261 W MYFCM : methods: send
05-29 12:21:46.760 8188 8261 W MYFCM : methods: setAutoInitEnabled
05-29 12:21:46.760 8188 8261 W MYFCM : methods: subscribeToTopic
05-29 12:21:46.760 8188 8261 W MYFCM : methods: unsubscribeFromTopic
05-29 12:21:46.761 8188 8261 E unknown:ReactNative: Exception in native call
05-29 12:21:46.761 8188 8261 E unknown:ReactNative: java.lang.RuntimeException: Could not invoke RNFIRMessaging.subscribeToTopic
我怀疑我的gradle文件可能是这个问题的范围:
/android/build.gradle:
buildscript {
dependencies {
classpath 'com.android.tools.build:gradle:3.1.2'
classpath 'com.google.gms:google-services:3.2.1'
}
...
}
ext {
targetSdkVersion = 25
compileSdkVersion = 25
buildToolsVersion = "27.0.3"
googlePlayServicesVersion = "15.0.2"
firebaseVersion = "15.0.2"
supportLibVersion = "25.4.0"
reactNativeVersion = "0.53.3"
}
/android/app/build.gradle:
dependencies {
...
implementation("com.google.firebase:firebase-core:$firebaseVersion") { force = true }
implementation("com.google.firebase:firebase-messaging:$firebaseVersion") { force = true }
implementation("com.android.support:exifinterface:$supportLibVersion") { force = true }
implementation("com.google.android.gms:play-services-vision:$googlePlayServicesVersion") { force = true }
implementation ("com.google.android.gms:play-services-gcm:$googlePlayServicesVersion") { force = true }
...
}
...
apply plugin: "com.google.gms.google-services"
如果重要,我正在使用react-native-fcm。上面的代码片段是来自该存储库的修改代码。
之前的研究:
force = true
应防止多个firebase副本浮动。 minifyEnabled false
)如何解决此问题?
答案 0 :(得分:4)
我遇到了完全相同的问题,它与新版本的firebase(https://firebase.google.com/support/release-notes/android#20180523)相关
我能够通过修改react native fcm库中的文件来修复它(暂时):
./ node_modules /反应天然-FCM /机器人/的build.gradle
你必须替换:
compile 'com.google.firebase:firebase-core:+'
compile 'com.google.firebase:firebase-messaging:+'
根据您要使用的特定版本,在您的情况下我认为它是15.0.2所以它应该是:
compile 'com.google.firebase:firebase-core:15.0.2'
compile 'com.google.firebase:firebase-messaging:15.0.2'
这将通过指定您需要的版本来阻止使用最新版本的库。