使用MobTech平台实现免费的短信验证码验证功能

一、技术概述

使用短信验证码验证注册、登录和找回密码几乎是每一个APP、甚至是许多网页所需要支持的技术。对于我们学生完成非商用项目,往往需要一个免费提供短信验证码技术支持的SDK,而许多平台需要收费,很难找到适合的平台。

二、技术详述

1.在MobTech中获取App Key和App Secret

(1)首先进入MobTech官网:https://www.mob.com/

(2)登陆过后,选择开发者服务,点击SMSSDK。

(3)点击开始使用

(4)点击创建应用,按要求填写信息后创建,并接入SMSSDK

(5)随后点击创建好的应用查看应用的App Key和App Secret

2.实现短信验证码验证功能

这里给出MobTech的开发文档链接:https://www.mob.com/wiki/detailed?wiki=SMSSDK_for_Android_kuaisujicheng&id=23

(1)在项目中相应位置插入脚本和MobSDK插件和扩展

对应项目中插入脚本: 对应项目中插入MobSDK插件和扩展:

脚本代码:

buildscript {

    repositories {
        jcenter()
    }

    dependencies {
        // 注册MobSDK
        classpath "com.mob.sdk:MobSDK:2018.0319.1724"
    }
}

MobSDK插件和扩展:

apply plugin: 'com.mob.sdk'

MobSDK {
    appKey "替换为mob官方申请的appkey"
    appSecret "替换为mob官方申请的appkey对应的appSecret"
    SMSSDK {}
}

(2)功能实现

i)短信验证按钮60s计时

    Handler hd = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == CODE_REPEAT) {
                btn_check.setEnabled(true);
                btn_sure.setEnabled(true);
                tm.cancel();//取消任务
                tt.cancel();//取消任务
                TIME = 60;//时间重置
                btn_check.setText("重新发送验证码");
            }else {
                btn_check.setText(TIME + "重新发送验证码");
            }
        }
    };

ii)回调

    EventHandler eh=new EventHandler(){
        @Override
        public void afterEvent(int event, int result, Object data) {
            if (result == SMSSDK.RESULT_COMPLETE) {
                if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {
                    phoneNum = et_phonenum.getText().toString();
                    password = et_password.getText().toString();
                    userName = et_userName.getText().toString();
                    //
                    //这里将数据userName password phoneNum发送到数据库
                    //
                    //
                    Intent intent = new Intent(RegisterActivity.this,MainActivity.class);
                    intent.putExtra("phone",phoneNum);
                    startActivity(intent);
                    toast("验证成功");
                }else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE){       //获取验证码成功
                    toast("获取验证码成功");
                }else if (event ==SMSSDK.EVENT_GET_SUPPORTED_COUNTRIES){//如果你调用了获取国家区号类表会在这里回调
                    //返回支持发送验证码的国家列表
                }
            }else{//错误等在这里(包括验证失败)
                //错误码请参照http://wiki.mob.com/android-api-错误码参考/这里我就不再继续写了
                toast("验证码不匹配,请重新输入验证码");
            }
        }
    };

iii)弹窗确认下发

    private void alterWarning() {
        //构造器
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("提示"); //设置标题
        builder.setMessage("我们将要发送到" + phone + "验证"); //设置内容
        builder.setIcon(R.mipmap.ic_launcher);//设置图标,图片id即可
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            //设置确定按钮
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss(); //关闭dialog
                //通过sdk发送短信验证(请求获取短信验证码,在监听(eh)中返回)
                SMSSDK.getVerificationCode(country, phone);
                //做倒计时操作
                Toast.makeText(RegisterActivity.this, "已发送" + which, Toast.LENGTH_SHORT).show();
                btn_check.setEnabled(false);
                btn_sure.setEnabled(true);
                tm = new Timer();
                tt = new TimerTask() {
                    @Override
                    public void run() {
                        hd.sendEmptyMessage(TIME--);
                    }
                };
                tm.schedule(tt,0,1000);
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { //设置取消按钮
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                Toast.makeText(RegisterActivity.this, "已取消" + which, Toast.LENGTH_SHORT).show();
            }
        });
        //参数都设置完成了,创建并显示出来
        builder.create().show();
    }

iiii)销毁短信注册

    protected void onDestroy() {
        super.onDestroy();
        // 注销回调接口registerEventHandler必须和unregisterEventHandler配套使用,否则可能造成内存泄漏。
        SMSSDK.unregisterEventHandler(eh);

    }

下面给出完整前后端代码:

