Home > front end >  Unresolved reference: getIntentSender / While trying to obtain phone number in Jetpack Compose
Unresolved reference: getIntentSender / While trying to obtain phone number in Jetpack Compose

Time:05-26

I am trying to obtain phone number(s) in Jetpack compose following Googles Phone Number Hint Docs. But I am stuck in a problem where it says: getIntentSender() is unresolved in request: GetPhoneNumberHintIntentRequest.

I am also getting another error on addOnFailureListener

Type mismatch.
Required:
 OnFailureListener
Found:
 Int
@Composable
fun PhoneNumberConsent() {
    val context = LocalContext.current

    val request = GetPhoneNumberHintIntentRequest.builder().build()

    val phoneNumberHintIntentResultLauncher = rememberLauncherForActivityResult(
        contract = ActivityResultContracts.StartActivityForResult(),
    ) {
        try {
            val phoneNumber =
                Identity.getSignInClient(context)
                    .getPhoneNumberFromIntent(it.data)
        } catch (e: Exception) {
            Log.e(TAG, "Phone Number Hint failed")
        }

    }

    Identity.getSignInClient(context)
        .getPhoneNumberHintIntent(request)
        .addOnSuccessListener(
            try {
                phoneNumberHintIntentResultLauncher.launch(request.getIntentSender())
            } catch (e: Exception) {
                Log.e(TAG, "Launching the PendingIntent failed")
            } as OnSuccessListener<in PendingIntent>
        )
        .addOnFailureListener(
            Log.e(TAG, "Phone Number Hint failed")
        )
}

CodePudding user response:

Google documentation is really bad at this point. addOnSuccessListener accepts a listener, which can be passed as trailing closure.

Result passed to this listener is a pending intent which has intentSender property, and it can be used to create IntentSenderRequest.

Here's a working example:

val context = LocalContext.current

val request = GetPhoneNumberHintIntentRequest.builder().build()

val phoneNumberHintIntentResultLauncher = rememberLauncherForActivityResult(
    contract = ActivityResultContracts.StartIntentSenderForResult(),
) {
    try {
        val phoneNumber = Identity.getSignInClient(context)
                .getPhoneNumberFromIntent(it.data)
        println("phoneNumber $phoneNumber")
    } catch (e: Exception) {
        println("Phone Number Hint failed")
        e.printStackTrace()
    }

}

Button(onClick = {
    Identity.getSignInClient(context)
        .getPhoneNumberHintIntent(request)
        .addOnSuccessListener { pendingIntent ->
            try {
                phoneNumberHintIntentResultLauncher.launch(
                    IntentSenderRequest.Builder(
                        pendingIntent.intentSender
                    ).build()
                )
            } catch (e: Exception) {
                println("Launching the PendingIntent failed")
                e.printStackTrace()
            }
        }
        .addOnFailureListener {
            println("addOnFailureListener $it")
        }
}) {

}

If you need to run it immediately after the view appears, use LaunchedEffect instead of Button.onClick. Your current approach contradicts one of the basic rules of Compose, which is that composable functions must be free of side-effects. Read more in thinking in compose

CodePudding user response:

According to this docs there is no getIntentSender() method in GetPhoneNumberHintIntentRequest class. Maybe there is a typo in the tutorial you are following, try to use result instead of request:

Identity.getSignInClient(context)
    .getPhoneNumberHintIntent(request)
    .addOnSuccessListener { result ->
        try {
            phoneNumberHintIntentResultLauncher.launch(result.intentSender.sendIntent)
        } catch (e: Exception) {
            Log.e(TAG, "Launching the PendingIntent failed")
        } as OnSuccessListener<in PendingIntent>
    }
    .addOnFailureListener(
        Log.e(TAG, "Phone Number Hint failed")
    )
  • Related