android - Twilio with Parse on Android 做短信验证

标签 android sms parse-platform twilio

我正在尝试使用 Parse 中的 Twilio 云模块发送短信验证。 我该怎么做。我没有任何准确的安卓教程或使用指南。如何将参数发送到云函数?

最佳答案

您可以在 Parse 上创建两个 Cloud Function,一个用于向用户发送验证码,一个用于验证用户的电话号码。

要开始使用 Parse 云函数,请查看本指南 https://www.parse.com/docs/js/guide#cloud_modules

设置好Parse工具后,进入main.js文件并添加这两个函数

// Include the Twilio Cloud Module and initialize it
var twilio = require('twilio')('<Your Twilio Account Sid>', '<Your Twilio Auth Token>');

// Define the Cloud Functions

Parse.Cloud.define("sendVerificationCode", function(request, response) {
    var verificationCode = Math.floor(Math.random()*999999);
    var user = Parse.User.current();
    user.set("phoneVerificationCode", verificationCode);
    user.save();

    twilio.sendSms({
        From: "<Your Twilio phone number>",
        To: request.params.phoneNumber,
        Body: "Your verification code is " + verificationCode + "."
    }, function(err, responseData) { 
        if (err) {
            response.error(err);
        } else { 
            response.success("Success");
        }
    });
});

Parse.Cloud.define("verifyPhoneNumber", function(request, response) {
    var user = Parse.User.current();
    var verificationCode = user.get("phoneVerificationCode");
    if (verificationCode == request.params.phoneVerificationCode) {
        user.set("phoneNumber", request.params.phoneNumber);
        user.save();
        response.success("Success");
    } else {
        response.error("Invalid verification code.");
    }
});

之后,在你的Android项目中你可以请求手机验证如下

HashMap<String, Object> params = new HashMap<String, Object>();
params.put("phoneNumber", phoneNumber);
progressBar.setVisibility(View.VISIBLE);
ParseCloud.callFunctionInBackground("sendVerificationCode", params, new FunctionCallback<JSONObject>() {
    public void done(JSONObject response, ParseException e) {
        progressBar.setVisibility(View.GONE);
        if (e == null) {
             Log.d("Response", "no exceptions! " + response.toString());
             // Code sent successfully you have to wait it or ask the user to enter the code for verification
        } else {
             Log.d("Response", "Exception: " + response.toString() + e);
             Toast.makeText(getApplicationContext(),  "Something wrong.  Please try again." + e, Toast.LENGTH_LONG).show(); 
        }
    }
});

然后验证用户(收到)/(输入)用户的代码

HashMap<String, Object> params = new HashMap<String, Object>();
params.put("phoneNumber", phoneNumber);
params.put("phoneVerificationCode", code);
progressBar.setVisibility(View.VISIBLE);
ParseCloud.callFunctionInBackground("verifyPhoneNumber", params, new FunctionCallback<String>() {
    public void done(String response, ParseException e) {
        progressBar.setVisibility(View.GONE);
        if (e == null) {
            token = response;
            Log.d("Response", "no exceptions! " + response);
            ParseUser.becomeInBackground(token, new LogInCallback() {
                @Override
                public void done(ParseUser parseUser, ParseException e) {
                    if (e == null){
                        Log.d("Response", "no exceptions! ");
                        Intent i = new Intent(LoginActivity.this, MainActivity.class);
                        startActivity(i);
                        finish();
                    } else {
                        Log.d("Response", "Exception: " + e);
                        Toast.makeText(getApplicationContext(),"Something wrong.  Please try again." + e, Toast.LENGTH_LONG).show();
                    }
                }
            });
        } else {
            Log.d("Response", "Exception: " + response + e);
            Toast.makeText(getApplicationContext(), "Something wrong.  Please try again.", Toast.LENGTH_LONG).show();
        }
    }
});

关于android - Twilio with Parse on Android 做短信验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25492763/

相关文章:

ios - 如果在 iOS 上使用系统帐户进行​​身份验证,则无法检索 Twitter 电子邮件

c# - 使用 asp.net C# 通过 GSM 调制解调器发送短信

android - react native TouchableNativeFeedback 波纹太慢

android - 使用 Monkey Test 时如何防止输入设置

android - 如何使用ActionMenuView?

Android 发送 SMS 线程中可见的 SMS(无 GUI)

android - 试图让 HTML HREF SMS 链接在 jasonette for android 中工作

ios - 使用 canEditRowAtIndexPath 从解析中删除图像

javascript - 解析 Javascript API Cloud 代码 afterSave 并访问 beforeSave 值

java - android中word文档转pdf