Firebase Phone Auth not sending me a text message with code

Viewed 23

Trying to implement flutter firebase phone authentication for the first time, I am not getting a text message with a code.

Trying on iOS right now, Android later...

  • Phone is enabled as a sign-in method in firebase console
  • I've enabled push notifications in xcode, and uploaded my APN auth key to firebase console. Also enabled the background modes: background fetch, remote notifications.

I am wondering if the problem is with Firebase cloud messaging. I added it to my pubspec, but am confused on what to do with it.

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(),
    );
  }
}

my phone verify function...

  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);
      },
    );
  }
1 Answers
Related