Ben-J
Ben-J

Reputation: 1125

Make Twilio listen for incoming call from device start up

I have integrated Twilio SDK in my app. I can receive incoming call AFTER my app has been started at least one time.

But if the user restart his phone and does not start my app, I can't reach him with a call.

How to make the Twilio SDK listen to incoming call from device start up? (without the need to launch my app at least one time)

Upvotes: 3

Views: 436

Answers (1)

Thom
Thom

Reputation: 19

The Twilio Client SDK does not provide a persistent service.

Furthermore, the Android SDK does not provide a reliable mechanism for persistent services for user applications. System applications can persist services.

Persistent Services

However, this use case can be accomplished by creating a BroadcastReceiver and register it to receive ACTION_BOOT_COMPLETED. The BroadcastReceiver will then handle the logic of making a client available to receive a call.

  1. In your AndroidManifest.xml add the required permission:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

  1. In your AndroidManifest.xml under your element add the

<receiver
        android:name="com.example.MyBroadcastReceiver">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
</receiver>
  1. Create the BroadcastReceiver to launch the Twilio Client SDK when receiving an Intent broadcast. To launch the SDK the following steps have to be executed:

    1. Initialize the Twilio Client SDK
    2. Retrieve the client's JWT capability
    3. Create a Device with the token
    4. Provide a PendingIntent to the Device to allow you to receive incoming calls - using an Activity in the example.

Checkout Twilio's Quickstart Tutorial

com.example

public class MyBroadcastReceiver extends BroadcastReceiver implements DeviceListener {

    private Device clientDevice;

    public void onReceive(final Context context, Intent intent) {

        // You'll need a mechanism to retrieve this time sensitive token.
        final String capabilityToken = "YOUR_JWT_TOKEN";

        // Initialize the Twilio Client SDK
        Twilio.initialize(context.getApplicationContext(), new Twilio.InitListener() {

            @Override
            public void onInitialized() {
                // Create a Device
                clientDevice = Twilio.createDevice(capabilityToken, MyBroadcastReceiver.this);

                // Providing a PendingIntent to the Device
                Intent intent = new Intent(context.getApplicationContext(), YourApplicationActivity.class);
                PendingIntent pendingIntent = PendingIntent.getActivity(context.getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
                clientDevice.setIncomingIntent(pendingIntent);
            }

            @Override
            public void onError(Exception e) {
                Log.e("MyBroadcastReceiver", e.toString());
            }

        });

        // Implements DeviceListener methods
    }
}

Alternatively, the BroadcastReceiver could launch one of your services which then handles any business logic related to the Twilio Client SDK.

Upvotes: 2

Related Questions