ionic-team / capacitor

Build cross-platform Native Progressive Web Apps for iOS, Android, and the Web āš”ļø
https://capacitorjs.com
MIT License
11.7k stars 989 forks source link

bug: Didn't find class "com.google.firebase.iid.FirebaseInstanceId" #4622

Closed iyasilias closed 3 years ago

iyasilias commented 3 years ago

Bug Report

Didn't find class "com.google.firebase.iid.FirebaseInstanceId" when using Push Notification on Android (publish success, but the app will crash when entering page with Push Notification code. When debugging, the error above appears).

Capacitor Version

šŸ’Š   Capacitor Doctor  šŸ’Š 

Latest Dependencies:

  @capacitor/cli: 3.0.0
  @capacitor/core: 3.0.0
  @capacitor/android: 3.0.0
  @capacitor/electron: 3.0.0
  @capacitor/ios: 3.0.0

Installed Dependencies:

  @capacitor/cli 2.4.7
  @capacitor/ios 2.4.2
  @capacitor/android 2.4.2
  @capacitor/core 2.4.2
  @capacitor/electron not installed

[success] Android looking great! šŸ‘Œ
  Found 2 Capacitor plugins for ios:
    @capacitor-community/barcode-scanner (1.2.1)
    @capacitor-community/fcm (1.1.0)
[success] iOS looking great! šŸ‘Œ

Platform(s)

Android

Current Behavior

Problem with Firebase Instant ID

Expected Behavior

Should use the new one since FirebaseInstanceId is deprecated.

Code Reproduction

Other Technical Details

npm --version output: 6.12.1

node --version output: v12.13.1

pod --version output (iOS issues only): 1.10.1

Additional Context

Actually similar bug also appears on ios, but I fix with the guide from here. https://medium.com/nerd-for-tech/how-to-solve-capacitor-error-no-such-module-firebaseinstanceid-9c142933b589

Upon inspecting the debug message, I came across this code

    FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener(getActivity(), new OnSuccessListener<InstanceIdResult>() {
      @Override
      public void onSuccess(InstanceIdResult instanceIdResult) {
        sendToken(instanceIdResult.getToken());
      }
    });
    FirebaseInstanceId.getInstance().getInstanceId().addOnFailureListener(new OnFailureListener() {
      public void onFailure(Exception e) {
        sendError(e.getLocalizedMessage());
      }
    });
    call.success();

Now I'm pretty sure I just have to change FirebaseInstanceId.getInstance().getInstanceId() with new implementation, but I do not know how.. I've tried FirebaseInstallations.getInstance().getId() but seems to have problem with addOnSuccessListener.

I really can't afford Capacitor 3 at this time. Please help.

TQ.

Ionitron commented 3 years ago

This issue may need more information before it can be addressed. In particular, it will need a reliable Code Reproduction that demonstrates the issue.

Please see the Contributing Guide for how to create a Code Reproduction.

Thanks! Ionitron šŸ’™

iyasilias commented 3 years ago

I'll try to reply asap. I've got some errands to do first.

iyasilias commented 3 years ago

I wasn't able to reproduce the error. Maybe it is the result of the complexity of my app. Anyhow, I was able to fix the problem. So I think I would like to share it here.

And yes, it has to do with the deprecation of FirebaseInstanceId.

In PushNotifactions.java from line 84: Change this

public void register(PluginCall call) {
    FirebaseMessaging.getInstance().setAutoInitEnabled(true);
    FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener(getActivity(), new OnSuccessListener<InstanceIdResult>() {
      @Override
      public void onSuccess(InstanceIdResult instanceIdResult) {
        sendToken(instanceIdResult.getToken());
      }
    });

      FirebaseInstanceId.getInstance().getInstanceId().addOnFailureListener(new OnFailureListener() {
        public void onFailure(Exception e) {
          sendError(e.getLocalizedMessage());
        }
      });
    call.success();
  }

to this

