Skip to content

About

Native incoming calls for HAINIVA agent apps on React Native / Expo: Android call notifications over the lock screen, iPhone PushKit + CallKit, with LiveKit audio.

Topics

Resources

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

@hainiva/react-native-call

Native incoming calls for HAINIVA agent apps built with React Native / Expo — the module the HAINIVA Agent app itself rings with, for partners who build their own agent app on HAINIVA.

  • Android: the system call notification (CallStyle) with a full-screen intent — the call rings over the lock screen with Answer / Decline, the ringtone loops until it is answered, declined, cancelled or expires — and a microphone foreground service for the length of a call.
  • iPhone: PushKit VoIP pushes reported to CallKit from native code at launch, before JavaScript runs (iOS requires it for every VoIP push), and the audio session handed to LiveKit's WebRTC when CallKit activates it.

The call itself (accepting it with HAINIVA, joining the LiveKit room) is done by your app — with @hainiva/agent-sdk or your own code. This module only rings and reports what the user did.

Install

Requires Expo SDK 53+ with a development build (not Expo Go) and LiveKit's React Native WebRTC:

npx expo install @hainiva/react-native-call @livekit/react-native @livekit/react-native-webrtc

Add the config plugin to app.json / app.config.ts, then npx expo prebuild:

{
  "expo": {
    "plugins": [
      ["@hainiva/react-native-call", {
        "appName": "Acme Agent",
        "android": { "unknownCallerLabel": "Incoming call", "ringtone": "acme_ring" },
        "ios": { "unknownCallerLabel": "Customer", "ringtoneSound": "acme_ring.caf" }
      }]
    ],
    "ios": { "infoPlist": { "UIBackgroundModes": ["audio", "voip", "remote-notification"] } }
  }
}

The module is found by Expo autolinking; there is nothing to register by hand.

Options

All optional. An unknown or misspelt key fails the prebuild rather than silently ringing with the defaults.

Option Platform Default
appName both the app's own name — shown with the call's line, and alone for a push this phone must not show
android.unknownCallerLabel Android Incoming call
android.incomingChannelId / incomingChannelName / incomingChannelDescription Android hainiva_calls / Incoming calls / Rings when a call comes in
android.ongoingChannelId / ongoingChannelName / ongoingCallTitle Android hainiva_in_call / Ongoing call / Ongoing call
android.ringtone Android the phone's ringtone; else the name of a sound in your res/raw (no extension)
android.smallIcon Android the system call icon; else a drawable name
android.storeName Android hainiva_store (SharedPreferences file of storeGet/storeSet)
ios.unknownCallerLabel iPhone Unknown caller
ios.ringtoneSound iPhone the system ringtone; else a sound file in the app bundle
ios.iconTemplateImage iPhone none; else an image in your asset catalog (CallKit button)
ios.includesCallsInRecents iPhone false
ios.storagePrefix / ios.voipTokenKey iPhone hc. / hainiva.voipToken (UserDefaults keys)

Android fixes a channel's sound when the channel is first created: to change the ringtone for phones that already rang, also choose a new incomingChannelId.

Usage

import { Platform } from "react-native";
import { HainivaCall, onCallAction, onCallPush, onVoipToken, getVoipToken, setRingTenant } from "@hainiva/react-native-call";

// After sign-in: the organisation this phone rings for (the tenantId you signed
// in with; clear it with setRingTenant(null) on sign-out).
setRingTenant(TENANT_ID);

// Android: a data push (e.g. @react-native-firebase/messaging background handler).
async function onRingPush(data: Record<string, string>) {
  if (data.type === "call") HainivaCall?.showIncomingCall(data.interactionId, data.caller, data.line, Number(data.expiresAt));
  if (data.type === "call_cancel") HainivaCall?.cancelIncomingCall(data.interactionId);
}

// iPhone: register the PushKit token with HAINIVA; CallKit already shows each ring.
if (Platform.OS === "ios") {
  const register = (token: string) =>
    agent.push.register({ platform: "ios", token, kind: "voip", bundleId: "com.example.agentapp" });
  const token = getVoipToken();          // known already (a previous launch)
  if (token) void register(token);
  onVoipToken((t) => void register(t));  // new or changed token
  onCallPush((data) => rememberRing(data));
}

// What the user did on the ring (also on a cold start: drain the pending one).
const pending = HainivaCall?.takePendingAction();
if (pending) handle(pending);
const stop = onCallAction(handle);

function handle(a: { action: "answer" | "open" | "decline" | "end"; id: string }) {
  if (a.action === "answer") {
    // accept with HAINIVA, join LiveKit, then keep the mic alive with the screen off:
    HainivaCall?.startCallService(callerName);
  }
  if (a.action === "end") hangUp();
}

// When the call is over:
HainivaCall?.stopCallService();
HainivaCall?.setShowWhenLocked(false);

Rings through your own push credentials

HAINIVA pushes rings to your app with your Firebase service account and APNs key, uploaded on the agent app (Organization → Developer → Agent apps, or PUT /v1/agent-apps/:id/push-credentials), and to the devices your app registered with @hainiva/agent-sdk's push.register({ platform, token, kind: "voip", bundleId }) (the bundle id must be one of the agent app's allowedBundleIds). The payload is the one above, all strings: type (call / call_cancel), tenantId, interactionId, conversationId, path, caller, line, expiresAt (Unix milliseconds), and transferMode for transfers. iPhone VoIP pushes go to <bundle id>.voip; iOS needs a CallKit call for every one, so ring ends come over the SDK's socket instead of a push.

Android 14+: check HainivaCall.canUseFullScreenIntent() and send the user to openFullScreenIntentSettings() when it is false, or the ring cannot show over the lock screen.

Documentation

Security

  • The module writes nothing to the device log — caller names and numbers never reach logcat or a sysdiagnose. CI checks the native sources for it.
  • On iPhone a push for any tenant other than the one set with setRingTenant is shown as appName alone and ended at once — a phone handed to someone else never shows the previous account's callers.
  • storeGet / storeSet are a plain app-private store (SharedPreferences / UserDefaults), not encrypted. Keep tokens in expo-secure-store (Keychain / Android Keystore); never put a refresh token here. Turn off Android backup (android.allowBackup: false) so app data is not copied off the phone.
  • Ring pushes should carry as little as possible: HAINIVA sends the caller's display name and the line only.

See SECURITY.md to report a vulnerability.

License

MIT

About

Native incoming calls for HAINIVA agent apps on React Native / Expo: Android call notifications over the lock screen, iPhone PushKit + CallKit, with LiveKit audio.

Topics

Resources

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages