如何修复“:react-native-firebase:compileDebugJavaWithJavac FAILED” +无法找到导入com.google.firebase.iid.FirebaseInstanceIdService;

时间:2019-06-09 11:04:06

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

最近,我克隆了一个新项目,并尝试react-native run-android 不幸的是,由于某些原因,我无法运行我的项目,并且终端向我显示此错误:> Task :react-native-firebase:compileDebugJavaWithJavac FAILED,在此Err上,我详细了解了更多信息=>'找不到符号import com.google。 firebase.iid.FirebaseInstanceIdService;'。

我对此问题进行了很多搜索,发现“ FirebaseInstanceIdService”已被弃用,因此我应该在=> import com.google.firebase.iid.FirebaseInstanceIdService;中的InstanceIdService.java中注释\node_modules\react-native-fcm\android\src\main\java\com\evollu\react\fcm\InstanceIdService.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();
                    }
                }
            }
        });
    }
}

编辑后的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-fcmreact-native-firebase中都遇到了相同的问题,看来我应该对'RNFirebaseInstanceIdService.java'和'{ {1}}'

我自己解决了问题后,便添加了此注释: 这根本不是关于react-native-fcm的内容!正如我之前说过的,这全都是关于react-native-firebase的,尽管我尝试进行一些更改后,react-native-fcm中确实存在过时的'instanceIdService'。

谢谢

2 个答案:

答案 0 :(得分:0)

您使用的项目正在使用react-native-fcm npm模块。 FirebaseInstanceIdService类是一个不完整的类,已被删除。 为了为fcm生成令牌,可以在类MessagingInstance.java中使用onNewToken函数,并删除项目中对FirebaseInstanceIdService类的任何引用。 onNewToken方法代码:-

@Override
    public void onNewToken(String s) {
        super.onNewToken(s);
        Log.d("NEW_TOKEN", s);
    }

请按照以下链接中的说明克服错误:-

FirebaseInstanceIdService is deprecated

编辑:-如果您在项目中不使用推送通知,则可以从项目中删除整个react-native-fcm模块。

答案 1 :(得分:0)

因此,每次遇到错误:> 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-):如果对您不起作用,请随时问我任何问题