本頁說明如何使用 SMS User Consent API 要求使用者同意讀取單一簡訊驗證訊息。如果使用者同意,API 會傳回訊息文字,您可以取得驗證碼並完成驗證程序。
安裝依附元件
在應用程式的 build.gradle
檔案中加入 Play 服務驗證元件:
implementation 'com.google.android.gms:play-services-auth:17.0.0'
implementation 'com.google.android.gms:play-services-auth-api-phone:17.4.0'
1. 取得使用者的電話號碼
如果您沒有使用者的電話號碼,請先要求號碼再啟動簡訊驗證流程。
您可透過適用於應用程式的方式取得使用者的電話號碼。如果建立使用者帳戶時不需要提供電話號碼,請考慮使用密碼專用 Smart Lock 提示選取器協助使用者填寫電話號碼。如何使用提示選取器:
Kotlin
private val CREDENTIAL_PICKER_REQUEST = 1 // Set to an unused request code
// Construct a request for phone numbers and show the picker
private fun requestHint() {
val hintRequest = HintRequest.Builder()
.setPhoneNumberIdentifierSupported(true)
.build()
val credentialsClient = Credentials.getClient(this)
val intent = credentialsClient.getHintPickerIntent(hintRequest)
startIntentSenderForResult(
intent.intentSender,
CREDENTIAL_PICKER_REQUEST,
null, 0, 0, 0
)
}
public override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
when (requestCode) {
CREDENTIAL_PICKER_REQUEST ->
// Obtain the phone number from the result
if (resultCode == Activity.RESULT_OK && data != null) {
val credential = data.getParcelableExtra<Credential>(Credential.EXTRA_KEY)
// credential.getId(); <-- will need to process phone number string
}
// ...
}
}
Java
private static final int CREDENTIAL_PICKER_REQUEST = 1; // Set to an unused request code
// Construct a request for phone numbers and show the picker
private void requestHint() throws IntentSender.SendIntentException {
HintRequest hintRequest = new HintRequest.Builder()
.setPhoneNumberIdentifierSupported(true)
.build();
PendingIntent intent = Credentials.getClient(this).getHintPickerIntent(hintRequest);
startIntentSenderForResult(intent.getIntentSender(),
RESOLVE_HINT, null, 0, 0, 0);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case CREDENTIAL_PICKER_REQUEST:
// Obtain the phone number from the result
if (resultCode == RESULT_OK) {
Credential credential = data.getParcelableExtra(Credential.EXTRA_KEY);
// credential.getId(); <-- will need to process phone number string
}
break;
// ...
}
}
2. 開始聽取收到的訊息
接著,呼叫 SMS User Consent API 的 startSmsUserConsent()
方法即可開始監聽收到的訊息。如果您知道簡訊的來源電話號碼,請指明該號碼 (否則請傳遞 null
)。這樣一來,SMS User Consent API 就只會在來自這個號碼的訊息上觸發。
如要開始聆聽內容:
Kotlin
// Start listening for SMS User Consent broadcasts from senderPhoneNumber
// The Task<Void> will be successful if SmsRetriever was able to start
// SMS User Consent, and will error if there was an error starting.
val task = SmsRetriever.getClient(context).startSmsUserConsent(senderPhoneNumber /* or null */)
Java
// Start listening for SMS User Consent broadcasts from senderPhoneNumber
// The Task<Void> will be successful if SmsRetriever was able to start
// SMS User Consent, and will error if there was an error starting.
Task<Void> task = SmsRetriever.getClient(context).startSmsUserConsent(senderPhoneNumber /* or null */);
監聽收到的簡訊時,您可以設定驗證系統,將驗證碼傳送至使用者在第一個步驟中取得的電話號碼。
接下來五分鐘內,當裝置收到包含一次性代碼的簡訊時,Play 服務將廣播至應用程式,以提示使用者授予讀取訊息的權限。只有在符合下列條件時,訊息才會觸發廣播:
- 訊息包含 4 至 10 個英數字元,且至少包含一個數字。
- 如果您指定傳送者的電話號碼,訊息會透過該號碼傳送。
使用具有 SEND_PERMISSION
權限並回應 SMS_RETRIEVED_ACTION
意圖的廣播接收器處理這些廣播訊息。如何建立及註冊廣播接收器:
Kotlin
private val SMS_CONSENT_REQUEST = 2 // Set to an unused request code
private val smsVerificationReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
if (SmsRetriever.SMS_RETRIEVED_ACTION == intent.action) {
val extras = intent.extras
val smsRetrieverStatus = extras?.get(SmsRetriever.EXTRA_STATUS) as Status
when (smsRetrieverStatus.statusCode) {
CommonStatusCodes.SUCCESS -> {
// Get consent intent
val consentIntent = extras.getParcelable<Intent>(SmsRetriever.EXTRA_CONSENT_INTENT)
try {
// Start activity to show consent dialog to user, activity must be started in
// 5 minutes, otherwise you'll receive another TIMEOUT intent
startActivityForResult(consentIntent, SMS_CONSENT_REQUEST)
} catch (e: ActivityNotFoundException) {
// Handle the exception ...
}
}
CommonStatusCodes.TIMEOUT -> {
// Time out occurred, handle the error.
}
}
}
}
}
override fun onCreate(savedInstanceState: Bundle?) {
// ...
val intentFilter = IntentFilter(SmsRetriever.SMS_RETRIEVED_ACTION)
registerReceiver(smsVerificationReceiver, SmsRetriever.SEND_PERMISSION, intentFilter)
}
Java
private static final int SMS_CONSENT_REQUEST = 2; // Set to an unused request code
private final BroadcastReceiver smsVerificationReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (SmsRetriever.SMS_RETRIEVED_ACTION.equals(intent.getAction())) {
Bundle extras = intent.getExtras();
Status smsRetrieverStatus = (Status) extras.get(SmsRetriever.EXTRA_STATUS);
switch (smsRetrieverStatus.getStatusCode()) {
case CommonStatusCodes.SUCCESS:
// Get consent intent
Intent consentIntent = extras.getParcelable(SmsRetriever.EXTRA_CONSENT_INTENT);
try {
// Start activity to show consent dialog to user, activity must be started in
// 5 minutes, otherwise you'll receive another TIMEOUT intent
startActivityForResult(consentIntent, SMS_CONSENT_REQUEST);
} catch (ActivityNotFoundException e) {
// Handle the exception ...
}
break;
case CommonStatusCodes.TIMEOUT:
// Time out occurred, handle the error.
break;
}
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// ...
IntentFilter intentFilter = new IntentFilter(SmsRetriever.SMS_RETRIEVED_ACTION);
registerReceiver(smsVerificationReceiver, SmsRetriever.SEND_PERMISSION, intentFilter);
}
您可以為 EXTRA_CONSENT_INTENT
啟動活動,提示使用者授予一次性權限,以便讀取訊息內容。
3. 透過訊息取得驗證碼
在 onActivityResult()
方法中,處理使用者對權限要求的回應。如果您取得 RESULT_OK
的結果代碼,表示使用者已獲得讀取訊息內容的權限,您可以從意圖中取得訊息文字。
Kotlin
public override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
when (requestCode) {
// ...
SMS_CONSENT_REQUEST ->
// Obtain the phone number from the result
if (resultCode == Activity.RESULT_OK && data != null) {
// Get SMS message content
val message = data.getStringExtra(SmsRetriever.EXTRA_SMS_MESSAGE)
// Extract one-time code from the message and complete verification
// `message` contains the entire text of the SMS message, so you will need
// to parse the string.
val oneTimeCode = parseOneTimeCode(message) // define this function
// send one time code to the server
} else {
// Consent denied. User can type OTC manually.
}
}
}
Java
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
// ...
case SMS_CONSENT_REQUEST:
if (resultCode == RESULT_OK) {
// Get SMS message content
String message = data.getStringExtra(SmsRetriever.EXTRA_SMS_MESSAGE);
// Extract one-time code from the message and complete verification
// `sms` contains the entire text of the SMS message, so you will need
// to parse the string.
String oneTimeCode = parseOneTimeCode(message); // define this function
// send one time code to the server
} else {
// Consent canceled, handle the error ...
}
break;
}
}
收到訊息文字後,您可以剖析驗證碼並自動填入,或是以其他方式完成驗證流程。