একটি এসএমএস যাচাইকরণ কোড পড়ার জন্য এককালীন সম্মতির অনুরোধ করুন

এই পৃষ্ঠাটি বর্ণনা করে যে কীভাবে এসএমএস ব্যবহারকারীর সম্মতি API ব্যবহার করতে হয় একটি একক SMS যাচাইকরণ বার্তা পড়ার জন্য ব্যবহারকারীর সম্মতির অনুরোধ করতে। ব্যবহারকারী সম্মতি দিলে, API বার্তাটির পাঠ্য ফেরত দেয়, যেখান থেকে আপনি যাচাইকরণ কোড পেতে পারেন এবং যাচাইকরণ প্রক্রিয়াটি সম্পূর্ণ করতে পারেন।

নির্ভরতা ইনস্টল করুন

আপনার অ্যাপের build.gradle ফাইলে Play Services auth কম্পোনেন্ট অন্তর্ভুক্ত করুন:

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. ব্যবহারকারীর ফোন নম্বর পান

আপনার কাছে ব্যবহারকারীর ফোন নম্বর না থাকলে, আপনি একটি SMS যাচাইকরণ প্রবাহ শুরু করার আগে এটির অনুরোধ করুন৷

আপনি এমনভাবে ব্যবহারকারীর ফোন নম্বর পেতে পারেন যা আপনার অ্যাপের জন্য উপযুক্ত। ব্যবহারকারীর অ্যাকাউন্ট তৈরি করার জন্য সেই তথ্যের প্রয়োজন না হলে ব্যবহারকারীকে তাদের ফোন নম্বর পূরণ করতে সহায়তা করার জন্য পাসওয়ার্ডের জন্য স্মার্ট লক ইঙ্গিত নির্বাচনকারী ব্যবহার করার কথা বিবেচনা করুন। ইঙ্গিত নির্বাচক ব্যবহার করতে:

কোটলিন

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
            }
        // ...
    }
}

জাভা

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() পদ্ধতিতে কল করুন। আপনি যদি ফোন নম্বর জানেন যেখান থেকে SMS বার্তাটি আসবে, তা নির্দিষ্ট করুন (অন্যথায়, null পাস করুন)। এইভাবে, SMS ব্যবহারকারীর সম্মতি API শুধুমাত্র এই নম্বর থেকে আসা বার্তাগুলিতে ট্রিগার করবে৷

শোনা শুরু করতে:

কোটলিন

// 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 */)

জাভা

// 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 ইন্টেন্টে সাড়া দেয় এমন একটি ব্রডকাস্ট রিসিভার দিয়ে এই সম্প্রচারগুলি পরিচালনা করুন৷ ব্রডকাস্ট রিসিভার তৈরি এবং নিবন্ধন করতে:

কোটলিন

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)
}

জাভা

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 এর ফলাফল কোড পান, ব্যবহারকারী বার্তাটির বিষয়বস্তু পড়ার অনুমতি দিয়েছেন এবং আপনি উদ্দেশ্য থেকে বার্তা পাঠ্য পেতে পারেন।

কোটলিন

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.
            }
    }
}

জাভা

@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;
    }
}

একবার আপনার কাছে বার্তাটি পাঠ্য হয়ে গেলে, আপনি যাচাইকরণ কোডটি পার্স করতে পারেন এবং ফর্মটি স্বয়ংক্রিয়ভাবে পূরণ করতে পারেন বা অন্যথায় যাচাইকরণ প্রবাহটি সম্পূর্ণ করতে পারেন৷