0

I'm currently try to implement a two factor authentication system on a project i'm working on using twilio as a sms gateway service to request a random login token and then send it to the user as a text message. I followed the tutorial found here "https://www.twilio.com/blog/2016/05/how-to-send-an-sms-from-android.html" to test the service out. Following the tutorial I hosted the backend on Heroku. The app works just fine and says that the sms has been sent. However I never receive it. Any help would great.

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.content.Context;
import java.io.IOException;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {

 private EditText mTo;
 private EditText mBody;
 private Button mSend;
 private OkHttpClient mClient = new OkHttpClient();
 private Context mContext;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mTo = (EditText) findViewById(R.id.txtNumber);
    mBody = (EditText) findViewById(R.id.txtMessage);
    mSend = (Button) findViewById(R.id.btnSend);
    mSend.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            try {
                post(" https://cryptic-shore-79857.herokuapp.com", new 
Callback(){

                    @Override
                    public void onFailure(Call call, IOException e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onResponse(Call call, Response response) 
 throws IOException {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                mTo.setText("");
                                mBody.setText("");
                                Toast.makeText(getApplicationContext(),"SMS Sent!",Toast.LENGTH_SHORT).show();
                            }
                        });
                    }
                });
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    });
    mContext = getApplicationContext();
}
Call post(String url, Callback callback) throws IOException {
    RequestBody formBody = new FormBody.Builder()
            .add("To", mTo.getText().toString())
            .add("Body", mBody.getText().toString())
            .build();
    Request request = new Request.Builder()
            .url(url)
            .post(formBody)
            .build();
    Call response = mClient.newCall(request);
    response.enqueue(callback);
    return response;
}
}

I'm thinking the URL that connects to Heroku is incorrect but I have no idea what it should be.

Wshivnarine
  • 23
  • 2
  • 7
  • Have you tested the back end Heroku application without the app to make sure that it works? You could use curl or [Postman](https://www.getpostman.com/) to send the request. – philnash Apr 18 '17 at 09:29
  • I have used postman to test the get function which worked but the post function didn't get me a response @philnash – Wshivnarine Apr 18 '17 at 16:41
  • The app from the blog post would only have worked (as far as I can see) with POST. Can you share the backend code you are using for sending the message? – philnash Apr 18 '17 at 17:23
  • the backend I'm using the one that was provided in the blog. I wanted to test the functionality of the service before moving forward. I just clicked the heroku button to host it there. – Wshivnarine Apr 19 '17 at 05:16
  • From what I can see, [the app expects a `POST` request](https://github.com/mplacona/twilio-sms-spark/blob/master/src/main/java/SMSBackend.java#L30). When you make a `GET` request to it, do you actually receive the SMS message? – philnash Apr 19 '17 at 08:02
  • using postman GET receives a hello world, I don't get anything using the POST function on the app or using postman. Do you think I should download the backend and edit it , i didn't want to stray too far from the blogs instructions initially. – Wshivnarine Apr 19 '17 at 13:54
  • @philnash I just checked the backend code there is a post method that is supposed to build and provide an Sms message to my twilio number. deploying the app to heroku I provided the number that was given to me when I signed up – Wshivnarine Apr 19 '17 at 16:12

1 Answers1

0

Twilio developer evangelist here.

You're POSTing your request to the wrong URL. Currently your code does:

try {
    post("https://cryptic-shore-79857.herokuapp.com", new 
Callback(){

But the path for the action that sends the SMS should be:

try {
    post("https://cryptic-shore-79857.herokuapp.com/sms", new 
Callback(){

Note, the /sms path.

Let me know if that helps at all.

philnash
  • 70,667
  • 10
  • 60
  • 88
  • That does work, and I receive the "sms sent" message. However I never actually receive the text message. Could the issue be with my twilio account or on the heroku backend. @philnash – Wshivnarine Apr 21 '17 at 14:50
  • Are you using your "test credentials" in the heroku account? – philnash Apr 21 '17 at 14:50
  • I was not, I just tried changing my SID and Authtoken but didn't change my the number i received from twilio and received nothing. Should i change the phone number to my own instead. – Wshivnarine Apr 21 '17 at 16:23
  • You'll need to make sure you're using the production credentials from Twilio. Make sure you set the heroku environment variable `TWILIO_NUMBER` to a Twilio number that you own and that can send SMS messages and that you have verified the phone number you are trying to send the message to in your Twilio account. – philnash Apr 21 '17 at 16:26
  • I've changed the the SID and Authtoken to the test credentials and the number is the one I received from twilio being used to send a text message to the number I used to create the account. I still have not received a the text message I tried to send. I do appreciate the time your taking to help me through this. – Wshivnarine Apr 21 '17 at 16:34
  • Ok, definitely don't use the test credentials, they will appear to work successfully but won't send the message. You must use the production credentials to send the message. Is there anything in your [Twilio debugger](https://www.twilio.com/console/dev-tools/debugger)? – philnash Apr 21 '17 at 16:36
  • I changed them back. There's nothing in my twilio debugger. My logcat debugger is telling me "finish composing text on inactive inputconnection" it doesn't read as an error though. – Wshivnarine Apr 21 '17 at 16:47
  • Your logcat debug message is probably about [clearing the text from your input](http://stackoverflow.com/questions/8122625/getextractedtext-on-inactive-inputconnection-warning-on-android). Are you able to check the status of the response you receive from the heroku action? Have you managed to send an SMS message using Postman and the correct URL for your application (`https://cryptic-shore-79857.herokuapp.com/sms`)? – philnash Apr 21 '17 at 17:03
  • I just tried to send a POST request to your application and I got a 500 error. Can you check the heroku logs to see what the errors from the web application are? – philnash Apr 21 '17 at 17:05
  • I'm looking at it now. at=info method=POST path="/sms" host=cryptic-shore-79857.herokuapp.com request_id=c7bcc9d2-37b1-4d80-85e5-6db112ef319f fwd="104.39.40.79" dyno=web.1 connect=1ms service=4ms status=500 bytes=208 protocol=https' – Wshivnarine Apr 21 '17 at 17:17
  • So, I passed the parameters I needed, so either your account sid, auth token or Twilio number isn't set properly. – philnash Apr 21 '17 at 17:18
  • I just got it to work . I still don't understand what went wrong as these were the credentials I initially used. But I thank you for the help you've provided – Wshivnarine Apr 21 '17 at 17:24
  • Hey, since you were able to fix this, would you be able to mark the answer as accepted? It helps other people who may have had the same problem. – philnash Apr 25 '17 at 12:20