Aliyun SMS Service Integration

Whether app or web will use SMS verification function, Ali provides high-quality SMS services.
This will integrate step records for future reference.

Access key and Access key Secret

1. Register the developer of Ali Open Platform and authenticate it by real name.
2. Open Ali Short Message Service
3. Click on the Access Key button in the SMS console to view Access key and Access key Secret


4. To create short message signatures, individual users can only create one short message signature, while enterprise users can create five short message signatures.
5, SMS signature is the main name of SMS, for example, Alipay's short message is Alipay, and Alipay is short message signature. Short message signature needs to be audited after submission. It can be audited and approved within 2 hours of normal working time.

6. Create a short message template: The short message template is the content of the short message. If there are parameters in the short message, it can be set by referring to Ali's parameter template.

These things are ready for development.

2. Integrated Short Message Function

1. Download two jar packages of Ali SMS and import them into the project
2. Coding with reference to official demo
I wrote a tool class myself. It's very simple.
If there is more demand, it can be expanded on the basis of it.

/**
 * Short Message Sending Tool
 * @author YangYang_2000
 * @version 1.0
 * @date 2017 28 December
 */
public class SmsUtil {
    // Product Name: Cloud Communication Short Message API products, developers do not need to replace
    private static final String product = "Dysmsapi";
    // Product domain name, developers do not need to replace
    private static final String domain = "dysmsapi.aliyuncs.com";

    // This needs to be replaced by the developer's own AK (found in the Aliyun Access Console)
    private static String accessKeyId = "Own AccessKeyId";
    private static String accessKeySecret = "Own AccessKeySecert";
    private static String identifyingTempleteCode = "My own short message template";
    private static String singName = "Short Message Signature";
    /**
    * Send Short Message Verification Code
    */
    public static SendSmsResponse sendIdentifyingCode(String mobile, String code) throws Exception {
        try {
            return sendSms(mobile, "{\"code\":\"" + code + "\"}");
        } catch (ClientException e) {
            throw new Exception();
        }
    }
    /**
    * Sending SMS
    */
    public static SendSmsResponse sendSms(String mobile, String templateParam) throws ClientException {
        // Adjustable timeout
        System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
        System.setProperty("sun.net.client.defaultReadTimeout", "10000");
        // Initialization of acsClient does not support region ization for the time being
        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
        DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
        IAcsClient acsClient = new DefaultAcsClient(profile);
        // Assemble Request Objects - See Console for Specific Description - Document section
        SendSmsRequest request = new SendSmsRequest();
        // Mandatory: Mobile number to be sent
        request.setPhoneNumbers(mobile);
        // Mandatory: Short Message Signature - Can be found in the Short Message Console
        request.setSignName(singName);
        // Mandatory: Short Message Template - Can be found in the Short Message Console
        request.setTemplateCode(identifyingTempleteCode);
        // Optional: Replace the JSON string with variables in the template, such as "Dear ${name} and your verification code ${code}", where the value is
        request.setTemplateParam(templateParam);
        // Fill-in-Uplink SMS Extension Code (please ignore this field for users with no special needs)
        // request.setSmsUpExtendCode("90997");
        // Optional: outId extends the field to the business side and eventually brings this value back to the caller in a short message reply message
        request.setOutId("yourOutId");
        // hint may throw an exception here, notice catch
        SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
        return sendSmsResponse;
    }
}


/**
 * Short Message Status Monitoring Tool
 * @author YangYang_2000
 * @version 1.0
 * @date 2017 28 December
 */
