-
-
Notifications
You must be signed in to change notification settings - Fork 76
watchLocation method doesn't get location in background mode android #256
Comments
I had a similar problem. It is not directly related to nativescript-geolocation rather to Android itself. You have to create a foreground service in order to track geolocation while the app is in the foreground. Read more here: https://developer.android.com/about/versions/oreo/background-location-limits#tuning-behavior |
I still the same issue, i get only few coordinates after which "service onStopJob" is called |
My initial fix seems to only fix this problem on Xiaomi devices. I've tried it on pure Android (like Samsung phones) and it would stop working after few seconds. |
I think I found the solution!!! What I did was I added My final service tag looks like this:
My Hope it helps someone as this was really painful to me... 🤯 |
Background still not working for me on android, Nexus 5X API 28 emulator. I can see the logs on console: But as soon as I send it to the background, the Geolocation icon at the top of the phone is gone, and it stops logging my location. I have added the following to Android Manifest:
Any ideas of what could be missing? Thanks in advance. Edit: Background service actually seems to work fine when ran on my Samsung Galaxy tablet though. Might be something specific with the simulator... |
Confirmed on Samsung Android 10. Thank you butaminas! |
I'm working through this issue presently. The guide suggested by @Aceman18 looks pertinent, 'tho it's an Angular example and my project Javascript. So far I've not been successful in even getting a clean build, even though I've done lots of such conversions over the years. If anyone has a working version in Javascript I'd love to connect with you. [Edit] I have this working nicely now in a JavaScript project. Here's the changes I made: Create a new file, foreground-service.android.js in the /app folder:
Add the permission in AndroidManifest.xml
And define the service in AndroidManifest.xml
Resolve the path in webpack.config.js:
Set up the foreground service in app.js:
And, if you're not already including the utils plugin, add
Based on the comments in this issue, I also set compileSdkVersion in app.gradle: android { |
Referencing the above post, for webpack 5, you'll need to adjust webpack.config.js to something like this:
|
@dlcole Thank you for the code, that's very helpful - and it works. But unfortunately, this is not enough for a newbie like me. I'm unable to figure out where in your code the response to watchLocation happens. Like for everyone else, watchLocation works well when my app is in the foreground, not all when it is not. I have your code included and compiled, the foreground service starts (I have console-writes to tell me that). But then what? I need to write out the locations - where do I do that from, in your code? Related: the location needs to be tracked even if the user swipes away the app without logging out - I'm hoping the foreground service will handle that? Last question: Any idea how this would work on iOS? Sorry if all this sounds dumb! All help appreciated! |
@nmandyam Your iOS works differently, but doesn't require additional services. In Xcode under Signing and Capabilities -> Background Modes, you'll need to check "Location Updates". In your Info.plist file, you'll need the entries:
and also
Good luck! |
@dlcole thank you! |
@dlcole sorry to bother you about this again, but I'm completely lost. As it stands now:
So it starts tracking location all OK when the app is in the foreground. And it continues to track location when the app goes to the background. I see the Android notification that the foreground service is running. But when I swipe away the app, the location stops being tracked, like you said would happen. BUT: Per that article that you linked me to, it's the foreground service that makes sure that my app continues to report location when swiped away. And the Java code is nearly identical to your snippet above, so I assume the "foreground service" that the article refers to and the "foreground service" that you've helped me build are the same thing. Therefore, by implementing the foreground service, the app should continue reporting location. Yet, swipe away the app and it won't track location (just like you said it won't). What am I missing? Thanks for all your help. |
When you say, "swiped away" do you mean that you've actually terminated the app? This is a key distinction. |
Sorry, didn't know there was a distinction.
I go to the list of Recent apps, swipe to the next or right to get to my
app, swipe the app away. I don't log out, I don't terminate it from the
System screen. Does that make sense?
…On Tue, 9 Aug, 2022, 18:58 David Cole, ***@***.***> wrote:
When you say, "swiped away" do you mean that you've actually terminated
the app? This is a key distinction.
—
Reply to this email directly, view it on GitHub
<#256 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ADAOCKLW4QACWOWGGNRGJPLVYJMGRANCNFSM4JP675HQ>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
So when you open the list of Recent apps, your app is no longer there, yes? If so, this means your app is actually terminated and not just in the background. The article I linked to in a prior posts suggests how to make this scenario work, but my code only works when the app is in the foreground (normal case) or background, using the foreground service (I admit the terminology is confusing). |
@dlcole I managed to get it working with foreground service, the only problem is after some time location updates stop receiving for some reason. Do you happen to know why? let me know if you can assist |
@shradovyy I haven't see this behavior but I've not tested this code recently. How long does it take to stop receiving updates? |
It really depends. It might work for as long as I don't open any other apps and keep the screen on (do not lock the phone). As soon as I open other app it seems like android suspends the app even when foreground service is running. When I open the app again from the recent apps it kind of restarts (splash screen is shown) |
@dlcole by the way, thank you for a quick reply |
OK, I’ll test this later today. Sent from my iPhoneOn Mar 28, 2023, at 11:11 AM, shradovyy ***@***.***> wrote:
It really depends. It might work for as long as I don't open any other apps and keep the screen on (do not lock the phone). As soon as I open other app it seems like android suspends the app even when foreground service is running.
When I open the app again from the recent apps it kind of restarts (splash screen is shown)
—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you were mentioned.Message ID: ***@***.***>
|
So I tested this back on Tuesday and the Android phone continued to send it's location data. BUT, the phone may need to be moving for that to happen, depending on what you specify in your watchLocation options:
I noticed that even with the above specified, I was only receiving location updates from Android when the phone was moving. |
Thanks @dlcole , it works fine in background in android. This is the code in Typescript: `@NativeClass() onCreate() { onBind(intent: android.content.Intent) { onUnbind(intent: android.content.Intent) { onDestroy() { getNotification() {
} |
Which platform(s) does your issue occur on?
Android
Android 10
Device Moto G(2nd Generation)
Please, provide the following version numbers that your issue occurs with:
CLI: 8.3.18
In, package.json
"nativescript-geolocation": "^5.1.0",
Please, tell us how to recreate the issue in as much detail as possible.
I'm using Geolocation apis to get location in background in android. When I call watchLocation in foreground, it's giving location, but when app goes to background, I didn't get any location. I am displaying location in console.log. For ios, when app goes in background mode, it's display log of location. I am using nativescript with angular.
Is there any code involved?
The text was updated successfully, but these errors were encountered: