I have the Android project. I use Gradle and Kotlin.
I need to realize the SMS sending mechanism with Twilio.
I used this manual: How to Send an SMS from Android
So, I have created the Backend.
Here is the build.gradle
for Backend:
group 'com.my.app'
version '1.0-SNAPSHOT'
buildscript {
repositories { jcenter() }
dependencies {
classpath 'com.github.jengelman.gradle.plugins:shadow:1.2.3'
}
}
apply plugin: 'java'
apply plugin: 'application'
apply plugin: 'com.github.johnrengelman.shadow'
sourceCompatibility = 1.8
mainClassName = 'SMSBackend'
repositories {
mavenCentral()
}
dependencies {
testImplementation group: 'junit', name: 'junit', version: '4.12'
implementation 'com.sparkjava:spark-core:2.6.0'
implementation group: 'com.twilio.sdk', name: 'twilio', version: '7.9.0'
implementation 'org.slf4j:slf4j-simple:1.6.1'
}
And here is the main activity - SMSBackend.java
import com.twilio.http.TwilioRestClient;
import com.twilio.rest.api.v2010.account.Message;
import com.twilio.rest.api.v2010.account.MessageCreator;
import com.twilio.type.PhoneNumber;
import static spark.Spark.*;
import static spark.Spark.get;
import static spark.Spark.post;
public class SMSBackend {
public static void main(String[] args) {
get("/", (req, res) -> "Hello, World!");
TwilioRestClient client = new TwilioRestClient.Builder(System.getenv("AC749....d462247b104a"), System.getenv("096e4788......ed95cc36c")).build();
post("/sms", (req, res) -> {
String body = req.queryParams("Body");
String to = req.queryParams("To");
String from = "+15037381694";
Message message = new MessageCreator(
new PhoneNumber(to),
new PhoneNumber(from),
body).create(client);
return message.getSid();
});
}
}
After that, I use ngrok in order this app becomes available externally.
I type the command:
ngrok http 4567
And have an output as the link below:
And now I go to my Android application. I need to enter my telephone number, click on the button and after that receive the SMS.
For this, I put the link from ngrok
into my activity.
My MyActivity.kt
package com.my.app
import android.content.Context
import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.Menu
import android.view.View
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import android.widget.Toast
import java.io.IOException;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
class MyActivity : AppCompatActivity() {
private val mClient = OkHttpClient()
private val mContext = getApplicationContext()
private val message = resources.getString(R.string.textSMS)
private val userPhoneNumber = findViewById(R.id.editText) as EditText
private val buttonSend = findViewById(R.id.button) as Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_my)
buttonSend.setOnClickListener {
post(("https://2b722554.ngrok.io/sms"), object : Callback {
override fun onFailure(call: Call, e: IOException) {
e.printStackTrace()
}
@Throws(IOException::class)
override fun onResponse(call: Call, response: Response) {
runOnUiThread(object : Runnable {
public override fun run() {
userPhoneNumber.setText("")
message.setText("")
Toast.makeText(applicationContext, "SMS Sent!", Toast.LENGTH_SHORT).show()
}
})
}
})
}
}
@Throws(IOException::class)
internal fun post(url: String, callback: Callback): Call {
val formBody = FormBody.Builder()
.add("To", userPhoneNumber.getText().toString())
.add("Body", message.getText().toString())
.build()
val request = Request.Builder()
.url(url)
.post(formBody)
.build()
val response = mClient.newCall(request)
response.enqueue(callback)
return response
}
So, when I run my application on the emulator or real device I can enter the number but the button is not working.
But I can send and receive the SMS from Twilio site, so, my Twilio number is Ok.
What am I missing?
UPD. I have changed all my kotline code. Now my application doesn't run on the emulator or real device