evollu / react-native-fcm

react native module for firebase cloud messaging and local notification
MIT License
1.73k stars 681 forks source link

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

Open thisisbalaG opened 5 years ago

thisisbalaG commented 5 years ago

"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 commented 5 years ago

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.

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

TomYan2255 commented 5 years ago

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

ravishankar3961 commented 5 years ago

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

haripermadi commented 5 years ago

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

ravishankar3961 commented 5 years ago

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 ?

TomYan2255 commented 5 years ago

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

TomYan2255 commented 5 years ago

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 commented 5 years ago

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 :)

haripermadi commented 5 years ago

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

haripermadi commented 5 years ago

but my push notifications not working

MITDD6338 commented 5 years ago

@thisisbalaG Same error happening with me

ravishankar3961 commented 5 years ago

but my push notifications not working

@haripermadi were you able to generate token ?

haripermadi commented 5 years ago

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.

MITDD6338 commented 5 years ago

@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

duydatpham commented 5 years ago

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 :|

suraneti commented 5 years ago

@ravishankar3961 It working again with notification push tested.

NomanGul commented 5 years ago

Thanks @ravishankar3961 It's working now!

danhnguyeen commented 5 years ago

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

PriyaPatne9 commented 5 years ago

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

rohit75 commented 5 years ago

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

vinay340 commented 5 years ago

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

danhnguyeen commented 5 years ago

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

ayansGit commented 5 years ago

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

Krutarth-Dave commented 5 years ago

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

vinay340 commented 5 years ago

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?

souravkrj commented 5 years ago
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

theodorusyoga commented 5 years ago

@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?

brianinator commented 5 years ago

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.

zhou-ting commented 5 years ago

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
Gabsys commented 5 years ago

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?

jonathanwmaddison commented 5 years ago

@Gabsys I had the same issue and found out I am several version behind on react-native-fcm. I updated to react-native-fcm": "^16.2.4" and this line is now present in that file.

never00miss commented 5 years ago

apparently it’s because google deprecated FirebaseInstanceService, now you must use FirebaseMessagingService

Bob-JZhao commented 5 years ago

@zhou-ting , thanks for the solution. Alternatively, you can add below configuration in ${project}\android\gradle.properties file. So that every time when you clean the project, don't need to change ${project}\node_modules\react-native-fcm\android\build.gradle again.

googlePlayServicesVersion=16.0.3
firebaseMessagingVersion=17.6.0
ravishankar3961 commented 5 years ago

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.

yeah 😄, i cannot agree more

kyubjn commented 5 years ago

@ravishankar3961 thank bro.

vinay340 commented 5 years ago

@theodorusyoga , No Need to Switch to react-native-firebase. I got a solution to get a push when app is in foreground. simply we need to make use of

FCM.presentLocalNotification()

Function with some inputs when app is in foreground explicitly .Below FCM object helped to solve issue.

FCM.presentLocalNotification({ channel: “channel ID", title:”Title" body: “Body" sound: “default" priority: “High" auto_cancel: true, icon: “icon name" color: “any default colour" vibrate: 300, wake_screen: true, group: "group", ongoing: true, show_in_foreground: true // this is very important });

dev-z commented 5 years ago

@ravishankar3961 The docs for onNewToken(String token) states that

public void onNewToken (String token)
Called when a new token for the default Firebase project is generated.

This is invoked after app install when a token is first generated, and again if the token changes.

So, do we still need this line (below) for getting the updated token?

 String refreshedToken = FirebaseInstanceId.getInstance().getToken();

Isn't the updated token available in the token parameter of this method? Can we just write

@Override
    public void onNewToken(String token) {
        // Get updated InstanceID token.
        String refreshedToken = token;
        ...
        ...
ghost commented 5 years ago

@ravishankar3961 working fine when the app is in background, closed but when the app is in forgeround its not working

ravishankar3961 commented 5 years ago

@ravishankar3961 The docs for onNewToken(String token) states that

public void onNewToken (String token)
Called when a new token for the default Firebase project is generated.

This is invoked after app install when a token is first generated, and again if the token changes.

So, do we still need this line (below) for getting the updated token?

 String refreshedToken = FirebaseInstanceId.getInstance().getToken();

Isn't the updated token available in the token parameter of this method? Can we just write

@Override
    public void onNewToken(String token) {
        // Get updated InstanceID token.
        String refreshedToken = token;
        ...
        ...

yes we can do that, as the onNewToken method is already giving us token

jpventura commented 5 years ago

I've made a quick dirty hack updating InstanceIdService.java and pointing my package.json to my project fork:

"react-native-fcm": "git@github.com:jpventura/react-native-fcm.git#bugfix/issue-1111"

however it would be cleaner if the solution was merged, because I don't intent keeping the fork alive forever 😉

ailton-moreira commented 5 years ago

I have the same issue. If any one found working solution can pls share.

brianinator commented 5 years ago

Since I only depended on Android notifications from this module I decided to migrate to react-native-firebase and it was a smooth transition for me.

suresh-jbt commented 5 years ago

Resolve by doing this :

android/app/build.gradle

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

android/gradle.properties At the bottom of file

firebaseCoreVersion=16.0.3
firebaseMessagingVersion=17.6.0

NOTE: No need to change the version of the library or in node_modules code Just need to update global config using your project gradle.properties file

Cheers guys....

devdany commented 5 years ago

@ravishankar3961 i resolve the problem about build. but. push_notificatioin is not work.

before fix, it worked.

the way get token, is it right?

import FCM from 'react-native-fcm';
const fmc_token = await FCM.getFCMToken().then(token => token);
digitalh2o2 commented 5 years ago

@YourBoB method worked perfectly as well as changing my compiledSdkVersion and targetSdkVersion to 28 in build.gradle(not app one) file. iOS notifications worked fine and since Android is at Oreo i needed to add channel: default to the payload for the notifications to work.

anastely commented 5 years ago

@digitalh2o2 hey, i can get the Fcm Token but when sending the notification to my app i didn't see any notify but in Firebase i see it's done and sent!

- I think issue with this file AndroidManifest.xml

FAILURE: Build failed with an exception.

digitalh2o2 commented 5 years ago

What version are you on @anastely ? I set mine to 16.1.0 and classpath 'com.google.gms:google-services:4.1.0 in my build.gradle file. Also in your build.gradle file make sure to move google() to the top within repositories brackets of buildscript and allprojects

axdamx commented 5 years ago

Since I only depended on Android notifications from this module I decided to migrate to react-native-firebase and it was a smooth transition for me.

can you help me on how to migrate from fcm to react-native-firebase?

iamcxa commented 5 years ago

Resolve by doing this :

android/app/build.gradle

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

android/gradle.properties At the bottom of file

firebaseCoreVersion=16.0.3
firebaseMessagingVersion=17.6.0

NOTE: No need to change the version of the library or in node_modules code Just need to update global config using your project gradle.properties file

Cheers guys....

I can confirm that this way works, thanks

brianinator commented 5 years ago

@axdamx Sure. I'm in react-native-firebase discord.