public class SmsCheckUtil {
    /**
    * Detection of SMS Sending Status Based on Receipt Code
    */
    public static String checkCode(String jsonStr, String code) {
        if (code.equals(SmsCommon.SMS_OK)) {//Successful sending, implementation of interface
            return SmsCommon.SMS_OK;
        } else if (code.equals(SmsCommon.SMS_MOBILE_NUMBER_ILLEGAL)) {//Mobile phone number format error
            jsonStr = ResponseUtil.getRspFailObj(Return.CODE_MOBILE_TYPE_FAIL, Return.MSG_MOBILE_TYPE_FAIL);
            return jsonStr;
        } else if (code.equals(SmsCommon.SMS_BUSINESS_LIMIT_CONTROL)) {//Business Current Limitation, Prohibit Multiple Distribution
            jsonStr = ResponseUtil.getRspFailObj(Return.CODE_MOBILE_TIEE, Return.MSG_MOBILE_TIEE);
            return jsonStr;
        } else {//Uncommonly used exceptions are not judged for the time being, and uniformly return to the internal exceptions of the server.
            jsonStr = ResponseUtil.getRspFailObj(Return.CODE_EXCEPTION, Return.MSG_EXCEPTION);
            return jsonStr;
        }
    }
}


/**
 * Short Message Return Code
 *
 * @author YangYang_2000
 * @version 1.0
 * @date 2017 28 December
 * @see https://help.aliyun.com/knowledge_detail/57717.html
 */
public class SmsCommon {
    // Send successfully
    public static String SMS_OK = "OK";
    // If the balance is greater than zero, please contact the engineer by creating the work order.
    public static String SMS_OUT_OF_SERVICE = "isv.OUT_OF_SERVICE";
    // Illegal SMS Template
    public static String SMS_TEMPLATE_ILLEGAL = "isv.SMS_TEMPLATE_ILLEGAL";
    // Illegal Short Message Signature
    public static String SMS_SIGNATURE_ILLEGAL = "isv.SMS_SIGNATURE_ILLEGAL";
    // Parameter exception
    public static String SMS_INVALID_PARAMETERS = "isv.INVALID_PARAMETERS";
    // Illegal mobile phone number
    public static String SMS_MOBILE_NUMBER_ILLEGAL = "isv.MOBILE_NUMBER_ILLEGAL";
    // Template missing variables
    public static String SMS_TEMPLATE_MISSING_PARAMETERS = "isv.TEMPLATE_MISSING_PARAMETERS";
    // Service current limiting
    public static String SMS_BUSINESS_LIMIT_CONTROL = "isv.BUSINESS_LIMIT_CONTROL";
    // Insufficient account balance
    public static String SMS_AMOUNT_NOT_ENOUGH = "isv.AMOUNT_NOT_ENOUGH";
}

3. Call, test
SendSmsResponse response = SmsUtil.sendIdentifyingCode(mobile,sixCode);
After successful debugging, the background returns to app Json with a prompt: {"code": 0, "data": {}, "message": "successful sending"}
The mobile phone receives short messages:

Registration or login using authentication code

At the back end, a random six-digit number is generated and sent to the mobile phone, and the verification code database is inserted at the same time.
Determine the corresponding relationship between mobile phone number and authentication number in the table when logging in or registering.
After successful login or registration, the consumed authentication code is deleted from the database.

Fourth, recharge and check the arrival rate of short messages

After the integration is successful, the error code will be found to indicate that the account balance is insufficient after the test.
At this time to Ali's SMS console, to recharge, in the absence of purchasing packages, a text message is 0.045 yuan, or 4:5.
It's rather expensive. Save 10 yuan.

Five, summary

Ali's short message arrival rate is still very high. More than a dozen were sent, none of them failed.

There is a long way to go. Vegetables are not the original sin, but degeneration is the original sin.
My CSDN: https://blog.csdn.net/wuyangyang_2000
My brief book: https://www.jianshu.com/u/20c2f2c3560a
My Nuggets: https://juejin.im/user/58009b94a0bb9f00586bb8a0
My GitHub: https://github.com/wuyang2000
Personal website: http://www.xiyangkeji.cn
Personal app (Cissie) dandelion connection: https://www.pgyer.com/KMdT
My Wechat Public Number: Qi Yang (regular promotion of high-quality technical articles, welcome attention)
Android Technology Exchange Group: 691174792

The above articles can be reproduced, please indicate the original.

Keywords: Mobile JSON Database github

Added by FluxNYC on Mon, 16 Sep 2019 11:28:32 +0300