npx expo install @notifee/react-native
Keeping The Call Alive In Background
One of the crucial functionalities of a video or audio calling application is to keep the call alive in the background. On this page, we focus on what must be added to your app to support this. After enabling, the user of your app will notice that the call is kept alive even if the app goes to the background as they will still hear the remote audio streams while the app is kept in the background.
Android Setup
In Android, we use a foreground service to keep the call alive. The SDK will automatically create and manage the foreground service. The first step is to install the Notifee
library so that SDK can handle a foreground service. To install the Notifee
library, run the following command in your terminal of choice:
Notifee version 9 or above is required to handle foreground service permissions for the calls.
The next step is, in order to be able to use the foreground service, some declarations need to be added the AndroidManifest.xml
. In app.json, in the plugins
field, add true to the androidKeepCallAlive
property in the @stream-io/video-react-native-sdk
plugin. This will add the declarations automatically.
{
"plugins": [
[
"@stream-io/video-react-native-sdk",
{
"androidKeepCallAlive": true
}
],
// your other plugins
]
}
If Expo EAS build is not used, please do npx expo prebuild --clean
to edit the AndroidManifest.xml
again after adding the config plugin property.
yarn add @notifee/react-native
npx pod-install
The next step is, in order to be able to use the foreground service, some declarations need to be added in the AndroidManifest.xml
:
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<!-- We declare the permissions needed for using foreground service to keep call alive -->
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_CAMERA" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MICROPHONE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_CONNECTED_DEVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC" />
<service
android:name="app.notifee.core.ForegroundService"
tools:replace="android:foregroundServiceType"
android:stopWithTask="true"
android:foregroundServiceType="shortService|dataSync|camera|microphone|connectedDevice" />
When uploading the app to the Play Store, it is essential to declare the permissions for foreground services in the Play Console and provide an explanation for their use. This includes adding a link to a video that demonstrates how the foreground service is utilized during video and audio calls. This procedure is required only once. For more details, click here. The added permissions are:
android.permission.FOREGROUND_SERVICE_CAMERA
- To access camera when app goes to backgroundandroid.permission.FOREGROUND_SERVICE_MICROPHONE
- To access microphone when app goes to backgroundandroid.permission.FOREGROUND_SERVICE_CONNECTED_DEVICE
- To access bluetooth headsets when app goes to backgroundandroid.permission.FOREGROUND_SERVICE_DATA_SYNC
- To keep video/audio calls alive when both camera and microphone access permissions were not granted
Optional: override the default configuration of the foreground service notifications
import { StreamVideoRN } from "@stream-io/video-react-native-sdk";
import { AndroidImportance } from "@notifee/react-native";
StreamVideoRN.updateConfig({
foregroundService: {
android: {
// see https://notifee.app/react-native/reference/nativeandroidchannel
// for the various properties that can be used
channel: {
id: "stream_call_foreground_service",
name: "Service to keep call alive",
lights: false,
vibration: false,
importance: AndroidImportance.DEFAULT,
},
// you can edit the title and body of the notification here
notificationTexts: {
title: "Video call is in progress",
body: "Tap to return to the call",
},
},
},
});
iOS Setup
The way to keep audio alive in the background is to enable the audio
background mode. When you enable this capability, your app’s audio playback will continue to play when users lock their iOS device or switch to another app. In Xcode: Open the Info.plist
file and add audio
in UIBackgroundModes
. By editing this file with a text editor, you should see:
<key>UIBackgroundModes</key>
<array>
<string>audio</string>
</array>