How to implement background service in your FlutterFlow (Especially for iOS Application)?
As we know, FlutterFlow is a visual development platform designed for building Flutter apps, does not natively offer features or plugins for implementing background services on both Android and iOS. And it focuses on simplifying the app development process.
However, you can achieve background service in your FlutterFlow app by using Flutter plugins within your FlutterFlow project. We have many Flutter plugins like background_fetch, flutter_background_service, to handle background tasks. You can easily achieve this service for android. But for iOS it is a bit complicated.
In my recent project which involved periodic background tasks, specifically fetching location data and sending it to an API, I have done much research on how to implement this in flutterflow but I didn’t get any solution. After many research i got one solution which i’m going to share here.
This approach proved successful, especially when faced with the specific requirement of fetching location data in the background. Notably, other packages did not offer the desired level of compatibility or support within the FlutterFlow context. It’s important to note that the implementation of background functionality, particularly on iOS, may pose challenges and may not be as straightforward.
In conclusion, the combination of the geolocator and flutter_background_service packages offer a viable solution for implementing background functionality within a FlutterFlow project. It is recommended to closely adhere to the outlined steps and continually follow the below steps:
Step 1: Download Project Code
Download your FlutterFlow project code and open it in a text editor, such as Visual Studio Code.
Step 2: Implement Background Service
Refer to the Flutter documentation for background tasks and specifically, utilize the flutter_background_service package. Detailed information can be found on the official package page: https://pub.dev/packages/flutter_background_service.
Step 3: Configure Dependencies
To enable background service functionality on iOS, add the following dependencies to your pubspec.yaml file. Ensure you are using the latest versions:
dependencies:
flutter_background_service: ^5.0.5
geolocator: ^10.1.0
http: ^0.13.5
device_info_plus: ^9.1.1
flutter_local_notifications: ^16.3.0 # Optional for notification functionality
permission_handler: 10.0.0
Run flutter pub get to fetch the latest dependencies.
Step 4: Fetch Location Code
Write code to fetch the device’s location. Refer to the geolocator package documentation for guidance: https://pub.dev/packages/geolocator.
Step 5: Background Service Code
Implement the background service code using the flutter_background_service package. Follow the documentation provided here: https://pub.dev/packages/flutter_background_service.
Step 6: Combine Location and Background Service Code
Ensure that you consolidate the code for fetching location and the background service within a single file.
Step 7: Integrate Background Service
Call your background service method from within your main widget file to initiate the background service. This ensures that the background tasks are triggered appropriately.
Now we have to do some configuration for ios.
Inside the Info.plist file, add the necessary permissions for location access and background operation:
<key>NSLocationWhenInUseUsageDescription</key>
Recommended by LinkedIn
<string>Your App need access to your location</string>
<key>NSLocationUsageDescription</key>
<string>your app needs access to your location when in use</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>Your app needs access to your location even when not in use. </string>
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>This app needs access to your location even when not in use. </string>
<key>NSLocationTemporaryUsageDescriptionDictionary</key>
<string>Your app needs temporary access to full accuracy location information</string>
<key>UIBackgroundModes</key>
<array>
<string>location</string>
<string>fetch</string>
<array>
<key>BGTaskSchedulerPermittedIdentifiers</key>
<array>
<string>com.Example.background.refresh</string>
</array>
3. Now open AppDelegate.swift file in your vs code and add these lines
import flutter_background_service_ios // add this
SwiftFlutterBackgroundServicePlugin.taskIdentifier =”com.Example.background.refresh”
(Make sure to add the same task identifire in your info.plist file under “BGTaskSchedulerPermittedIdentifiers” key.
4. Whatever permission you have given in your info.plist same permission should be there in your Podfile also.
Turning on the Background Modes capability
That’s it… Now you can build your app to TestFlight from Xcode.
Have you faced this similar issues? Please share in comment..