Flutter: Background tasks
In mobile application development, background tasks are vital in ensuring a seamless and interactive user experience. While users interact with the foreground of an app, background tasks quietly operate in the background, handling critical processes without interrupting the app’s primary functionality.
Whether updating data, sending push notifications, tracking user locations, or managing multimedia playback, background tasks have become a cornerstone of modern mobile applications. In this article, we will explore the importance of background tasks, their impact on user experience, and how they optimize app performance to deliver smoother and more engaging mobile app usage.
Types of tasks in a Mobile Application:
Foreground refers to the state of an application when it is actively being used and visible on the screen.
Background describes the state of an application when it is still open but not actively visible, typically because the user has switched to another app or pressed the “home” button.
Terminated refers to the state of an application when it is no longer running, either because the user has locked the device or explicitly closed the app using the app switcher.
Why do we need background services?
Background services are crucial for providing a better user experience in mobile applications. They ensure uninterrupted functionality by allowing apps to continue essential tasks and processes in the background, even when not actively used.
Some of the use cases of background tasks to enhance user experience are:
Real-time Updates: Apps can receive and process data in the background through background tasks, eliminating the need for users to refresh the app manually and ensuring that they stay up to date.
Multitasking: Users can perform other activities while the app works in the background, thanks to background tasks. This enhances their multitasking capabilities.
Content Updates: The app's content is continuously updated through background tasks, allowing users to access new information effortlessly without requiring manual action.
Resource Management: Efficient management of system resources is made possible by background tasks, which balance priorities based on user interactions and app requirements.
So, let us dive into the implementation part using the Flutter plugin for background tasks.
Add the plugin with the command:flutter pub add flutter_background_service
Please make sure your project already uses the version of gradle tools below:
- in
android/build.gradle
classpath 'com.android.tools.build:gradle:7.1.2'
- in
android/gradle/wrapper/gradle-wrapper.properties
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-all.zip
Enable the background_fetch
in your Xcode:
Now, add the identifiers(BGTaskSchedulerPermittedIdentifiers
) to your info.plist:
Search for Permitted background task scheduler identifiers
and add the identifiers.
Note: The naming convention I usually follow for the identifiers is
(
<bundle identifier>.<taskname>
) e.g.com.app.bgService.taskName
And in your ios/Runner/AppDelegate.swift
file
import UIKit
import Flutter
import flutter_background_service_ios // add this
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
/// Add following lines line
SwiftFlutterBackgroundServicePlugin.taskIdentifier = "com.app.bgService.taskName1"
SwiftFlutterBackgroundServicePlugin.taskIdentifier = "com.app.bgService.taskName2"
GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}
The setup is done!!
Now, run the app in your IDE.
Android Screenshot:
To change the icon in the notification, add a drawable icon to android/app/src/main/res/drawable
with the name ic_bg_service_small.