Custom call event handler on Android
A custom call event handler is your own Java class that receives VoIP call events on the native Android side. Use it to open your own call screen when the user answers, or to pass call events to a part of the app that the main WebView can’t reach, such as a second activity with its own WebView.
How the handler works
Anchor link to- For every call event, the plugin first sends the JavaScript event to the main WebView, then calls your handler. Your
registerEventlisteners keep working. - When a handler is set, the plugin doesn’t bring the app to the foreground on Answer. Without a handler, the plugin opens the app’s launcher activity. With a handler, open your screen in
onAnswer, or the app stays in the background. - The plugin reads the class name from the app manifest on the first call event after the app process starts, and creates the class through reflection. The class needs a public constructor without arguments.
- The callbacks don’t receive a
Context. Keep an applicationContextin your app, for example in yourApplicationsubclass, asMyApp.context()does in the example below.
Call events
Anchor link toThe handler implements com.pushwoosh.calls.listener.CallEventListener. Implement all eight methods, and leave the ones you don’t need empty.
| Method | Called when | JavaScript event |
|---|---|---|
onCreateIncomingConnection(Bundle payload) | An incoming call starts ringing | voipPushPayload |
onAnswer(PushwooshVoIPMessage message, int videoState) | The user answers | answer |
onReject(PushwooshVoIPMessage message) | The user declines | reject |
onDisconnect(PushwooshVoIPMessage message) | The call ends | hangup |
onCallCancelled(PushwooshVoIPMessage message) | A cancel push cancels the call | voipDidCancelCall |
onCallCancellationFailed(String callId, String reason) | A cancel push can’t be applied | voipDidFailToCancelCall |
onCallAdded(PushwooshVoIPMessage message) | The system in-call service adds the call | None |
onCallRemoved(PushwooshVoIPMessage message) | The system in-call service removes the call | None |
PushwooshVoIPMessage gives you the values from the VoIP push:
| Getter | Returns |
|---|---|
getCallId() | callId, or null if the push has none |
getCallerName() | callerName, or Unknown Caller if the push has none |
getHasVideo() | video |
getSupportsHolding() | supportsHolding |
getSupportsDTMF() | supportsDTMF |
getCancelCall() | cancelCall |
getRawPayload() | The whole push Bundle, including your custom keys |
onCreateIncomingConnection receives the raw Bundle. To use the getters there, wrap it: new PushwooshVoIPMessage(payload).
1. Register the handler in the manifest
Anchor link toAdd a meta-data entry to <application> with the full class name of your handler:
<application ...> <meta-data android:name="com.pushwoosh.cordova.CALL_EVENT_HANDLER" android:value="com.example.app.MyCallEventHandler" /></application>Step 3 shows where this entry goes in Cordova and in Capacitor.
2. Write the handler
Anchor link toThis handler opens your call screen on Answer and sends each call event as a broadcast inside the app:
public class MyCallEventHandler implements CallEventListener { public static final String ACTION_CALL_EVENT = "com.example.app.CALL_EVENT";
/** * Called when the user answers the call. * Opens the app on the call screen and broadcasts the event to the rest of the app. */ @Override public void onAnswer(@NonNull PushwooshVoIPMessage message, int videoState) { Context context = MyApp.context(); // your application Context
Intent intent = new Intent(context, VideoCallActivity.class) .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP) .putExtra("callId", message.getCallId()); context.startActivity(intent);
broadcast("answer", message); }
@Override public void onReject(@NonNull PushwooshVoIPMessage message) { broadcast("reject", message); }
@Override public void onDisconnect(@NonNull PushwooshVoIPMessage message) { broadcast("hangup", message); }
@Override public void onCallCancelled(@NonNull PushwooshVoIPMessage message) { broadcast("cancelled", message); }
@Override public void onCreateIncomingConnection(@Nullable Bundle payload) {} @Override public void onCallAdded(@NonNull PushwooshVoIPMessage message) {} @Override public void onCallRemoved(@NonNull PushwooshVoIPMessage message) {} @Override public void onCallCancellationFailed(@Nullable String callId, @NonNull String reason) {}
private static void broadcast(String event, PushwooshVoIPMessage message) { Context context = MyApp.context(); // your application Context Intent intent = new Intent(ACTION_CALL_EVENT) .setPackage(context.getPackageName()) .putExtra("event", event) .putExtra("callId", message.getCallId()); context.sendBroadcast(intent); }}3. Add the code to your project
Anchor link toCapacitor
Anchor link toAdd the handler class to android/app/src/main/java/ and the meta-data entry from Step 1 to android/app/src/main/AndroidManifest.xml.
Add the Pushwoosh calls SDK to the app module in android/app/build.gradle. Use the version set in the plugin’s src/android/add-android-voip.gradle:
dependencies { compileOnly "com.pushwoosh:pushwoosh-calls:6.17.1"}Without it, the build fails with package com.pushwoosh.calls.listener does not exist, because the plugin adds the calls SDK only to its own module.
Cordova
Anchor link toCordova regenerates the native project, so add the handler through a local plugin. Its plugin.xml copies the class and adds the meta-data to the manifest:
<platform name="android"> <source-file src="src/android/MyCallEventHandler.java" target-dir="src/com/example/app" />
<config-file target="AndroidManifest.xml" parent="/manifest/application"> <meta-data android:name="com.pushwoosh.cordova.CALL_EVENT_HANDLER" android:value="com.example.app.MyCallEventHandler" /> </config-file></platform>Cordova doesn’t need an extra Gradle dependency.