【发布时间】:2019-06-09 11:04:06
【问题描述】:
最近我克隆了一个新项目并尝试react-native run-android
不幸的是,由于某些原因,我无法运行我的项目并且终端向我显示此错误:> Task :react-native-firebase:compileDebugJavaWithJavac FAILED,在此错误之上,我详细看到了有关此的更多信息 => '找不到符号导入 com.google.firebase。 iid.FirebaseInstanceIdService;'。
我对这个问题进行了很多搜索,发现“FirebaseInstanceIdService”已被弃用,因此我应该在位于 => \node_modules\react-native-fcm\android\src\main\java\com\evollu\react\fcm\InstanceIdService.java 的 InstanceIdService.java 中评论 import com.google.firebase.iid.FirebaseInstanceIdService;,此外我还应该进行一些其他更改。但我又收到了同样的错误。
InstanceIdService.java 的内容(修改前):
package com.evollu.react.fcm;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import com.facebook.react.ReactApplication;
import com.facebook.react.ReactInstanceManager;
import com.facebook.react.bridge.ReactContext;
import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.FirebaseInstanceIdService;
public class InstanceIdService extends FirebaseInstanceIdService {
private static final String TAG = "InstanceIdService";
/**
* Called if InstanceID token is updated. This may occur if the security of
* the previous token had been compromised. This call is initiated by the
* InstanceID provider.
*/
// [START refresh_token]
@Override
public void onTokenRefresh() {
// Get updated InstanceID token.
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.d(TAG, "Refreshed token: " + refreshedToken);
// Broadcast refreshed token
Intent i = new Intent("com.evollu.react.fcm.FCMRefreshToken");
Bundle bundle = new Bundle();
bundle.putString("token", refreshedToken);
i.putExtras(bundle);
final Intent message = i;
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
public void run() {
// Construct and load our normal React JS code bundle
ReactInstanceManager mReactInstanceManager = ((ReactApplication) getApplication()).getReactNativeHost().getReactInstanceManager();
ReactContext context = mReactInstanceManager.getCurrentReactContext();
// If it's constructed, send a notification
if (context != null) {
LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(message);
} else {
// Otherwise wait for construction, then send the notification
mReactInstanceManager.addReactInstanceEventListener(new ReactInstanceManager.ReactInstanceEventListener() {
public void onReactContextInitialized(ReactContext context) {
LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(message);
}
});
if (!mReactInstanceManager.hasStartedCreatingInitialContext()) {
// Construct it in the background
mReactInstanceManager.createReactContextInBackground();
}
}
}
});
}
}
编辑的InstanceIdService.java的内容:
package com.evollu.react.fcm;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import com.facebook.react.ReactApplication;
import com.facebook.react.ReactInstanceManager;
import com.facebook.react.bridge.ReactContext;
import com.google.firebase.iid.FirebaseInstanceId;
//import com.google.firebase.iid.FirebaseInstanceIdService; //Commented FirebaseInstanceIdService
import com.google.firebase.messaging.FirebaseMessagingService; //ADD FirebaseMessagingService
// public class InstanceIdService extends FirebaseMessagingService {
public class MyFireBaseInstanceIDService extends FirebaseMessagingService {
private static final String TAG = "InstanceIdService";
// private static final String TAG = MyFireBaseInstanceIDService.class.getSimpleName();;
/**
* Called if InstanceID token is updated. This may occur if the security of
* the previous token had been compromised. This call is initiated by the
* InstanceID provider.
*/
// [START refresh_token]
@Override
public void onNewToken(String token) { //Added onNewToken method
// Get updated InstanceID token.
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.d(TAG, "Refreshed token: " + refreshedToken);
// Broadcast refreshed token
Intent i = new Intent("com.evollu.react.fcm.FCMRefreshToken");
Bundle bundle = new Bundle();
bundle.putString("token", refreshedToken);
i.putExtras(bundle);
final Intent message = i;
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
public void run() {
// Construct and load our normal React JS code bundle
ReactInstanceManager mReactInstanceManager = ((ReactApplication) getApplication()).getReactNativeHost().getReactInstanceManager();
ReactContext context = mReactInstanceManager.getCurrentReactContext();
// If it's constructed, send a notification
if (context != null) {
LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(message);
} else {
// Otherwise wait for construction, then send the notification
mReactInstanceManager.addReactInstanceEventListener(new ReactInstanceManager.ReactInstanceEventListener() {
public void onReactContextInitialized(ReactContext context) {
LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(message);
}
});
if (!mReactInstanceManager.hasStartedCreatingInitialContext()) {
// Construct it in the background
mReactInstanceManager.createReactContextInBackground();
}
}
}
});
}
}
请注意:我在react-native-fcm 和react-native-firebase 都有同样的问题,看来我应该对'RNFirebaseInstanceIdService.java'和'RNFirebasePerformance.java'进行一些更改
我自己解决了这个问题后才添加了这个注释:: 这根本不是关于 react-native-fcm 的!正如我之前所说的,这完全是关于 react-native-firebase 的,“尽管在 react-native-fcm 中确实存在已弃用的 'instanceIdService',一旦我尝试进行一些更改。
谢谢
【问题讨论】:
-
我的建议是使用 react-native-firebase lib 而不是 react-native-fcm,react-native-firebase 与 Google 的最新更改保持同步,因此易于设置,就像魅力。rnfirebase.io/docs/v5.x.x/getting-started
-
@Moein 我对 firebase 有同样的问题 => 我正在尝试在 node-modules/react-native-firebase/...[somewhere].../RNFireBaseInstanceIdService 中进行相同的更改.java
-
您是否严格按照 firebase 文档进行操作?您不需要对 node_module 文件中的任何文件进行任何更改。因此,首先,从您的项目中卸载 firebase,然后清除您的 node_module 缓存以及项目上所有可能链接的依赖项,然后重新安装 firebase。如果还不能正常工作,它可能会回到你的 firebase 的一些依赖项。请分享你的 android/app/build.gradle 和 android/build.gradle 文件。可能忘记添加一些依赖项
-
是的,我尝试更新所需的软件包,但它太意大利面了,并且在安装或更改不同版本的 gradle 后依赖于所有软件包的 classPath 或 distributionURL 或在其他地方进行其他更改并面临其他错误不断,我终于决定转移+删除整个节点模块并丢弃其他地方的所有更改并再次重新安装软件包
npm install,我终于在节点模块内部进行了一些更改 -react-native-firebase我要去在这里发布和分享
标签: android firebase react-native firebase-cloud-messaging