Native incoming calls in Flutter with CallKit, PushKit and Agora
How we built 1-to-1 voice calls in a Flutter app: Agora audio, native ringing on Android, PushKit and CallKit on iOS, no double rings and metered minutes.
- Written by
- DevTech365 Engineering
- Published
- Reading time
- 7 minutes
Paira is a Flutter app we have been building since May 2026, with BLoC for state management. Members who match can call each other: one to one, audio only, over Agora. The requirement was easy to state and hard to meet. An incoming call must ring like a phone call, full screen and on the lock screen, even when the app is closed.
Getting audio to flow is the easy part. The work is in waking a closed app, satisfying two very different operating systems, never ringing twice and cleaning up after every call. This is how we built native incoming calls in Flutter with CallKit, PushKit and Agora, and what we test.
Four transports, one call
A Paira call uses four channels, each with one job:
| Transport | Job | Android | iOS |
|---|---|---|---|
| REST API | Start, answer, reject, cancel and end calls | Shared | Shared |
| SignalR | Call events while the app is open | Shared | Shared |
| Push | Wake the app and ring | High-priority FCM data message | APNs VoIP push through PushKit |
| Agora | Carry the audio | Agora SDK | Agora SDK, audio session owned by CallKit |
The backend owns call state and mints a fresh Agora token for each call. If nobody answers, the server marks the call missed after about 35 seconds and notifies both sides. On the device, Agora's media events decide whether the other person has really joined or left, while server events drive the call's lifecycle.
One state machine owns every call
All of those inputs land in one place: an app-level CallCubit, a state machine created at startup so a call can arrive on any screen. Its phases are idle, outgoing ringing, incoming ringing, connecting, connected and ended. The Agora engine, the native call UI and the real-time connection each sit behind a small interface with a no-op implementation, so the whole machine runs in tests without native code.
Four invariants do most of the work:
- Ring exactly once. Every ring trigger is keyed on the call ID, so the same call arriving over SignalR and a push rings one time.
- One call at a time. An incoming call while another is in flight is rejected as busy. An outgoing call claims the busy state before its first
await, so a call that arrives during the microphone prompt is rejected instead of racing it. - Always clean up. Every terminal state leaves the Agora channel, stops timers and dismisses the native UI, and each step is safe to repeat.
- Explain the ending. The screen briefly shows Declined, Missed or the call length before it closes.
// Simplified: every ring trigger, from any transport, goes through one guard
void onIncoming(CallEvent event) {
if (!_seenCallIds.add(event.callId)) return; // already ringing or handled
if (_isBusy) {
_calls.reject(event.callId); // busy: one call at a time
return;
}
_nativeUi.showIncoming(event);
emit(state.copyWith(phase: CallPhase.incomingRinging, call: event));
}
Android: a data message wakes a closed app
On Android, the backend sends a high-priority FCM data message to the callee. Even when the app is closed, Firebase runs the app's background handler, which shows a full-screen ringer through the flutter_callkit_incoming plugin, a CallKit-style native call UI for Android. The manifest needs full-screen intent, microphone foreground service and call management permissions, plus an activity allowed to show over the lock screen and turn the screen on.
Three bugs taught us what to test:
- Answering ended the call. Dismissing the native ringer after an accept made the plugin emit a call-ended event, which the state machine read as a hang-up. The app now marks calls it ends itself and consumes that echo exactly once.
- Cold-start accepts went nowhere. With the app closed, the accept event fired before Dart subscribed, the background handler's state was invisible to the main app, and an accept is not a notification tap. On startup the app now asks the plugin for active calls and resumes the accepted one through the normal answer path.
- The callee had no microphone. Android generally will not show a permission dialog over the lock screen, so a call answered from the ringer joined without mic access. The app now asks once after sign-in, with a short explanation before the system dialog, and keeps the at-call request as a fallback.
iOS: PushKit reports to CallKit in Swift
iOS needs a different transport. A data push does not launch a terminated iOS app, and an alert push only runs code when the user taps it. The only way to ring a closed app is an APNs VoIP push delivered through PushKit, and since iOS 13 every VoIP push must be reported to CallKit as an incoming call before the push handler finishes, or iOS terminates the app.
That deadline is why the ring path is native. AppDelegate holds a PKPushRegistry for the life of the process, and the incoming-push callback reports the call to CallKit in Swift before any Dart code runs. The CallKit configuration is deliberately narrow: audio only, one call at a time, no hold or grouping, calls listed in Recents, and CallKit configuring the voice-chat audio session. When the user accepts, the app resumes the call through the same Dart path Android uses for a cold start.
The push is the doorbell; Agora is the conversation. The VoIP payload carries only the call ID, caller name and photo, Agora channel and conversation ID, with the same keys as the FCM payload, so the backend serializes one shape. No Agora token travels in a push. The app fetches it from the answer endpoint after the user accepts, exactly as on Android.
The backend contract has three rules:
- Tokens are per device. VoIP tokens cannot be addressed by topic, so the app registers its token at login, startup and session refresh, unregisters at logout, and the backend pushes to every iOS device the callee has. Registration ignores the notifications toggle on purpose: muting notifications is not a request to stop receiving calls.
- Send VoIP directly through APNs. Firebase's Admin SDK cannot send VoIP pushes. Expiration is zero, so a stale ring is never delivered late.
- Only valid calls. Every VoIP push carries a UUID call ID, because CallKit rejects anything else and iOS punishes a push that does not end in a reported call.
No double rings
With both transports live, an iOS device in the foreground can receive an FCM ring and a VoIP push for the same call, and asking CallKit to show one call twice is not a safe operation. The backend rule is that FCM never rings iOS. The app enforces it too, because the client cannot control when a backend change rolls out. The routing decision is a pure function of the payload and the platform, so it is unit tested without platform channels. Cancel and missed-call dismissals still travel over FCM on both platforms, because a redundant dismissal is harmless.
Inside the app, the call-ID guard catches the remaining overlap between SignalR and push, and an active-call flag prevents ringing for a call already on screen. One more trap: FCM payloads carry enum values in PascalCase while REST and SignalR use camelCase, so every parser is case-insensitive.
Metering minutes by subscription
Call minutes are metered on the server against the member's subscription. A usage endpoint returns the remaining minutes and messages for the active plan, and a missing value means unlimited, which must never be read as zero.
The app keeps one shared usage state. It loads when a session starts and refreshes when a chat opens, after every call ends and after a payment. Each chat shows a compact allowance strip under the header, with an amber warning at five minutes or fewer and a locked state with an upgrade prompt at zero. The real check runs at the top of the state machine's outgoing-call method, before the busy claim, the microphone prompt and the initiate request, so a blocked attempt has no side effects. It runs again after the permission prompt returns. If the allowance cannot be verified, the call button stays disabled with a retry option: the guard fails closed.
A client can only enforce so much. It can block a known zero balance, but it cannot stop a race between two devices on one account. Account-wide enforcement belongs in the server's initiate endpoint. The client guard is the friendly early answer, not the security boundary.
What to test
Automated tests carry most of the load, and none of them need a device:
- Parsers for both enum casings, unknown values and malformed payloads.
- Every state machine transition with fake seams, including busy rejection, the echo after a programmatic end, cold-start resume and cleanup on every terminal state.
- The platform routing function: rings suppressed on iOS, unchanged on Android.
- VoIP token registration: an unchanged token is not sent again, and a failed request never blocks login.
- The usage guard: zero minutes, no plan and an unverified balance never reach the microphone prompt or the initiate request.
Native behavior still needs real phones, so the test plan includes a manual matrix: foreground, background, locked and terminated states for accept, decline, cancel and timeout across two devices, plus audio in both directions, mute and speaker, and the denied-permission path. On iOS it adds audio routing through CallKit and the call's entry in Recents.
Takeaways
- Route every transport into one state machine and key everything on the call ID. Duplicates are normal, not exceptional.
- On iOS, ring in native code and report to CallKit before anything else runs.
- Ring first and fetch media credentials after the user accepts.
- Treat client-side metering as UX, and enforce limits on the server.
- Test the cold start, the lock screen and the echo, not just the happy path.
Calling touches the app, the backend and two push providers at once, so we plan it as joint mobile app development and API and backend development work rather than a plugin to drop in.
- Flutter
- VoIP
- CallKit
DevTech365 Engineering
Software engineering team
The engineers who build DevTech365's mobile apps, web platforms, APIs and AI systems, writing about the decisions behind production software.
