最近我克隆了一个新项目并试图react-native run-android
不幸的是,由于某些原因,我无法运行我的项目,终端向我显示此错误:> Task :react-native-firebase:compileDebugJavaWithJavac FAILED
,在此错误上方,我详细看到了有关此的更多信息=>"找不到符号导入com.google.firebase.iid.FirebaseInstanceIdService;"。
我搜索了很多这个问题,我发现"FirebaseInstanceIdService"已被弃用,所以我应该在 InstanceIdService 中import com.google.firebase.iid.FirebaseInstanceIdService;
发表评论.java位于 =>node_modulesreact-native-fcmandroidsrcmainjavacomevollureactfcmInstanceIdService.java
,此外我还应该进行一些其他更改。 但我再次收到同样的错误。
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();
}
}
}
});
}
}
编辑后的实例身份服务.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
"进行一些更改
在我自己解决问题后刚刚添加了这个注释::这根本不是关于反应原生-FCM! 正如我之前所说,这一切都是关于 react-native-firebase 的,"尽管弃用的'instanceIdService'确实存在于 react-native-fcm 中,一旦我尝试进行一些更改。
谢谢
所以每次我都遇到错误:> Task :react-native-firebase:compileDebugJavaWithJavac FAILED
文件中不推荐使用的FirebaseInstanceIdService
会有一些问题,RNFirebaseInstanceIdService.java
incrementCounter
位于RNFirebasePerformance.java
中。由于FirebaseInstanceIdService
已被弃用,因此我对此进行了评论并导入了FirebaseMessagingService
。
(1)我在RNFirebaseInstanceIdService.java中所做的更改:
package io.invertase.firebase.messaging;
import android.content.Intent;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
// import com.google.firebase.iid.FirebaseInstanceIdService; //I commented this once because it's deprecated
import com.google.firebase.messaging.FirebaseMessagingService; //I added this one
public class RNFirebaseInstanceIdService extends FirebaseMessagingService {
private static final String TAG = "RNFInstanceIdService";
public static final String TOKEN_REFRESH_EVENT = "messaging-token-refresh";
// I commented the below codes
// @Override
// public void onTokenRefresh() {
// Log.d(TAG, "onTokenRefresh event received");
// // Build an Intent to pass the token to the RN Application
// Intent tokenRefreshEvent = new Intent(TOKEN_REFRESH_EVENT);
// // Broadcast it so it is only available to the RN Application
// LocalBroadcastManager.getInstance(this).sendBroadcast(tokenRefreshEvent);
// }
}
(2)我在RNFirebasePerformance中所做的更改.java
...
.
.
//somewhere in the file I searched for incrementCounter and commented it
@ReactMethod
public void incrementCounter(String identifier, String event) {
// getOrCreateTrace(identifier).incrementCounter(event); //I commented this line
}
.
.
...
所以,我在上面做了这些更改,之后它对我有用......就像一个魅力!!
注意手动编辑节点模块文件并不专业,因此更建议将其分叉。
B-): 如果它不适合您,请不要犹豫,问我任何问题
您使用的项目正在使用 react-native-fcm npm 模块。 类 FirebaseInstanceIdService 是一个已弃用的类,已被删除。 为了生成 fcm 的令牌,您可以在类 MessagingInstance 中使用函数 onNewToken.java并删除项目中对类 FirebaseInstanceIdService 的任何引用。 onNewToken 方法代码:-
@Override
public void onNewToken(String s) {
super.onNewToken(s);
Log.d("NEW_TOKEN", s);
}
请按照以下链接中的说明克服您的错误:-
FirebaseInstanceIdService 已弃用。
编辑:- 如果您在项目中没有使用推送通知,您可以从项目中删除整个 react-native-fcm 模块。