前端:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="50dp"
    android:gravity="center"
    tools:context="com.example.messagetest.RegisterActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width="70dp"
            android:layout_height="wrap_content"
            android:text="用户名:"
            android:textSize="20dp"
            android:layout_weight="0"/>

        <EditText
            android:inputType="phone"
            android:id="@+id/reg_et_userName"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width="70dp"
            android:layout_height="wrap_content"
            android:text="手机号:"
            android:textSize="20dp"
            android:layout_weight="0"/>

        <EditText
            android:inputType="phone"
            android:id="@+id/reg_et_phonenum"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width="70dp"
            android:layout_height="wrap_content"
            android:text="密   码:"
            android:textSize="20dp"
            android:layout_weight="0"/>

        <EditText
            android:inputType="phone"
            android:id="@+id/reg_et_key"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            />

    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <EditText
            android:id="@+id/reg_et_checkecode"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:hint="验证码" />

        <Button
            android:background="@color/colorPrimary"
            android:id="@+id/reg_btn_check"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="获取验证码" />


    </LinearLayout>
    <Button
        android:background="@color/colorAccent"
        android:id="@+id/reg_btn_register"
        android:layout_marginTop="20dp"
        android:textColor="#131313"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="注册" />

</LinearLayout>

后端:

package com.example.messagetest;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.mob.MobSDK;

import java.util.Timer;
import java.util.TimerTask;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import cn.smssdk.EventHandler;
import cn.smssdk.SMSSDK;

public class RegisterActivity extends AppCompatActivity{

    private TimerTask tt;
    private Timer tm;
    private EditText et_phonenum;
    private EditText et_userName;
    private Button btn_check;
    private EditText et_checkecode;
    private Button btn_sure;
    private EditText et_password;
    private String password;
    private String userName;
    private String phoneNum;
    private int TIME = 60;//倒计时60s这里应该多设置些因为mob后台需要60s,我们前端会有差异的建议设置90,100或者120
    public String country="86";//这是中国区号,如果需要其他国家列表,可以使用getSupportedCountries();获得国家区号
    private String phone;
    private static final int CODE_REPEAT = 1; //重新发送


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        MobSDK.init(this, "24793dde94dc6", "6e636da9b16e5bf8d5fae19ca30ea6ac");
        SMSSDK.registerEventHandler(eh); //注册短信回调(记得销毁,避免泄露内存)
        et_password =(EditText)findViewById(R.id.reg_et_key) ;
        et_phonenum = (EditText) findViewById(R.id.reg_et_phonenum);
        et_userName = (EditText) findViewById(R.id.reg_et_userName);
        btn_check = (Button) findViewById(R.id.reg_btn_check);
        et_checkecode = (EditText) findViewById(R.id.reg_et_checkecode);
        btn_sure = (Button) findViewById(R.id.reg_btn_register);
        et_password=(EditText) findViewById(R.id.reg_et_key);
        btn_check.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                phone = et_phonenum.getText().toString().trim().replaceAll("/s","");
                if (!TextUtils.isEmpty(phone)) {
                    //定义需要匹配的正则表达式的规则
                    String REGEX_MOBILE_SIMPLE =  "[1][358]\\d{9}";
                    //把正则表达式的规则编译成模板
                    Pattern pattern = Pattern.compile(REGEX_MOBILE_SIMPLE);
                    //把需要匹配的字符给模板匹配,获得匹配器
                    Matcher matcher = pattern.matcher(phone);
                    // 通过匹配器查找是否有该字符,不可重复调用重复调用matcher.find()
                    if (matcher.find()) {//匹配手机号是否存在
                        alterWarning();

                    } else {
                        toast("手机号格式错误");
                    }
                } else {
                    toast("请先输入手机号");
                }
            }
        });

        btn_sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //获得用户输入的验证码
                String name = et_userName.getText().toString().replaceAll("/s","");
                String code = et_checkecode.getText().toString().replaceAll("/s","");
                String pn = et_phonenum.getText().toString().trim().replaceAll("/s","");
                String pw = et_password.getText().toString().replaceAll("/s","");

                if (TextUtils.isEmpty(name)) {//判断用户名是否为空
                    toast("请输入用户名");
                }
                else if (!TextUtils.isEmpty(name)) {//用户名非空的情况下判断唯一性
                    /**
                     *
                     *
                     * 判断填写的用户名(这里的变量是name)是否是唯一的
                     *
                     *
                     */
                }
                else if (TextUtils.isEmpty(pn)) {//判断手机号是否为空
                    toast("请输入手机号");
                }
                else if (!TextUtils.isEmpty(pn)) {//手机号非空的情况下判断唯一性
                    /**
                     *
                     *
                     *
                     * 判断填写的手机号(这里的变量是pn)是否是唯一的
                     *
                     *
                     */
                }
                else if (TextUtils.isEmpty(pw)) {//判断密码是否为空
                    toast("请输入密码");
                }
                else if (!TextUtils.isEmpty(code)) {//判断验证码是否为空
                    //验证
                    SMSSDK.submitVerificationCode( country,  phone,  code);
                }else{//如果用户输入的内容为空,提醒用户
                    toast("请输入验证码后再提交");
                }
            }
        });
    }


    Handler hd = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == CODE_REPEAT) {
                btn_check.setEnabled(true);
                btn_sure.setEnabled(true);
                tm.cancel();//取消任务
                tt.cancel();//取消任务
                TIME = 60;//时间重置
                btn_check.setText("重新发送验证码");
            }else {
                btn_check.setText(TIME + "重新发送验证码");
            }
        }
    };
    //回调
    EventHandler eh=new EventHandler(){
        @Override
        public void afterEvent(int event, int result, Object data) {
            if (result == SMSSDK.RESULT_COMPLETE) {
                if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {
                    phoneNum = et_phonenum.getText().toString();
                    password = et_password.getText().toString();
                    userName = et_userName.getText().toString();
                    //
                    //这里将数据userName password phoneNum发送到数据库
                    //
                    //
                    Intent intent = new Intent(RegisterActivity.this,MainActivity.class);
                    intent.putExtra("phone",phoneNum);
                    startActivity(intent);
                    toast("验证成功");
                }else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE){       //获取验证码成功
                    toast("获取验证码成功");
                }else if (event ==SMSSDK.EVENT_GET_SUPPORTED_COUNTRIES){//如果你调用了获取国家区号类表会在这里回调
                    //返回支持发送验证码的国家列表
                }
            }else{//错误等在这里(包括验证失败)
                //错误码请参照http://wiki.mob.com/android-api-错误码参考/这里我就不再继续写了
                toast("验证码不匹配,请重新输入验证码");
            }
        }
    };
    //吐司的一个小方法
    private void toast(final String str) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(RegisterActivity.this, str, Toast.LENGTH_SHORT).show();
            }
        });
    }
    //弹窗确认下发
    private void alterWarning() {
        //构造器
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("提示"); //设置标题
        builder.setMessage("我们将要发送到" + phone + "验证"); //设置内容
        builder.setIcon(R.mipmap.ic_launcher);//设置图标,图片id即可
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            //设置确定按钮
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss(); //关闭dialog
                //通过sdk发送短信验证(请求获取短信验证码,在监听(eh)中返回)
                SMSSDK.getVerificationCode(country, phone);
                //做倒计时操作
                Toast.makeText(RegisterActivity.this, "已发送" + which, Toast.LENGTH_SHORT).show();
                btn_check.setEnabled(false);
                btn_sure.setEnabled(true);
                tm = new Timer();
                tt = new TimerTask() {
                    @Override
                    public void run() {
                        hd.sendEmptyMessage(TIME--);
                    }
                };
                tm.schedule(tt,0,1000);
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { //设置取消按钮
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                Toast.makeText(RegisterActivity.this, "已取消" + which, Toast.LENGTH_SHORT).show();
            }
        });
        //参数都设置完成了,创建并显示出来
        builder.create().show();
    }

    //销毁短信注册
    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 注销回调接口registerEventHandler必须和unregisterEventHandler配套使用,否则可能造成内存泄漏。
        SMSSDK.unregisterEventHandler(eh);

    }

}

