Cannot Receive Incoming call on Twilio android Client

1k views Asked by At

I am Developing an android application for making calls,using the Twilio API. I am able to make an outgoing call from the twilio android client,but I cannot receive any incoming calls. So what additional code needs to be written on the server side apart from assigning the incoming capability. I am using node js for the server.

//android code

    public void onInitialized() {
    new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        capabilityToken = HttpHelper
                                .httpGet("/my server url/token");   
                        device = Twilio.createDevice(capabilityToken,null);

                    /* Code to handle incoming connections */

                    Intent intent = new Intent(context,HelloMonkeyActivity.class);
  PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent,
                                PendingIntent.FLAG_UPDATE_CURRENT);
                device.setIncomingIntent(pendingIntent);

                    } catch (Exception e) {
                        Log.e(TAG,
                                "Failed to obtain capability token: "
                                        + e.getLocalizedMessage());
                    }
                }

            }).start();

        }


//establish a connection
    public void handleIncomingConnections(Device inDevice,
            Connection inConnection) {
        if (connection != null)
            connection.disconnect();
        connection = inConnection;
        connection.accept();
    }

//server code

    var express=require('express');
     var app=express();
     var twilio=require('twilio');
     var acctId='my account id';
     var authToken='my token';
     var applicationId='my apps id';

    app.get('/token',function(req,res){
        var capability=new twilio.Capability(acctId,authToken);
        capability.allowClientIncoming('client');
        capability.allowClientOutgoing(applicationId);
        var token=capability.generate();
        console.log("token:"+token);
        res.send(token);
    });

    app.get('/call',function(req,res){
    var caller_id="//Twilio number";
    var twiml=new twilio.TwimlResponse();
    var dialTo=req.query.DialTo;
    twiml.dial(dialTo,{callerId:caller_id});
    res.send(twiml.toString());
});
3

There are 3 answers

0
chamlingd On
6
Devin Rader On

Twilio evangelist here.

It looks like you are not wiring up the Device's Listener when you call createDevice.

device = Twilio.createDevice(capabilityToken,null);

Should be:

device = Twilio.createDevice(capabilityToken,this);

Where this implements the DeviceListener interface:

public class MonkeyPhone implements Twilio.InitListener, DeviceListener

Twilio uses the DeviceListner interface in order to launch the activity that notifies you that there is an incoming connection.

I would also suggest checking your Twilio Call logs to ensure that there are no issues with Twilio retreiving the TwiML needed in order to forward the inbound call to this instance of Twilio Client and verify that Twilio is attempting to make the outbound call to the correct Client instance.

Remember that in order to direct an inbound call to your Android application running Twilio Client you'll need to use the <Dial> verb and the <Client> noun:

<Response>
    <Dial>
        <Client>[your_client_instance_name]</Client>
    </Dial>
</Response>

Hope that helps.

0
New Coder On

Implement these classes Twilio.InitListener, DeviceListener, ConnectionListener

and then initialize the device class

device = Twilio.createDevice(token, Your_Class.this);

            Intent intent = new Intent(context, YourClass.class);
            PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
            device.setIncomingIntent(pendingIntent);

This works for me well .. Hope works for you too!