react-native-fcm icon indicating copy to clipboard operation
react-native-fcm copied to clipboard

error: cannot find symbol import com.google.firebase.iid.FirebaseInstanceIdService;

Open thisisbalaG opened this issue 5 years ago • 76 comments

"react-native": "0.55.3" "react-native-fcm": "^16.2.4",

Running on android emulator : android version : 7.0

My project is not running suddenly with the following error. Please help.

<===="ProjectDirectory"====> \node_modules\react-native-fcm\android\src\main\java\com\evollu\react\fcm\InstanceIdService.java:14: error: cannot find symbol import com.google.firebase.iid.FirebaseInstanceIdService; ^ symbol: class FirebaseInstanceIdService location: package com.google.firebase.iid

thisisbalaG avatar May 07 '19 05:05 thisisbalaG

log..

Task :react-native-fcm:compileDebugJavaWithJavac FAILED D:\Balaji Workspace React native\SPIUnlimited\phase 2 Unlimited\unlimited-phase-ii\node_modules\react-native-fcm\android\src\main\java\com\evollu\react\fcm\InstanceIdService.java:14: error: cannot find symbol import com.google.firebase.iid.FirebaseInstanceIdService; ^ symbol: class FirebaseInstanceIdService location: package com.google.firebase.iid D:\Balaji Workspace React native\SPIUnlimited\phase 2 Unlimited\unlimited-phase-ii\node_modules\react-native-fcm\android\src\main\java\com\evollu\react\fcm\InstanceIdService.java:16: error: cannot find symbol public class InstanceIdService extends FirebaseInstanceIdService { ^ symbol: class FirebaseInstanceIdService D:\Balaji Workspace React native\SPIUnlimited\phase 2 Unlimited\unlimited-phase-ii\node_modules\react-native-fcm\android\src\main\java\com\evollu\react\fcm\InstanceIdService.java:26: error: method does not override or implement a method from a supertype @Override ^ D:\Balaji Workspace React native\SPIUnlimited\phase 2 Unlimited\unlimited-phase-ii\node_modules\react-native-fcm\android\src\main\java\com\evollu\react\fcm\InstanceIdService.java:44: error: cannot find symbol ReactInstanceManager mReactInstanceManager = ((ReactApplication) getApplication()).getReactNativeHost().getReactInstanceManager(); ^ symbol: method getApplication() D:\Balaji Workspace React native\SPIUnlimited\phase 2 Unlimited\unlimited-phase-ii\node_modules\react-native-fcm\android\src\main\java\com\evollu\react\fcm\InstanceIdService.java:48: error: cannot find symbol LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(message); ^ symbol: method getApplicationContext() D:\Balaji Workspace React native\SPIUnlimited\phase 2 Unlimited\unlimited-phase-ii\node_modules\react-native-fcm\android\src\main\java\com\evollu\react\fcm\InstanceIdService.java:53: error: cannot find symbol LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(message); ^ symbol: method getApplicationContext() Note: Some input files use or override a deprecated API. Note: Recompile with -Xlint:deprecation for details. 6 errors

FAILURE: Build failed with an exception.

  • What went wrong: Execution failed for task ':react-native-fcm:compileDebugJavaWithJavac'.

Compilation failed; see the compiler error output for details.

  • Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

  • Get more help at https://help.gradle.org

BUILD FAILED in 1m 8s 236 actionable tasks: 228 executed, 8 up-to-date Could not install the app on the device, read the error above for details. Make sure you have an Android emulator running or a device connected and have set up your Android development environment: https://facebook.github.io/react-native/docs/getting-started.html

thisisbalaG avatar May 07 '19 05:05 thisisbalaG

add public void onNewToken(String s) in MessagingService can fixed the issues

TomYan2255 avatar May 07 '19 06:05 TomYan2255

add public void onNewToken(String s) in MessagingService can fixed the issues

But error is located in this file:- \node_modules\react-native-fcm\android\src\main\java\com\evollu\react\fcm\InstanceIdService.java file

ravishankar3961 avatar May 07 '19 06:05 ravishankar3961

Suddenly, I face the same error too, running on android device.

haripermadi avatar May 07 '19 06:05 haripermadi

I made some changes in code in below file, and its working now, also receiving notifications successfully: \node_modules\react-native-fcm\android\src\main\java\com\evollu\react\fcm\InstanceIdService.java file

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 {

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

Edit: i don't know if it is right thing to do or not, can anyone confirm ?

ravishankar3961 avatar May 07 '19 06:05 ravishankar3961

don't use InstanceIdService.java, see https://firebase.google.com/docs/reference/android/com/google/firebase/iid/FirebaseInstanceIdService

TomYan2255 avatar May 07 '19 06:05 TomYan2255

I made some changes in code in below file, and its working now, also receiving notifications successfully: \node_modules\react-native-fcm\android\src\main\java\com\evollu\react\fcm\InstanceIdService.java file

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 {

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

Edit: i don't know if it is right thing to do or not, can anyone confirm ?

maybe use

        <!--<intent-filter>-->
          <!--<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>-->
        <!--</intent-filter>-->
    <!--</service>-->

and add public void onNewToken(String s) in MessagingService

TomYan2255 avatar May 07 '19 06:05 TomYan2255

add public void onNewToken(String s) in MessagingService can fixed the issues

But error is located in this file:- \node_modules\react-native-fcm\android\src\main\java\com\evollu\react\fcm\InstanceIdService.java file

hide InstanceIdService.java don't used the class :)

TomYan2255 avatar May 07 '19 06:05 TomYan2255

It working again after I follow @ravishankar3961 , thanks. It is because depreciated method

haripermadi avatar May 07 '19 06:05 haripermadi

but my push notifications not working

haripermadi avatar May 07 '19 07:05 haripermadi

@thisisbalaG Same error happening with me

MITDD6338 avatar May 07 '19 07:05 MITDD6338

but my push notifications not working

@haripermadi were you able to generate token ?

ravishankar3961 avatar May 07 '19 07:05 ravishankar3961

but my push notifications not working

@haripermadi were you able to generate token ?

yes, I can generate the token. I used this for a chatting feature using qiscus sdk. When I test sending push notif from firebase console, it is working properly, but when I test using my chat it doesn't work.

haripermadi avatar May 07 '19 07:05 haripermadi

@ravishankar3961 I can change this code in my project my App Successfully build but App install in my device has stopped this app

so can We Help me

MITDD6338 avatar May 07 '19 08:05 MITDD6338

It working again after I follow @ravishankar3961 , thanks. It is because depreciated method

i build successfully. but this app crash when run. this is exception : java.lang.NoClassDefFoundError: Failed resolution of: Lcom/google/firebase/auth/FirebaseAuth; at com.google.firebase.auth.FirebaseAuthRegistrar.getComponents(Unknown Source:3)

someone help me :|

duydatpham avatar May 07 '19 08:05 duydatpham

@ravishankar3961 It working again with notification push tested.

suraneti avatar May 07 '19 08:05 suraneti

Thanks @ravishankar3961 It's working now!

NomanGul avatar May 07 '19 09:05 NomanGul

I have same problem. It worked before. But now, It shown the error. After trying @ravishankar3961 's solution, I can build without error now. But I tried to send the notification, it doesn't work

danhnguyeen avatar May 07 '19 09:05 danhnguyeen

Thanks for the help @ravishankar3961. Your solution resolved the error but my app is crashing on launch without any error.

PriyaPatne9 avatar May 07 '19 09:05 PriyaPatne9

Solution resolved the error for me also but app crashes on start

rohit75 avatar May 07 '19 10:05 rohit75

Thanks @ravishankar3961 .Your solution works but application started crashing on launch.

vinay340 avatar May 07 '19 10:05 vinay340

ah, I confirm that @ravishankar3961 's solution is worked for me. Many thanks

danhnguyeen avatar May 07 '19 11:05 danhnguyeen

Thanks @ravishankar3961 . The Solution did worked, you saved the day..!! 👍 The FirebaseInstanceIdService class is deprecated, So FirebaseMessagingService and onNewToken really worked !! Thanks again.

ayansGit avatar May 07 '19 11:05 ayansGit

@ravishankar3961 After doing the changes, I am getting this Error and Build fails.

  • What went wrong: Execution failed for task ':app:transformClassesAndResourcesWithProguardForPreqaRelease'.

Job failed, see logs for details

Krutarth-Dave avatar May 07 '19 11:05 Krutarth-Dave

Hello everyone , Below is the crash log i am getting when app launches.

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.appID, PID: 18120 java.lang.NoClassDefFoundError: Failed resolution of: Lcom/google/firebase/auth/FirebaseAuth; at com.google.firebase.auth.FirebaseAuthRegistrar.getComponents(Unknown Source) at com.google.firebase.components.ComponentRuntime.(com.google.firebase:firebase-common@@17.0.0:56) at com.google.firebase.FirebaseApp.(com.google.firebase:firebase-common@@17.0.0:478)

How to solve the above issue?

vinay340 avatar May 07 '19 12:05 vinay340

Screenshot 2019-05-07 at 5 59 39 PM

getToken() is also deprecated as written in documentation so what to do? should we use getInstanceId() as written

souravkrj avatar May 07 '19 12:05 souravkrj

@ravishankar3961 @haripermadi just facing this error around 3 hours ago, previously was running just fine. I tried @ravishankar3961's solution and the build succeed.

Notification received on background, but not on the foreground. For background notification, click_action is also not working. Probably the best time to switch to react-native-firebase?

theodorusyoga avatar May 07 '19 15:05 theodorusyoga

The @ravishankar3961 's solution worked but it not the right way as a build server will not get the fix. It needs to be added upstream to react-native-fcm.

brianinator avatar May 07 '19 16:05 brianinator

change firebase version in below files: ${project}\android\app\build.gradle

...
//implementation 'com.google.firebase:firebase-core'
//implementation 'com.google.firebase:firebase-messaging'
implementation 'com.google.firebase:firebase-core:16.0.8'
implementation 'com.google.firebase:firebase-messaging:17.6.0'
...

and ${project}\android\gradle.properties At the bottom of file add

firebaseCoreVersion=16.0.8
firebaseMessagingVersion=17.6.0

zhou-ting avatar May 07 '19 16:05 zhou-ting

change firebase version in below files: ${project}\node_modules\react-native-fcm\android\build.gradle,

...
//def DEFAULT_FIREBASE_CORE_VERSION           = "+"
//def DEFAULT_FIREBASE_MESSAGING_VERSION      = "+"
def DEFAULT_FIREBASE_CORE_VERSION           = "16.0.3"
def DEFAULT_FIREBASE_MESSAGING_VERSION      = "17.6.0"
...

and ${project}\android\app\build.gradle

...
//implementation 'com.google.firebase:firebase-core'
//implementation 'com.google.firebase:firebase-messaging'
implementation 'com.google.firebase:firebase-core:16.0.3'
implementation 'com.google.firebase:firebase-messaging:17.6.0'
...

Hmmm... my ${project}\node_modules\react-native-fcm\android\build.gradle originally didn't have def DEFAULT_FIREBASE_CORE_VERSION = "+" def DEFAULT_FIREBASE_MESSAGING_VERSION = "+" this 2 line.

At which part should I add the new lines?

Gabsys avatar May 07 '19 18:05 Gabsys