मैसेज (एसएमएस) से पुष्टि करने वाला कोड पढ़ने के लिए, एक बार की सहमति का अनुरोध करें

इस पेज पर, एसएमएस के ज़रिए उपयोगकर्ता की सहमति से जुड़ा एपीआई इस्तेमाल करके, एसएमएस से पुष्टि करने का मैसेज पढ़ने का तरीका बताया गया है. अगर उपयोगकर्ता की सहमति मिल जाती है, तो एपीआई मैसेज का टेक्स्ट दिखाता है. इससे आपको पुष्टि करने का कोड मिल सकता है और पुष्टि की प्रक्रिया पूरी की जा सकती है.

डिपेंडेंसी इंस्टॉल करना

अपने ऐप्लिकेशन की build.gradle फ़ाइल में, Play services के पुष्टि करने वाले कॉम्पोनेंट को शामिल करें:

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. आने वाले मैसेज सुनना शुरू करें

इसके बाद, आने वाले मैसेज को सुनना शुरू करने के लिए, एसएमएस User Consent API के startSmsUserConsent() तरीके को कॉल करें. अगर आपको पता है कि मैसेज (एसएमएस) किस फ़ोन नंबर से आया है, तो इसकी जानकारी दें (नहीं तो, null को पास करें). इस तरह, एसएमएस उपयोगकर्ता की सहमति वाला एपीआई सिर्फ़ इस नंबर से मिलने वाले मैसेज पर ट्रिगर होगा.

सुनने के लिए:

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 services आपके ऐप्लिकेशन पर ब्रॉडकास्ट करेगी. इसका मकसद, उपयोगकर्ता से मैसेज पढ़ने की अनुमति मांगना होगा. कोई मैसेज ब्रॉडकास्ट तब ही ट्रिगर होता है, जब वह इन शर्तों को पूरा करता हो:

  • मैसेज में 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;
    }
}

मैसेज टेक्स्ट मिलने के बाद, पुष्टि करने के लिए भेजा गया कोड पार्स किया जा सकता है. साथ ही, फ़ॉर्म अपने-आप भरा जा सकता है या पुष्टि की प्रक्रिया पूरी की जा सकती है.