How to realize the application to automatically fill in the SMS verification code?

Problem sharing

On the Huawei Developer Forum, some developers asked us: I want to realize the application to automatically fill in the SMS verification code, but if I turn on the SMS "verification code protection" function, the app cannot automatically fill in the verification code. Is there any way to get the SMS verification code? Question link: https://developer.huawei.com/consumer/cn/forum/topicview?tid=0202342491084740374&fid=18

We know that Huawei attaches great importance to privacy protection, and the privacy and security of users has always been the primary concern of Huawei. In order to protect users' payment security and account security, Huawei EMUI system provides the protection function of verification code SMS, which can prevent third-party applications from obtaining the verification code SMS received by users. As the developer asked, the app cannot automatically fill in the verification code after the SMS verification code protection is turned on. Does Huawei provide any method to automatically obtain the verification code and fill in the verification code when the SMS verification code protection is turned on?

Solution

HUAWEI Account Kit provides developers with an automatic SMS reading capability, which can help applications automatically read the verification code, realize the application to automatically or be authorized to read the SMS verification code, improve the verification efficiency and optimize the user experience.

The following is a scenario of SMS automatic reading ability: if the application requires the user to enter the mobile phone number and verify the user's identity through the SMS verification code, the application can have the ability to automatically read the SMS verification code for the application without applying for the SMS reading permission by integrating the ReadSmsManager service. In the verification process, users can omit the operation of manually entering the SMS verification code. The whole process of obtaining the SMS verification code can be simplified as follows: users click to obtain the verification code → receive the SMS → auto fill → users click, which will further optimize the user experience.

The following is a scenario of SMS automatic reading capability:

If the application requires the user to enter the mobile phone number and verify the user's identity through the SMS verification code, it can be integrated ReadSmsManager Service, so that the application can automatically read the SMS verification code for the application without applying for SMS reading permission. In the verification process, users can omit the operation of manually entering the SMS verification code. The whole process of obtaining the SMS verification code can be simplified as follows: users click to obtain the verification code → receive the SMS → auto fill → users click, which will further optimize the user experience.

Automatic reading SMS verification code function key development and operation guidance

1. Application call ReadSmsManager.start(Activity activity) Method requests to start the SMS reading service.

Task<Void> task = ReadSmsManager.start(MainActivity.this);
task.addOnCompleteListener(new OnCompleteListener<Void>() {
    @Override
    public void onComplete(Task<Void> task) {
        if (task.isSuccessful()) {
            // After the service is started successfully, you can continue the follow-up process
            doSomethingWhenTaskSuccess();
        }
    }
});

2. The application client sends the mobile phone number to the application server, which generates an authentication message and sends it to the user's mobile phone number through SMS. This part is completed by the developer.

3. When the user's device receives the authentication message, the HMS Core (APK) will explicitly send a broadcast to the application client, and the intent contains the text of the message. The application client can receive this authentication message using the broadcast. After the application client reads the text of the verification message, it can use regular expression or other logic to obtain the verification code from the message. The format of the verification code is defined by the application client and the server.

public class MySMSBroadcastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();
        if (bundle != null && ReadSmsConstant.READ_SMS_BROADCAST_ACTION.equals(intent.getAction())) {
            Status status = bundle.getParcelable(ReadSmsConstant.EXTRA_STATUS);
            if (status.getStatusCode() == CommonStatusCodes.TIMEOUT) {
                // The service has timed out, and the qualified SMS is not read. The service is closed
                doSomethingWhenTimeOut();
            } else if (status.getStatusCode() == CommonStatusCodes.SUCCESS) {
                if (bundle.containsKey(ReadSmsConstant.EXTRA_SMS_MESSAGE)) {
                    // The service reads qualified SMS and the service is closed
                    doSomethingWhenGetMessage(bundle.getString(ReadSmsConstant.EXTRA_SMS_MESSAGE));
                }
            }
        }
    }
}

In addition to the above capability of automatically reading SMS verification code, Huawei account service also provides the capability of authorizing to read SMS verification code, so that applications can automatically obtain SMS verification code for applications through user authorization without applying for SMS reading permission. In the verification process, users can save the operation of manually entering SMS verification code and optimize the user experience.

>>Visit Huawei account service official website , learn more
>>Get Automatically read SMS verification code development guidance document, Authorized to read SMS verification code development guidance document
>>Huawei account service open source warehouse address: GitHub,Gitee

Click the attention on the right side of the avatar in the upper right corner to learn the latest technology of Huawei mobile services for the first time~

Keywords: Android github gitee

Added by TheSeeker on Sat, 05 Mar 2022 03:27:07 +0200