admin管理员组文章数量:1356588
Recently, I came across a situation where I needed to save the user's current device location every hour. This needs to happen while the app is completely closed (not just minimized or running in the background).
So far, I haven't found any solution capable of achieving this with Flutter.
I even tried using the silent push concept from Firebase Messaging to trigger a message periodically, wake up the app, and save the location.
Example:
FirebaseMessaging.onBackgroundMessage(firebaseMessagingBackgroundHandler);
@pragma('vm:entry-point')
Future<void> firebaseMessagingBackgroundHandler(RemoteMessage message) async {
try {
await getAndSaveLocation();
} catch (e) {
log(e.toString());
}
}
However, no Flutter package was able to fetch the location while the app was closed (both on Android and iOS).
I also tried using the WorkManager package but had no success.
Is there any solution for this scenario?
I tested the following packages to try to fetch the location:
location, geolocator, and background_location.
Recently, I came across a situation where I needed to save the user's current device location every hour. This needs to happen while the app is completely closed (not just minimized or running in the background).
So far, I haven't found any solution capable of achieving this with Flutter.
I even tried using the silent push concept from Firebase Messaging to trigger a message periodically, wake up the app, and save the location.
Example:
FirebaseMessaging.onBackgroundMessage(firebaseMessagingBackgroundHandler);
@pragma('vm:entry-point')
Future<void> firebaseMessagingBackgroundHandler(RemoteMessage message) async {
try {
await getAndSaveLocation();
} catch (e) {
log(e.toString());
}
}
However, no Flutter package was able to fetch the location while the app was closed (both on Android and iOS).
I also tried using the WorkManager package but had no success.
Is there any solution for this scenario?
I tested the following packages to try to fetch the location:
location, geolocator, and background_location.
- If the app is completely quit, I believe you'll need to wake it with a notification that the user must acknowledge. Or, after a bit of googling, you might at least get updates from properly set geofencing. – Randal Schwartz Commented Mar 27 at 21:34
- @RandalSchwartz Is there any way to collect the location without opening the app and without user interaction? That is my goal at the moment. – GoedertDalmolin Commented Mar 28 at 1:21
- have you tried this, might be useful pub.dev/packages/background_fetch @GoedertDalmolin – Abhijith Commented Mar 28 at 5:41
- @Abhijith I've already tested this package (background_fetch) alongside the geolocator package to obtain location. The issue isn't with generating background tasks. The issue is finding a package that can get the location with the app closed. – GoedertDalmolin Commented Mar 28 at 13:33
1 Answer
Reset to default 0You can check out the package flutter_background_geolocation
Also, don't fet to get the
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION"/>
permission.
本文标签:
版权声明:本文标题:background - How to fetch and save the device location periodically while the Flutter app is closed - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744067622a2585295.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论