Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter-Firebase phone Auth always returns Token mismatch on iOS

I'm trying to use phone Authentication and it's working as expected on Android, but on iOS I always get Token mismatch and don't receive a code.

other Firebase services like cloud firestore and email Auth are working fine on iOS.

I made sure of the following:

-APN key is added in Firebase

-Google Services file is updated

-Background Modes and Push Notification capabilities are on

the error message is from PhoneVerificationFailed

Future<void> _verifyPhoneNumber() async {

    setState(() {
      _message = '';
    });

    final PhoneVerificationCompleted verificationCompleted =
        (AuthCredential phoneAuthCredential) async {

      await _auth.signInWithCredential(phoneAuthCredential);

      setState(() {
        _message = 'Received phone auth credential: $phoneAuthCredential';

      });

    };

    final PhoneVerificationFailed verificationFailed =
        (AuthException authException) {
      setState(() {
        _message = '********************\n\n'
            'Phone number verification failed. Code: ${authException.code}.'
            '\n Message: ${authException.message}'
            '\n\n********************';
      });
    };

    final PhoneCodeSent codeSent =
        (String verificationId, [int forceResendingToken]) async {
      _verificationId = verificationId;

      setState(() {
        _message = 'waiting for code';
        //waitingCode = true;
      });
    };

    final PhoneCodeAutoRetrievalTimeout codeAutoRetrievalTimeout =
        (String verificationId) {
      _verificationId = verificationId;
    };

    try {
      await _auth.verifyPhoneNumber(
          phoneNumber: number,
          timeout: const Duration(seconds: 30),
          verificationCompleted: verificationCompleted,
          verificationFailed: verificationFailed,
          codeSent: codeSent,
          codeAutoRetrievalTimeout: codeAutoRetrievalTimeout);
    } catch (e) {
      print('Error is $e');
    }
  }

and I'm getting these messages from log

Userinfo {
    "com.google.firebase.auth" =     {
        warning = "This fake notification should be forwarded to Firebase Auth.";
    };
}
UserDate : {
    "com.google.firebase.auth" =     {
        warning = "This fake notification should be forwarded to Firebase Auth.";
    };
}
UserDate Json : {
  "com.google.firebase.auth" : {
    "warning" : "This fake notification should be forwarded to Firebase Auth."
  }
}
flutter: 


TRUE
flutter: Call Back {
  "com.google.firebase.auth" : {
    "warning" : "This fake notification should be forwarded to Firebase Auth."
  }
}
like image 522
Nizar Avatar asked Oct 20 '25 11:10

Nizar


1 Answers

I understand it's too late in answering this. But I also faced the same error recently. I fixed the issue on iOS. Your entire configuration must be valid. There are two settings you need to make.

  • Remove any method swizzling variable from GoogleService-info.plist i.e. remove FirebaseAppDelegateProxyEnabled property from plist

  • In AppDelegate.swift, override this method and set the following

    func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) { Messaging.messaging().apnsToken = deviceToken }

It is mentioned at https://firebase.google.com/docs/cloud-messaging/ios/client#token-swizzle-disabled

I am using firebase_messaging: ^6.0.16 and the above settings worked

like image 142
Jay Avatar answered Oct 23 '25 00:10

Jay