Firebase Phone Auth没有向我发送带有代码的短信



第一次尝试实现flutter firebase电话身份验证时,我没有收到带代码的短信。

现在尝试iOS,稍后尝试Android。。。

  • 电话在firebase控制台中被启用为登录方法
  • 我已经在xcode中启用了推送通知,并将我的APN验证密钥上传到firebase控制台。还启用了后台模式:后台提取、远程通知

我想知道问题是否与Firebase云消息有关。我把它添加到了我的pubspec中,但我对如何处理它感到困惑

main。dart…

import 'package:flutter/material.dart';
import 'package:firebase_core/firebase_core.dart';
import 'firebase_options.dart';
import 'view/Onboarding.dart';
Future<void> main() async {
runApp(const MyApp());
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Pearmonie',
theme: ThemeData(
// This is the theme of your application.
//
// Try running your application with "flutter run". You'll see the
// application has a blue toolbar. Then, without quitting the app, try
// changing the primarySwatch below to Colors.green and then invoke
// "hot reload" (press "r" in the console where you ran "flutter run",
// or simply save your changes to "hot reload" in a Flutter IDE).
// Notice that the counter didn't reset back to zero; the application
// is not restarted.
primaryColor: Color(0xFF4328f7),
inputDecorationTheme: InputDecorationTheme(
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.black)
),
labelStyle: TextStyle(color: Colors.black45),
iconColor: Colors.grey,
)
),
home: const Onboarding(),
);
}
}

我的手机验证功能。。。

void EmailSignUp() async {
if (emailController.text == null || emailController.text == "" || phoneController.text == null || phoneController.text == "") {
print("Email and phone required!");
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text("Sorry! You'll need to type in an e-mail and password!", style: TextStyle(fontWeight: FontWeight.bold)), backgroundColor: color_bluepurple));
return;
}
FirebaseAuth auth = FirebaseAuth.instance;
await auth.verifyPhoneNumber(
phoneNumber: phoneController.text,
codeSent: (String verificationId, int? resendToken) async {
// Update the UI - wait for the user to enter the SMS code
setState(() {
otp = true;
});
},
timeout: const Duration(seconds: 120),
codeAutoRetrievalTimeout: (String verificationId) {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text("Sorry! Your SMS code has timed out!", style: TextStyle(fontWeight: FontWeight.bold)), backgroundColor: color_bluepurple));
},
verificationFailed: (FirebaseAuthException e) {
if (e.code == 'invalid-phone-number') {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text("Sorry! Your phone verification has failed!", style: TextStyle(fontWeight: FontWeight.bold)), backgroundColor: color_bluepurple));
}
// Handle other errors
},
verificationCompleted: (PhoneAuthCredential credential) async {
// ANDROID ONLY!
// Sign the user in (or link) with the auto-generated credential
await auth.signInWithCredential(credential);
},
);
}

解决方案是:为了让云消息工作,我必须在xcode中将GoogleService-Info.plist添加到Runner目标中。

然后跟随这篇文章:如何启用手机授权firebase,flutter,xcode

他们引用本页:https://firebase.google.com/docs/auth/ios/phone-auth

您需要从GoogleService-Info.plist复制REVERSED_CLIENT_ID,并将其添加到";信息";选项卡,然后将值粘贴到URL Schemes中。。就是这样!