【发布时间】:2019-12-04 23:45:56
【问题描述】:
我试图在我的颤振项目中启用 Firebase 电话身份验证,以便它实际向输入的号码发送带有身份验证码的 SMS。
-将 APNs 身份验证密钥上传到“云消息传递”下的 Firebase 项目设置中
-我还补充了:
io.flutter.embedded_views_preview
<string>NO</string>
到 info.plist 文件
然后我在 Xcode 项目中激活了 Background mode -> Remote Notifications 和 Push Notifications,并在 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.
【问题讨论】:
-
会发生什么?有哪些错误?另外,你可以发送一个使用 vanilla ios 或没有 Flutter 的 Android 代码吗? SMS 发送有很多移动部件,并将问题隔离到特定的库或工具,然后发现有意义的错误将是成功的第一步。
标签: swift firebase flutter firebase-authentication apple-push-notifications