使用 SMS Retriever API 自動進行簡訊驗證
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
使用 SMS Retriever API 即可在 Android 應用程式中自動執行簡訊使用者驗證,不必請使用者手動輸入驗證碼,也不需要任何額外的應用程式權限。在應用程式中實作自動簡訊驗證時,驗證流程應如下所示:

- 使用者在應用程式中啟動簡訊驗證。如果應用程式在建立使用者帳戶時不需要這些資訊,可能會提示使用者提供電話號碼,或是使用密碼專用 Smart Lock 提示選取器。
- 應用程式會向伺服器發出要求,以驗證使用者的電話號碼。視使用者資料庫中提供的資訊而定,這項要求可能包含使用者的 ID 和/或電話號碼。
- 同時,應用程式會呼叫 SMS Retriever API,開始監聽伺服器傳來的簡訊回應。
- 您的伺服器會傳送一則簡訊給使用者,其中包含要傳送到伺服器的一次性代碼,以及可識別應用程式的雜湊。
- 當使用者的裝置收到簡訊時,Google Play 服務會使用應用程式雜湊來判斷訊息適用於您的應用程式,並透過 SMS Retriever API 讓應用程式使用訊息文字。
- 應用程式會剖析訊息文字中的一次性代碼,並傳回您的伺服器。
- 您的伺服器會從應用程式收到一次性代碼並驗證代碼,最後再確認使用者已成功驗證帳戶。
如要在應用程式中實作自動簡訊驗證功能,請參閱 Android 和伺服器指南:
Android 指南
伺服器指南
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-07-25 (世界標準時間)。
[null,null,["上次更新時間:2025-07-25 (世界標準時間)。"],[[["\u003cp\u003eThe SMS Retriever API enables automatic SMS verification in Android apps, eliminating manual code entry for users.\u003c/p\u003e\n"],["\u003cp\u003eThis API streamlines user verification by having your app listen for an SMS message containing a one-time code, sent by your server after a verification request.\u003c/p\u003e\n"],["\u003cp\u003eGoogle Play services identifies the incoming SMS message as intended for your app using an app hash included in the message, ensuring secure delivery of the verification code.\u003c/p\u003e\n"],["\u003cp\u003eThe SMS Retriever API enhances user experience and security by automating verification without requiring extra app permissions.\u003c/p\u003e\n"]]],[],null,["# Automatic SMS Verification with the SMS Retriever API\n\nWith the SMS Retriever API, you can perform SMS-based user verification in your\nAndroid app automatically, without requiring the user to manually type\nverification codes, and without requiring any extra app permissions. When you\nimplement automatic SMS verification in your app, the verification flow looks\nlike this:\n\n[](/static/identity/sms-retriever/flow-overview.png)\n\n1. A user initiates SMS verification in your app. Your app might prompt the user to provide a phone number or use the [Smart Lock for Passwords hint selector](/identity/smartlock-passwords/android/retrieve-hints) if that information wasn't required to create the user's account.\n2. Your app makes a request to your server to verify the user's phone number. Depending on what information is available in your user database, this request might include the user's ID, the user's phone number, or both.\n3. At the same time, your app calls the SMS Retriever API to begin listening for an SMS response from your server.\n4. Your server sends an SMS message to the user that includes a one-time code to be sent back to your server, and a hash that identifies your app.\n5. When the user's device receives the SMS message, Google Play services uses the app hash to determine that the message is intended for your app, and makes the message text available to your app through the SMS Retriever API.\n6. Your app parses out the one-time code from the message text and sends it back to your server.\n7. Your server receives the one-time code from your app, verifies the code, and finally records that the user has successfully verified their account.\n\nTo implement automatic SMS verification in your app, see the Android and server\nguides:\n\n[Android Guide](/identity/sms-retriever/request)\n[Server Guide](/identity/sms-retriever/verify)"]]