Video calling is quickly becoming a necessary feature for many mobile apps in today's competitive app market. The cross-platform capabilities of Flutter give developers access to robust video calling plugins and tools. Making video calls in the background, however, introduces an additional level of complexity. In order to ensure a seamless user experience even when the app is minimized, this blog will show you how to incorporate background video calling in Flutter using packages like Agora and CallKit.
Video call apps need to keep running even when minimized. The ongoing video call enhances the user experience and replicates the functionality of mainstream communication apps such as Zoom or WhatsApp, irrespective of whether the user presses the home button unintentionally or simply wants to switch to a different app.
Make sure you have the Flutter SDK installed and understand the basics of Flutter development before beginning the implementation.
You can use the Agora developer console or any other video calling SDK.
A functional Flutter project in the foreground that already has video calling set up
1. Choose a Reliable Video SDK Because of its extensive documentation and sophisticated support for background operations, we'll be using Agora for this guide.
2. Enable Foreground Service for Android On Android, a foreground service is required to keep video call connectivity in the background:
AndroidManifest.xml: ```xml <uses-permission android:name="android.permission.FOREGROUND_SERVICE" /> <application ...> <service android:name=".YourForegroundService" android:exported="false" android:foregroundServiceType="mediaProjection|camera|microphone" /> </application> ``` In order to adhere to Android's background execution limitations, you must additionally show a persistent notification.
3. Handle Audio and Video in Background (Android) When the call starts, launch and control your foreground service using Flutter Foreground Task or native Android code through platform channels. Make sure the camera/mic access is maintained and the video engine continues to run.
4. iOS Configuration (CallKit Integration) For a smooth experience, integrate CallKit since Apple limits background processes:
- Use the `flutter_callkit_incoming` package. - Update the `Info.plist` with: ```xml <key>UIBackgroundModes</key> <array> <string>audio</string> <string>voip</string> </array> ``` - Set up `PushKit` to manage VoIP push alerts for incoming calls.
5. Manage Lifecycle Events Maintain call state by handling app lifecycle changes with `WidgetsBindingObserver` or `flutter_lifecycle_aware`:
```dart @override void didChangeAppLifecycleState(AppLifecycleState state) { if (state == AppLifecycleState.paused) { // App is in background // Ensure video/audio stream continues } else if (state == AppLifecycleState.resumed) { // App is back in foreground } } ``` 6. Testing on Real Devices On emulators, background execution behaves differently. Test on actual devices at all times, particularly with:
Strict background policies on Android 13+ and 14
- iOS 14+
7. Optional Enhancements - Include actionable buttons (Accept/Decline) in call notifications. - For the ringtone, use `flutter_ringtone_player`. - Handle call timeouts and disconnections with grace.
- When Android 13+ is minimized, the audio stops. :Make sure the foreground service is properly launched. - The call is not kept running in the background on iOS.: CallKit and PushKit must be used. - Android 14 app crashes: In `AndroidManifest.xml`, use the correct `foregroundServiceType`.
Although it is difficult to implement, background video calling in Flutter is possible with the correct setup and tools. You can offer a dependable and seamless video calling experience by utilizing platforms such as Agora and incorporating native services like CallKit and Android foreground services. Keep up with OS-level limitations and don't forget to test on real devices.