rekabhq / background_locator

A Flutter plugin for updating location in background.
MIT License
289 stars 330 forks source link

sendLocationEvent reached, but no callback #304

Closed frankvollebregt closed 2 years ago

frankvollebregt commented 2 years ago

Hello. First of all, sorry if this is has been asked before, and thanks for the awesome package!

I set up the package similar to the example, but my Dart callback is never called (no difference if the app is running in the foreground or background).

In the console, I do see something like the following:

sendLocationEvent {callback=2369190748653017841, location={...}}

Where all logs have the same callback identifier.

Looking through the code, this seems to be caused in the IsolateHolderService.kt file. link to the line

private fun sendLocationEvent(result: HashMap<Any, Any>) {
        //https://github.com/flutter/plugins/pull/1641
        //https://github.com/flutter/flutter/issues/36059
        //https://github.com/flutter/plugins/pull/1641/commits/4358fbba3327f1fa75bc40df503ca5341fdbb77d
        // new version of flutter can not invoke method from background thread

        if (backgroundEngine != null) {
            val backgroundChannel =
                    MethodChannel(backgroundEngine?.dartExecutor?.binaryMessenger, Keys.BACKGROUND_CHANNEL_ID)
            Handler(context.mainLooper)
                    .post {
                        Log.d("plugin", "sendLocationEvent $result")
                        backgroundChannel.invokeMethod(Keys.BCM_SEND_LOCATION, result)
                    }
        }
    }

Is there any immediate reason why this might be happening?

I tried running my code on 2 real devices:

But both show the same behaviour.

The callback specified in BackgroundLocator.registerLocationUpdate is in the LocationCallbackHandler class as per the example:

class LocationCallbackHandler {
  ...

  static Future<void> callback(LocationDto locationDto) async {
    print('hello callback');
  }
}

Any help solving this is appreciated!

Version information background_locator: ^1.6.12 Flutter 2.8.1 • channel stable • https://github.com/flutter/flutter.git Framework • revision 77d935af4d (5 weeks ago) • 2021-12-16 08:37:33 -0800 Engine • revision 890a5fca2e Tools • Dart 2.15.1
nick1377 commented 2 years ago

1.6.12 is broken. You have to rollback to 1.6.6. The issue is actually caused by a null exception in the fromJson method in location_dto.dart due to the added provider field for location.

RohitRB commented 2 years ago

@nick1377 Thanks for the update. I was facing the same problem.

RohitRB commented 2 years ago

Does this plugin support sdk 31 (Android 12) ?

As seen in the build.gradle file it says compileSdkVersion 30

@mehdok @nick1377

frankvollebregt commented 2 years ago

@RohitRB I think it should, given that the most recent patch specifically mentions fixing compatibility with the Android 12 permissions.

My older test app with a 6-12 months old version of this plugin even works fine on my Android 12 Pixel 6.

RohitRB commented 2 years ago

@frankvollebregt

thanks for your feedback. But i am facing issues with this new OS called FunTouch OS running android 12, 11

RohitRB commented 2 years ago

What can be the min interval that we can go to in seconds when keeping distance filter 1 meter so that the OS doesn't ban our app and we stop receiving location updates?

this question is a bit off track but i find no other place to put it. @mehdok @atsumo @moovida @splangi @gomazaba

frankvollebregt commented 2 years ago

1.6.12 is broken. You have to rollback to 1.6.6. The issue is actually caused by a null exception in the fromJson method in location_dto.dart due to the added provider field for location.

Hi @nick1377, thanks for your answer. Unfortunately, it still won't work for me after rolling back the package version. Running the example app with 1.6.6 works, so I'll try to adapt my code to that and hope I can get it working that way.

frankvollebregt commented 2 years ago

I just ran the app with the --no-sound-null-safety flag, after which the callbacks come through as expected. This is using version 1.6.6 of the package.

frankvollebregt commented 2 years ago

You were completely right @nick1377, it was caused by the provider being null in the fromJson method. For now, I've created a fork of the repo where I set the provider to an empty String if it's missing. This seems to resolve the issue.

I'm not sure whether this is a solution that should be applied, or if it should instead be ensured that the provider is set: At any rate, if anyone wants to use this dependency instead for the time being, feel free:

dependency_overrides:
  background_locator:
    git:
      url: git@github.com:frankvollebregt/background_locator.git
      ref: location-dto-null-fix
frankvollebregt commented 2 years ago

As this issue is essentially the same as #301 I'll close this one.

daviddagan commented 2 years ago

same issue here, please fix it as soon as you can

RohitRB commented 2 years ago

Please remove my email from this email

Thanks, Rohit Rajnikant

M: 9099 20 9892 | Email: @.***

On Sun, Feb 6, 2022 at 6:07 PM daviddagan @.***> wrote:

same issue here, please fix it as soon as you can

— Reply to this email directly, view it on GitHub https://github.com/rekabhq/background_locator/issues/304#issuecomment-1030824303, or unsubscribe https://github.com/notifications/unsubscribe-auth/AEXS4ETJIV4ACFY52YSBMGTUZZTQJANCNFSM5MHRXJPA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

You are receiving this because you were mentioned.Message ID: @.***>

glofru commented 2 years ago

I fix this also for Flutter 3.0: https://github.com/glofru/background_locator