Closed Piero512 closed 3 years ago
Hi @Piero512 Thank you for opening an issue;
No, Wiki is not up to date. Right now, you don't need to do anything on Android, But follow the wiki for iOS.
I am building an application where I need to send data to Firestore in the background even when the app is killed. When the app is in the background or foreground data is sent to Firestore but when the app is killed data did not send to Firestore. Can you tell me what could be the issue?
I'm using
background_locator: ^1.6.4
Flutter
Flutter 2.2.3 • channel stable • https://github.com/flutter/flutter.git
Framework • revision f4abaa0735 (2 weeks ago) • 2021-07-01 12:46:11 -0700
Engine • revision 241c87ad80
Tools • Dart 2.13.4
Dart
Dart SDK version: 2.13.4 (stable) (Wed Jun 23 13:08:41 2021 +0200) on "linux_x64"
This is the code I'm trying to execute when data is received
port.listen(
(dynamic data) async {
if (data != null) {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
final LocationDto position = data as LocationDto;
print('data: $position');
final Member member = await FirebaseService().getCurrentUserProfile();
member.currentPosition = '${position.latitude},${position.longitude}';
await FirebaseService()
.updateData(
FirebaseService.memberRef,
FirebaseService.memberChildId,
FirebaseService.getCurrentUserId(),
member.toJson())
.then((value) {});
} else {
print('data is null');
}
},
);
I found the problem. I need to add Firestore code in the callback to run even when the app is killed.
Future<void> _startLocator() async {
final Map<String, dynamic> data = {'countInit': 1};
return BackgroundLocator.registerLocationUpdate(
LocationCallbackHandler.callback, // <-- You need to add your callback code here
initCallback: LocationCallbackHandler.initCallback,
initDataCallback: data,
disposeCallback: LocationCallbackHandler.disposeCallback,
iosSettings: const IOSSettings(
accuracy: location_settings.LocationAccuracy.BALANCED,),
androidSettings: AndroidSettings(
accuracy: location_settings.LocationAccuracy.BALANCED,
interval: 5,
androidNotificationSettings: AndroidNotificationSettings(
notificationChannelName: 'Location tracking',
notificationTitle:
location.isNotEmpty ? location : 'Start Location Tracking',
notificationMsg: 'Track location in background',
notificationBigMsg:
'Background location is on to keep the app up-to-date with your location. This is required for main features to work properly when the app is not running.',
notificationIconColor: Colors.grey,
notificationTapCallback:
LocationCallbackHandler.notificationCallback)));
}
@zeeshanahmad0201 Glad to see you found the solution;
I just like to point that port.listen
is just for updating the UI
and won't be called when your app is in the background;
Every action that needs to be perform in the background should be inside the callback
method;
Hi.
I am looking into using some flutter plugins in the callback, but the example app in this repo has none of the things used for additional plugin support that the wiki indicates.
Last I remember, Flutter background callbacks had access to all native services in the V2 embedding, right?
What would be the updated recommendations for this?