Flutter Firebase电话身份验证代码未通过短信发送

时间:2019-07-26 15:45:31

标签: swift firebase flutter firebase-authentication apple-push-notifications

我正在尝试在我的flutter项目中启用Firebase电话身份验证,以便它实际将带有身份验证代码的SMS发送到输入的号码。

-将APNs身份验证密钥上载到“云消息”下的Firebase项目设置中

-我还添加了:

io.flutter.embedded_views_preview
<string>NO</string>

到info.plist文件

然后我在Xcode项目中激活了后台模式->远程通知和推送通知,并在AppDelegate.swift文件中实现了这两个功能:

override func application(_ application: UIApplication, 
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) 
{
    // Pass device token to auth
        Auth.auth().setAPNSToken(deviceToken, type: 
AuthAPNSTokenType.prod)

    // Further handling of the device token if needed by the app
    // ...
  }

  override func application(_ application: UIApplication,
      didReceiveRemoteNotification notification: [AnyHashable : 
Any],
      fetchCompletionHandler completionHandler: @escaping 
(UIBackgroundFetchResult) -> Void) {
    if Auth.auth().canHandleNotification(notification) {
        completionHandler(UIBackgroundFetchResult.noData)
      return
    }
    // This notification is not auth related, developer should 
handle it.
  }

这是处理登录屏幕中整个电话身份验证过程的代码

Future<void> verifyPhone() async {
final PhoneCodeAutoRetrievalTimeout autoRetrieve = (String verID) {
  this.verificationID = verID;
};

final PhoneCodeSent smsCodeSent = (String verID, [int 
forceCodeResend]) {

  this.verificationID = verID;
  smsCodeDialog(context).then((value) {
  });
};

final PhoneVerificationCompleted verificationSuccess =
    (AuthCredential credential) {
};

final PhoneVerificationFailed verificationFailed =
    (AuthException exception) {
  print("verification failed this bullshit");
  if (exception.message.contains('not authorized'))
    print(
        'Something weird has gone really wrong, please do not try 
later');
  else if (exception.message.contains('Network'))
    print('Please check your internet connection and try again');
  else if (exception.message.contains("credential is invalid"))
    print("credential is invalid you jerk");
  else
    print('Something has gone horribly wrong, please try later or 
never -> ${exception.message}');
};

await _auth.verifyPhoneNumber(
    phoneNumber: "+" + dialingCode + this.phoneNo,
    timeout: const Duration(seconds: 5),
    verificationCompleted: verificationSuccess,
    verificationFailed: verificationFailed,
    codeSent: smsCodeSent,
    codeAutoRetrievalTimeout: autoRetrieve);
}


  signIn() async {
    final AuthCredential credential = 
PhoneAuthProvider.getCredential(
      verificationId: verificationID,
      smsCode: smsCode,
    );
    final FirebaseUser user = await 
_auth.signInWithCredential(credential);
    final FirebaseUser currentUser = await _auth.currentUser();
    assert(user.uid == currentUser.uid);

    Navigator.pushReplacementNamed(context, "root");
  }

当我输入电话号码并在物理iPhone X上按“发送短信”时,我希望收到带有身份验证密钥的短信,但会收到以下错误消息:

The UIApplicationDelegate must handle remote notification for phone 
number authentication to work.

If app delegate swizzling is disabled, remote notifications received 
by UIApplicationDelegate need to be forwarded to FIRAuth s 
canHandleNotificaton: method.

1 个答案:

答案 0 :(得分:0)

可能您正在使用该问题附带的firebase_auth版本,可以在此flutter issue

中查看有关该版本的信息

对我来说,解决方案是添加firebase_auth的这个分叉:

firebase_auth:
git:
  url: https://github.com/collinjackson/plugins.git
  ref: 441417c2fed0ff26bf84a49ab2c5ffd2aa5487de
  path: packages/firebase_auth

或者可能需要将firebase_auth更新为版本0.11.1+8才能获得修复。

希望这会有所帮助。