rekabhq / background_locator

A Flutter plugin for updating location in background.
MIT License
288 stars 328 forks source link

background locator service does not run automatically in background after termination. when the app terminated from recent list of android, next time the background locator does not start fetching location. #236

Open EmadBahreini opened 3 years ago

EmadBahreini commented 3 years ago

Here is my location service class :

import 'dart:async'; import 'dart:isolate'; import 'dart:ui'; import 'package:background_locator/background_locator.dart'; import 'package:background_locator/location_dto.dart'; import 'package:background_locator/settings/android_settings.dart'; import 'package:background_locator/settings/ios_settings.dart'; import 'package:background_locator/settings/locator_settings.dart'; import 'package:flutter/material.dart'; import 'package:delivery/utils/LocationServiceHelper/LocationServiceRepository.dart'; import 'package:delivery/utils/LoggerHelper.dart'; import 'package:location_permissions/location_permissions.dart'; import 'LocationCallBackHandler.dart';

class LocationServiceHelper { static final LocationServiceHelper _instance = LocationServiceHelper._init(); factory LocationServiceHelper() => _instance; LocationServiceHelper._init();

late LocationDto _lastLocation; LocationDto get lastLocaion => _lastLocation; void updateLastLocation(LocationDto? location) { if (location != null) _lastLocation = location; }

ReceivePort port = ReceivePort(); bool initialized = false; void init() async { LoggerHelper.logger.wtf('start location service helper'); if (IsolateNameServer.lookupPortByName( LocationServiceRepository.isolateName) != null) { IsolateNameServer.removePortNameMapping( LocationServiceRepository.isolateName); }

IsolateNameServer.registerPortWithName( port.sendPort, LocationServiceRepository.isolateName);

port.listen( (dynamic data) async { await reciveCallBack(data); }, ); print('Initializing...'); await BackgroundLocator.initialize();

print('Initialization done'); final _isRunning = await BackgroundLocator.isServiceRunning();

print('Running ${_isRunning.toString()}'); }

Future reciveCallBack(LocationDto? data) async { try { await _updateNotificationText(data); //! SEND DATA TO API // await AgentApiProvider.sendLocation(data); updateLastLocation(data); if (data != null) LoggerHelper.logger .wtf(data.altitude.toString() + " , " + data.longitude.toString()); // MapBloc() // ..markMe( // LatLng(data.latitude, data.longitude), // );

await _updateNotificationText(data); } catch (e, s) { LoggerHelper.errorLog(e, s); } }

Future _updateNotificationText(LocationDto? data) async { if (data == null) { return; }

await BackgroundLocator.updateNotificationText( title: "status:online", msg: "${DateTime.now()}", bigMsg: "click to open application"); }

void stop() async { try { BackgroundLocator.unRegisterLocationUpdate(); // print(IsolateNameServer.removePortNameMapping( // LocationServiceRepository.isolateName)); } catch (e, s) { LoggerHelper.errorLog(e, s); } }

Future _checkLocationPermission() async { final access = await LocationPermissions().checkPermissionStatus(); switch (access) { case PermissionStatus.unknown: case PermissionStatus.denied: case PermissionStatus.restricted: final permission = await LocationPermissions().requestPermissions( permissionLevel: LocationPermissionLevel.locationAlways, ); if (permission == PermissionStatus.granted) { return true; } else { return false; } case PermissionStatus.granted: return true; default: return false; } }

Future start() async { if (await _checkLocationPermission()) BackgroundLocator.registerLocationUpdate( LocationCallbackHandler.callback, iosSettings: IOSSettings( accuracy: LocationAccuracy.NAVIGATION, distanceFilter: 0), autoStop: false, androidSettings: AndroidSettings( accuracy: LocationAccuracy.NAVIGATION, interval: 15, distanceFilter: 0, client: LocationClient.google, androidNotificationSettings: AndroidNotificationSettings( notificationChannelName: 'Location tracking', notificationTitle: 'get your location', notificationMsg: ''get your location', notificationBigMsg: 'something to show...', notificationIconColor: Colors.grey, notificationTapCallback: LocationCallbackHandler.notificationCallback), ), ); final _isRunning = await BackgroundLocator.isServiceRunning(); print('Running ${_isRunning.toString()}'); } }

Alexminator99 commented 3 years ago

You right. Im having the same problem...

mehdok commented 3 years ago

Hi @EmadBahreini Thank you for opening an issue and your patient;

I marked this as a bug, and I believe it is related to recognizing whether the service is running or not, I'll take a deep look into it this weekend;

shahmirzali49 commented 2 years ago

@mehdok any updates ?