2

Recently I cloned a new project and tried to react-native run-android Unfortunately for some reasons, I can't run my project and the terminal shows me this error: > Task :react-native-firebase:compileDebugJavaWithJavac FAILED, and above this Err I saw more info about this in detail => 'can not find symbol import com.google.firebase.iid.FirebaseInstanceIdService;'.

I searched this issue a lot and I found out that "FirebaseInstanceIdService" is deprecated so I should comment import com.google.firebase.iid.FirebaseInstanceIdService; in InstanceIdService.java located in => \node_modules\react-native-fcm\android\src\main\java\com\evollu\react\fcm\InstanceIdService.java, besides I should make some other changes too. but again I receieve the same error.

The content of InstanceIdService.java (before making any changes):

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();
                    }
                }
            }
        });
    }
}

The content of edited 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();
                    }
                }
            }
        });
    }
}

Please Note That: I have the same problem both in react-native-fcm and react-native-firebase it seems I should make some changes in 'RNFirebaseInstanceIdService.java' and 'RNFirebasePerformance.java'

JUST ADDED THIS NOTE AFTER I SOLVED THE ISSUE MYSELF:: it wasn't about react-native-fcm at all! it was all about react-native-firebase as I said before 'Although the deprecated 'instanceIdService' does exist in react-native-fcm which once I've tried to make some changes.

Thanks

CoderHana
  • 151
  • 1
  • 10
  • my suggestion is to use react-native-firebase lib instead of react-native-fcm, react-native-firebase is up to date with latest changes in Google and so easy to set up just like a charm.https://rnfirebase.io/docs/v5.x.x/getting-started – Moein Hosseini Jun 09 '19 at 12:31
  • @Moein I have the same problem with firebase => I'm trying to mak the same changes in node-modules/react-native-firebase/...[somewhere].../RNFireBaseInstanceIdService.java – CoderHana Jun 09 '19 at 12:48
  • did you follow exact the firebase documentation? you do not need to make any changes to any file from node_module files. so, first, uninstall firebase from your project, then clear your node_module cache, and all likely linked dependencies on your project, and reinstall firebase. if does not work yet, it is probably back to some dependencies for your firebase.please share your android/app/build.gradle and android/build.gradle file. maybe forget to add some dependencies – Moein Hosseini Jun 10 '19 at 04:51
  • Yes, and I tried to update the required packages, but it was too spaghetti and depended to all packages after installing or changing different versions of gradle both for its classPath or distributionURL or making other changes elsewhere and facing other errors continuously, I finally decided to shift+delete the whole node-modules and discard all changes elsewhere and reinstall the packages again `npm install`, I finally make some changes inside of the node-modules -`react-native-firebase` which I'm going to post and share it here – CoderHana Jun 10 '19 at 12:00

2 Answers2

2

So each time I faced the error: > Task :react-native-firebase:compileDebugJavaWithJavac FAILED there would be some problems about the deprecated FirebaseInstanceIdService in RNFirebaseInstanceIdService.java file and also incrementCounter located in RNFirebasePerformance.java .Since, FirebaseInstanceIdService is deprecated I commented that and imported FirebaseMessagingService instead.

(1) the changes I made in 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) the changes I made in 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
  }

.
.
...

so, I made those changes above and after that it worked for me....just like a charm!!

Note It's not professional to edit the node-modules files manually, hence the reason it's much more recommended to fork this.

B-) : please don't hesitate to ask me any questions if it doesn't work for you

CoderHana
  • 151
  • 1
  • 10
0

The project that you have used is using react-native-fcm npm module. The class FirebaseInstanceIdService is a depricated class and has been removed.. In order to generate the token for fcm you can use the function onNewToken in class MessagingInstance.java and delete any reference to class FirebaseInstanceIdService in the project. onNewToken method code:-

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

Please follow the instructions in the link below to overcome your errors:-

FirebaseInstanceIdService is deprecated.

Edit:- in case you are not using push notification in the project, you can remove the entire react-native-fcm module from the project.

  • I have the same problem in react-native-firebase with the same error, both for RNFirebaseInstanceIdService.java & RNFirebasePerfomance.java – CoderHana Jun 10 '19 at 07:43
  • does your AndroidManifest.xml file contain this code... ``` ``` if it does do remove it – Gáüřãv Jõşhï Jun 10 '19 at 09:49
  • best would be to run command npm i react-native-firebase@latest to install the latest package of react-native-firebase to get rid of all the deprecated files. – Gáüřãv Jõşhï Jun 10 '19 at 09:53
  • I tried to install the lastest version, but again I faced other errors...Finally I solve my problem which was mainly the react-native-firebase as I said before by manually changing inside of the node-module – CoderHana Jun 10 '19 at 12:02