短信验证码验证其他功能和接口可以参照实现短信验证码功能开头给的文档

三、遇到的问题和解决过程

(1)
在参考文档时,短信验证码错误的处理有图下画框这一句

在我实际开发时,输入错误的短信验证码会直接跳转到登录页面,并且没有任何提示,会让用户误以为验证码通过。在排查了过后觉得是验证码错误这个else分支里的问题,最后我改成了使用toast告诉用户验证码错误,如下图所示:

(2)
在开发完成过后,我们小组测试安卓APP时,发现收不到短信的情况。最开始以为是出现了BUG,但是经过我排查过后发现不应该有BUG,感到很奇怪。组长突然提醒我,说我之前好像提到过每天接收短信是有上限的,仔细阅读文档过后找到了收不到短信的原因。
需要注意的是,每天提供的免费短信只有20条,每个手机最多只能收10条等,若达到发送的短信上限,将不会再收到短信(具体请阅读文档,下面给出部分截图)

其余遇见问题我认为大部分参考文档可以解决。

四、总结

(1)使用短信验证码验证注册、登录和找回密码几乎是每一个APP、甚至是许多网页所需要支持的技术。对于我们学生完成非商用项目,往往需要一个免费提供短信验证码技术支持的SDK,而许多平台需要收费,我在寻找免费的平台上花了许多时间,最终发现MobTech是一个很适合学生在课程上开发的项目使用,提供的SDK和接口都很方便实用,且每天提供20条免费的短信验证码。
(2)在使用SDK前,建议先仔细阅读文档,上面有完整的产品和接口等的介绍,其实在开发中遇到的大部分问题都可以在文档中找到解决方案。
(3)由于我在使用中遇到了直接跳转到登录页面的问题,后面改成了toast直接告诉验证码错误更合适。

五、参考

1.MobTech文档中心\SMSSDK 作者:MobTech
2.解决mob网站短信验证SDK更新后,android studio下的mob短信验证接入问题 作者:haibowen
3.Android开发实现短信验证码功能(总结) 作业:lpCrazyBoy
4.Android实现短信验证功能(功能的使用) 作者:陈旭金-小金子

posted @ 2020-06-25 14:47  福州大学软件工程专业  阅读(1734)  评论(0编辑  收藏  举报