【问题标题】:How to send user location to Firestore even when app is killed in flutter?即使应用程序在颤动中被杀死,如何将用户位置发送到 Firestore?
【发布时间】:2021-09-25 18:19:34
【问题描述】:

即使应用程序被终止,我也在尝试将用户的位置发送到 Firestore。当应用程序处于前台或后台时,我能够成功地将数据发送到 Firestore,但在应用程序被终止时无法发送数据。谁能解释一下可能是什么问题?

我正在使用

background_locator: ^1.6.4

颤动

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 SDK version: 2.13.4 (stable) (Wed Jun 23 13:08:41 2021 +0200) on "linux_x64"

收到数据后我尝试执行的这段代码

receiverPort.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');
        }
      },
    );

如果我需要分享其他任何内容,请告诉我。谢谢

【问题讨论】:

    标签: firebase flutter dart google-cloud-firestore


    【解决方案1】:

    当应用程序的进程被杀死时,它的所有代码都会停止执行。这包括任何 Firestore 侦听器。操作系统这样做是为了防止应用在用户不使用时意外烧毁电池和数据。这是一项旨在使最终用户受益的功能,您的应用应尊重这一点。

    另见:

    【讨论】:

      【解决方案2】:

      我发现了问题。我需要在回调中而不是在ReceivePort 中添加 Firestore 代码。 这是工作代码:

      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)));
        }
      
      Future<void> callback(LocationDto locationDto) async {
         // Your call back code goes here
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-08-02
        • 2016-04-04
        • 2023-03-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-07-10
        相关资源
        最近更新 更多