javaandroidkotlinauthentication

Android app Sim card based authentication


I have created android app, for authentication purpose I want to send SMS to self number based on SIM card inserted without entering the phone number. Please suggest me solution for that.

I have tried using SubscriptionManager and TelephonyManager to fetch the phonenumber in background but it wont work in some devices.


Solution

  • This will get you the phone number:

    https://developers.google.com/identity/phone-number-hint/android

    GetPhoneNumberHintIntentRequest request = GetPhoneNumberHintIntentRequest.builder().build();
        Identity.getSignInClient(VerificationActivity.this)
            .getPhoneNumberHintIntent(request)
            .addOnSuccessListener(new OnSuccessListener<PendingIntent>() {
                @Override
                public void onSuccess(PendingIntent pendingIntent) {
                    try {
                        IntentSenderRequest intentSenderRequest = new IntentSenderRequest.Builder(pendingIntent).build();
                        phoneNumberHintLauncher.launch(intentSenderRequest);
                    } catch(Exception e) {
                        Log.e(TAG, "Launching the PendingIntent failed", e);
                        throw new RuntimeException(e);
                    }
                }
            })
            .addOnFailureListener(e -> {
                Log.e(TAG, "Phone Number Hint failed", e);
            });
    

    Here's the launcher:

    private final ActivityResultLauncher<IntentSenderRequest> phoneNumberHintLauncher = registerForActivityResult(
            new ActivityResultContracts.StartIntentSenderForResult(),
            result -> {
                try {
                    String phoneNumber = Identity.getSignInClient(VerificationActivity.this).getPhoneNumberFromIntent(result.getData());
                    
                    /// do stuff
                    
                    
                } catch (ApiException e) {
                    Log.e(TAG, e);
                }
            });
    

    To send an SMS, you need to implement a host of things including a 3rd party paid subscription.

    https://developers.google.com/identity/sms-retriever/request

    Good luck!