Reputation: 1125
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
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.
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.
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<receiver
android:name="com.example.MyBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
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:
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