【发布时间】:2020-01-24 20:00:31
【问题描述】:
我已经正确实现了 Firebase 电子邮件和 Google 登录,它在 iOS 和 Android 上都可以正常工作。 然后我尝试实现 Over the Phone Auth,首先它在 iPhone 上不起作用,但后来我做了一些研究,似乎我必须上传一些 APN,最后我正确设置它,现在它在 iPhone 上工作,但是当我尝试向 Android 发送 SMS 时,它没有收到它,而是向我发送了一条消息,表明代码已像在 iPhone 上一样发送。
最奇怪的是,如果我输入 iPhone 的手机号码,它会收到 SMS 代码。所以我想知道我是否必须像在 iPhone 上那样在 Android 上启用某种类型的推送通知,或者我是否必须设置一些真正的开发者帐户,因为现在我没有谷歌的帐户,或者我还有其他东西我不见了。
谁能帮帮我,好吗?
我使用的 auth 方法是 Flutter 示例之一:
class _PhoneSignInSection extends StatefulWidget {
_PhoneSignInSection(this._scaffold);
final ScaffoldState _scaffold;
@override
State<StatefulWidget> createState() => _PhoneSignInSectionState();
}
class _PhoneSignInSectionState extends State<_PhoneSignInSection> {
final TextEditingController _phoneNumberController = TextEditingController();
final TextEditingController _smsController = TextEditingController();
String _message = '';
String _verificationId;
@override
Widget build(BuildContext context) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Container(
child: const Text('Test sign in with phone number'),
padding: const EdgeInsets.all(16),
alignment: Alignment.center,
),
TextFormField(
controller: _phoneNumberController,
decoration:
InputDecoration(labelText: 'Phone number (+x xxx-xxx-xxxx)'),
validator: (String value) {
if (value.isEmpty) {
return 'Phone number (+x xxx-xxx-xxxx)';
}
return null;
},
),
Container(
padding: const EdgeInsets.symmetric(vertical: 16.0),
alignment: Alignment.center,
child: RaisedButton(
onPressed: () async {
_verifyPhoneNumber();
},
child: const Text('Verify phone number'),
),
),
TextField(
controller: _smsController,
decoration: InputDecoration(labelText: 'Verification code'),
),
Container(
padding: const EdgeInsets.symmetric(vertical: 16.0),
alignment: Alignment.center,
child: RaisedButton(
onPressed: () async {
_signInWithPhoneNumber();
},
child: const Text('Sign in with phone number'),
),
),
Container(
alignment: Alignment.center,
padding: const EdgeInsets.symmetric(horizontal: 16),
child: Text(
_message,
style: TextStyle(color: Colors.red),
),
)
],
);
}
// Example code of how to verify phone number
void _verifyPhoneNumber() async {
setState(() {
_message = '';
});
final PhoneVerificationCompleted verificationCompleted =
(AuthCredential phoneAuthCredential) {
_auth.signInWithCredential(phoneAuthCredential);
setState(() {
_message = 'Received phone auth credential: $phoneAuthCredential';
});
};
final PhoneVerificationFailed verificationFailed =
(AuthException authException) {
setState(() {
_message =
'Phone number verification failed. Code: ${authException.code}. Message: ${authException.message}';
});
};
final PhoneCodeSent codeSent =
(String verificationId, [int forceResendingToken]) async {
widget._scaffold.showSnackBar(SnackBar(
content:
const Text('Please check your phone for the verification code.'),
));
_verificationId = verificationId;
};
final PhoneCodeAutoRetrievalTimeout codeAutoRetrievalTimeout =
(String verificationId) {
_verificationId = verificationId;
};
await _auth.verifyPhoneNumber(
phoneNumber: _phoneNumberController.text,
timeout: const Duration(seconds: 5),
verificationCompleted: verificationCompleted,
verificationFailed: verificationFailed,
codeSent: codeSent,
codeAutoRetrievalTimeout: codeAutoRetrievalTimeout);
}
// Example code of how to sign in with phone.
void _signInWithPhoneNumber() async {
final AuthCredential credential = PhoneAuthProvider.getCredential(
verificationId: _verificationId,
smsCode: _smsController.text,
);
final FirebaseUser user =
(await _auth.signInWithCredential(credential));
final FirebaseUser currentUser = await _auth.currentUser();
assert(user.uid == currentUser.uid);
setState(() {
if (user != null) {
_message = 'Successfully signed in, uid: ' + user.uid;
} else {
_message = 'Sign in failed';
}
});
}
}
而我的 App 级 build.gradle 有这个依赖:
dependencies {
implementation 'com.google.firebase:firebase-auth:19.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
如导游所说:
将 Firebase 身份验证 Android 库的依赖项添加到 你的模块(应用级)Gradle 文件(通常是 app/build.gradle): 实施 'com.google.firebase:firebase-auth:19.0.0'
当然我在同一个 build.gradle 中有 apply plugin: 'com.google.gms.google-services'
编辑:当它使用列入白名单的电话号码进行测试时,我在 Android 上成功登录。
【问题讨论】:
-
在 Firebase 电话身份验证中,您可以设置一些测试号码,您是否尝试过使用这些号码。他们不会为这些号码发送短信,您需要输入预定义的验证码。只是为了缩小问题范围
-
嗨,Chenna 是的,当我使用列入白名单的号码进行测试时,它工作正常。我将编辑我的问题以包括这个问题。谷歌点!
-
要了解这个问题,您有两个不同的电话号码,一个在 iPhone 上,另一个在 Android 设备上。提供 iPhone 电话号码时没有任何问题(您是否在 Android 和 iPhone 应用中都尝试过),在 Android 应用上提供 iOS 电话号码时是否收到短信?
-
你能不能把
_auth.signInWithCredential(phoneAuthCredential); setState(() { _message = 'Received phone auth credential: $phoneAuthCredential'; })也改成_auth.signInWithCredential(phoneAuthCredential).then((u) {setState(){...}}); -
当我将它从 Android 应用程序发送到 iPhone 号码时,我会收到代码。当它从 iPhone 发送到 iPhone 时,它也会收到它。当我将它从 iPhone 应用程序发送到 Android 号码时,它没有收到它,当我将它从 Android 应用程序发送到 Android 号码时,它也没有收到它。
标签: firebase flutter firebase-authentication