public void register(PluginCall call) {
    FirebaseMessaging.getInstance().setAutoInitEnabled(true);

    FirebaseMessaging.getInstance().getToken().addOnSuccessListener(getActivity(), new OnSuccessListener<String>() {
        @Override
        public void onSuccess(String s) {
          sendToken(s);
        }
      });

    FirebaseMessaging.getInstance().getToken().addOnFailureListener(new OnFailureListener() {
      @Override
      public void onFailure(@NonNull Exception e) {
        sendError(e.getLocalizedMessage());
      }
    });

    call.success();
  }
sanderschnydrig commented 3 years ago

This issue shouldn't be closed as it is still present in all versions of Capacitor. You found a workaround but the issue still persists. Also the workaround didnt work for me on Capacitor v2.4.7.

iyasilias commented 3 years ago

does your issue same as mine? if so, i think the workaround should fix the problem.. how about you open a new issue then paste the error message, may be yours a bit different?

sanderschnydrig commented 3 years ago

@iyasilias I think its because I was using PushNotifications.requestPermission() according to older documentation. I found someone was using requestPermissions() with an "s" and it seemed to work now. I will get back to you when I have at this again.

scottwilson312 commented 3 years ago

I can confirm that the documentation for Capacitor 2 is wrong and posts broken code. you need that extra "s" on requestPermission.

barry-jones commented 2 years ago

I have had this error when moving over to this plugin. The issue exists when building and running on an emulator using API 30 but not when running on API 29.

patryk-eco commented 2 years ago

Check this out: https://stackoverflow.com/questions/68320549/flutter-java-lang-noclassdeffounderror-failed-resolution-of-lcom-google-fireb

It says you just have to add

implementation 'com.google.firebase:firebase-iid'

to the gradle file of your app.

It worked for me :+1:

kantharia commented 2 years ago

@patryk-eco your suggestion worked for me

caragonFagor commented 2 years ago

I wasn't able to reproduce the error. Maybe it is the result of the complexity of my app. Anyhow, I was able to fix the problem. So I think I would like to share it here.

And yes, it has to do with the deprecation of FirebaseInstanceId.

In PushNotifactions.java from line 84: Change this

public void register(PluginCall call) {
    FirebaseMessaging.getInstance().setAutoInitEnabled(true);
    FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener(getActivity(), new OnSuccessListener<InstanceIdResult>() {
      @Override
      public void onSuccess(InstanceIdResult instanceIdResult) {
        sendToken(instanceIdResult.getToken());
      }
    });

      FirebaseInstanceId.getInstance().getInstanceId().addOnFailureListener(new OnFailureListener() {
        public void onFailure(Exception e) {
          sendError(e.getLocalizedMessage());
        }
      });
    call.success();
  }

to this

public void register(PluginCall call) {
    FirebaseMessaging.getInstance().setAutoInitEnabled(true);

    FirebaseMessaging.getInstance().getToken().addOnSuccessListener(getActivity(), new OnSuccessListener<String>() {
        @Override
        public void onSuccess(String s) {
          sendToken(s);
        }
      });

    FirebaseMessaging.getInstance().getToken().addOnFailureListener(new OnFailureListener() {
      @Override
      public void onFailure(@NonNull Exception e) {
        sendError(e.getLocalizedMessage());
      }
    });

    call.success();
  }

this worked for me

rbloor97 commented 2 years ago

Check this out: https://stackoverflow.com/questions/68320549/flutter-java-lang-noclassdeffounderror-failed-resolution-of-lcom-google-fireb

It says you just have to add

implementation 'com.google.firebase:firebase-iid'

to the gradle file of your app.

It worked for me šŸ‘

This worked for me

tryroach commented 2 years ago

Check this out: https://stackoverflow.com/questions/68320549/flutter-java-lang-noclassdeffounderror-failed-resolution-of-lcom-google-fireb

It says you just have to add

implementation 'com.google.firebase:firebase-iid'

to the gradle file of your app.

It worked for me šŸ‘

this worked for me šŸ‘

ionitron-bot[bot] commented 1 year ago

Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of Capacitor, please create a new issue and ensure the template is fully filled out.