Skip to main content
Version: v4

Prepare your app for background updates

Image

In iOS, when the user enters in the background state, real time connection breaks with the server. So, if the user comes inside the chat window or in the conversation list then it won't update the new messages received from the real time server until it's being fetched from the API.

To keep user interaction alive with the application you can add our service which keeps real time connection alive in a background state for a few seconds. If the user is constantly switching between multiple applications at the same time, this service will help to update real time updates on the UI.

Background updates behaviour

This below table is applicable only when these steps are followed.

autoEstablishSocketConnection usage can be found in Managing Web-Socket connections manually.

ValueBehaviour
autoEstablishSocketConnectiontrueConnection is maintained in background for 30 seconds and after that it is killed.
autoEstablishSocketConnectionfalse1. If CometChat.connect() is called and app goes to background the connection is maintained for 30 seconds and after that connection is terminated.
2. If CometChat.disconnect() is called and app goes to background the connection not maintained.

Please follow the steps to prepare your app for background states.


Step 1. Add Background Modes in Capabilities

Click on Targets -> Project -> [+ Capability] -> Add 'Background Modes' section.

Image

Step 2. Select Background modes for processing

Select 'Background Fetch' and 'Background Processing' options by ticking them.

Image

Step 3. Add Code in Application class to configure background services for different states.

Add CometChat.configureServicesmethod in application class or scene delegate class as shown below.

func applicationWillResignActive(_ application: UIApplication) {
CometChat.configureServices(.willResignActive)
}

func applicationDidEnterBackground(_ application: UIApplication) {
CometChat.configureServices(.didBackground)
}
note
  1. As per the iOS standard guidelines, this service will keep alive for few seconds in background services in the system. After few seconds it will get terminated automatically by the system.
  2. This service is only available after SDK v4.x.x & UI Kit v4.x.x

Step 4. Check background service is running

You can check the background service is running or not using CometChat.BackgroundTaskEnabled() method. It will return true if service is running else it will return false.

if CometChat.backgroundTaskEnabled() {
print("Background service is running, it will automatically update UI.")
}else{
print("Background service is stopped, Please refresh messages or conversations here to update.")
}