SmsUserConsentEffect

fun SmsUserConsentEffect(codeLength: Int, onCodeReceive: (code: String) -> Unit, onError: (error: SmsConsentError) -> Unit, senderPhoneNumber: String? = null, restartKey: Any? = Unit)(source)

Reads a one-time code from the next incoming SMS using Google's SMS User Consent API.

When a message arrives, Play services shows a sheet asking the user to share it. If they agree, the first run of codeLength digits is passed to onCodeReceive. No SMS permission is needed.

The flow is one shot: after a code, an error or a five minute timeout it stops listening. Change restartKey to start again, for example after the user asks for a new code.

var attempt by remember { mutableIntStateOf(0) }

SmsUserConsentEffect(
codeLength = 6,
onCodeReceive = { code -> otpState.fillOtp(code, 6) },
onError = { error -> Log.w("Otp", "SMS consent ended: $error") },
restartKey = attempt,
)

Parameters

codeLength

Number of digits in the code. Must be positive.

onCodeReceive

Called with the extracted code.

onError

Called when the flow ends without a code. See SmsConsentError.

senderPhoneNumber

Only react to messages from this number. Null accepts any sender.

restartKey

Any value. A new value starts a fresh listening session.