mauron85/cordova-plugin-background-geolocation

Foreground service randomly starts

Opened this issue · 11 comments

It seems that using a foreground service causes the service to randomly pop-up.
I can see it since I have a notification icon when this happens.
I might be using something wrong but the app is closed using navigator.app.exit().

Your Environment

  • Plugin version: 3.0.6
  • Platform: Android
  • OS version: 9
  • Device manufacturer and model: Smamsung galaxy

Context

I'm using the service as foreground service to prevent OS from killing the app.
It seems that this causes the service to start randomly.
This is somewhat described here., I think:
https://stackoverflow.com/questions/28632521/stopped-foreground-service-restarting-itself
This is my exit sequence:
https://github.com/IsraelHikingMap/Site/blob/master/IsraelHiking.Web/sources/application/services/application-exit.service.ts#L82L85

Expected Behavior

When properly exiting the app, the service should not start

Actual Behavior

The service starts randomly.

Possible Fix

I think the following SO question has some info, It might already be in place, not sure...
https://stackoverflow.com/questions/28632521/stopped-foreground-service-restarting-itself

Steps to Reproduce

  1. Download and install the following app: https://play.google.com/store/apps/details?id=il.org.osm.israelhiking&hl=en
  2. Wait for it to fully start
  3. Press the back button twice
  4. Some time in the future you should see a GPS icon in the notification bar.

Debug logs

I'll see if I can produce some later on.

I think it is related to #551 or #552
@busaku @papattes @dedd1993 @hoisel Do you guys still experience this issue?
My app doesn't call the configure method before I start tracking but I still get this behavior.
Should I call the stop() method regardless of if the config and start were called when terminating the app?
I'm also using Ionic-native wrapper, not sure if it has anything to do with this issue...

The following are the log I'm getting when only entering and exiting the app:

2020-03-10 22:20:18 | DEBUG | Registering event listeners
2020-03-10 22:20:18 |  INFO | Initializing plugin
2020-03-10 22:19:02 | DEBUG | Service in [NOT STARTED] state. cmdId: [1]. startId: [1]
2020-03-10 22:19:02 |  INFO | Network condition changed has connectivity: true
2020-03-10 22:19:02 |  INFO | Creating PostLocationTask
2020-03-10 22:19:02 |  INFO | Creating LocationServiceImpl
2020-03-10 22:19:01 |  INFO | Attempt to stop bg service
2020-03-10 22:19:01 |  INFO | Destroying plugin
2020-03-10 22:19:01 |  INFO | Destroying plugin
2020-03-10 22:19:01 |  INFO | App is no longer visible
2020-03-10 22:19:01 |  INFO | App will be paused multitasking=true
2020-03-10 22:16:42 |  INFO | App will be resumed multitasking=true
2020-03-10 22:16:41 |  INFO | App is visible
2020-03-10 22:16:25 |  INFO | App is no longer visible
2020-03-10 22:16:23 |  INFO | App will be paused multitasking=true
2020-03-10 22:15:30 |  INFO | App will be resumed multitasking=true
2020-03-10 22:15:27 |  INFO | App will be paused multitasking=true
2020-03-10 22:15:12 | DEBUG | Registering event listeners
2020-03-10 22:15:12 |  INFO | Initializing plugin

The following seems like it shouldn't be there:
Creating LocationServiceImpl
This is a freshly installed app, I've exited the app at 22:19:01 without invoking neither config nor start or stop... :-/

Here's another log file:
In 21:17 an icon started appearing on my phone's notification bar (although I can't see it in the logs).
I pressed it at 21:54.
I did not fully exit the app on 21:08 but only pressed the home button.
The following usage did not call the configure or start methods.
Any input on this? Does someone have a workaround? Something I can try?

2020-03-11 21:54:50 | DEBUG | Service in [STARTED] state. cmdId: [3]. startId: [14]
2020-03-11 21:54:50 |  INFO | App will be paused multitasking=true
2020-03-11 21:54:38 | DEBUG | Registering event listeners
2020-03-11 21:54:38 |  INFO | Initializing plugin
2020-03-11 21:08:04 |  INFO | App is no longer visible
2020-03-11 21:08:04 | DEBUG | Service in [NOT STARTED] state. cmdId: [3]. startId: [12]
2020-03-11 21:08:04 |  INFO | App will be paused multitasking=true
2020-03-11 21:07:50 |  INFO | App will be resumed multitasking=true
2020-03-11 21:07:50 |  INFO | App is visible
2020-03-11 21:07:08 | DEBUG | Service in [NOT STARTED] state. cmdId: [3]. startId: [11]
2020-03-11 21:07:08 |  INFO | App is no longer visible
2020-03-11 21:07:08 |  INFO | App will be paused multitasking=true
2020-03-11 20:58:20 |  INFO | App will be resumed multitasking=true
2020-03-11 20:58:20 |  INFO | App is visible
2020-03-11 20:57:50 | DEBUG | Service in [NOT STARTED] state. cmdId: [3]. startId: [10]
2020-03-11 20:57:50 |  INFO | App is no longer visible
2020-03-11 20:57:50 |  INFO | App will be paused multitasking=true
2020-03-11 20:57:45 |  INFO | App will be resumed multitasking=true
2020-03-11 20:57:44 |  INFO | App is visible
2020-03-11 20:57:29 | DEBUG | Service in [NOT STARTED] state. cmdId: [3]. startId: [9]
2020-03-11 20:57:29 |  INFO | App is no longer visible
2020-03-11 20:57:29 |  INFO | App will be paused multitasking=true
2020-03-11 20:57:26 |  INFO | App will be resumed multitasking=true
2020-03-11 20:57:25 | DEBUG | Service in [NOT STARTED] state. cmdId: [3]. startId: [8]
2020-03-11 20:57:25 |  INFO | App will be paused multitasking=true
2020-03-11 20:57:13 |  INFO | App will be resumed multitasking=true
2020-03-11 20:57:13 |  INFO | App is visible
2020-03-11 20:57:06 |  INFO | App is no longer visible
2020-03-11 20:57:00 | DEBUG | Service in [NOT STARTED] state. cmdId: [3]. startId: [7]
2020-03-11 20:57:00 |  INFO | App will be paused multitasking=true
2020-03-11 20:55:50 |  INFO | App will be resumed multitasking=true
2020-03-11 20:55:48 | DEBUG | Service in [NOT STARTED] state. cmdId: [3]. startId: [6]
2020-03-11 20:55:48 |  INFO | App will be paused multitasking=true
2020-03-11 20:55:42 |  INFO | App will be resumed multitasking=true
2020-03-11 20:55:42 |  INFO | App is visible
2020-03-11 20:55:17 |  INFO | App is no longer visible
2020-03-11 20:55:10 | DEBUG | Service in [NOT STARTED] state. cmdId: [3]. startId: [5]
2020-03-11 20:55:10 |  INFO | App will be paused multitasking=true
2020-03-11 20:54:54 |  INFO | App will be resumed multitasking=true
2020-03-11 20:54:54 |  INFO | App is visible
2020-03-11 20:54:22 |  INFO | App is no longer visible
2020-03-11 20:54:22 | DEBUG | Service in [NOT STARTED] state. cmdId: [3]. startId: [4]
2020-03-11 20:54:22 |  INFO | App will be paused multitasking=true
2020-03-11 20:54:11 |  INFO | App will be resumed multitasking=true
2020-03-11 20:54:11 |  INFO | App is visible
2020-03-11 20:33:52 | DEBUG | Service in [NOT STARTED] state. cmdId: [3]. startId: [3]
2020-03-11 20:33:52 |  INFO | App is no longer visible
2020-03-11 20:33:52 |  INFO | App will be paused multitasking=true
2020-03-11 20:32:42 |  INFO | App will be resumed multitasking=true
2020-03-11 20:32:42 |  INFO | App is visible
2020-03-11 20:32:21 |  INFO | App is no longer visible
2020-03-11 20:32:19 | DEBUG | Service in [NOT STARTED] state. cmdId: [3]. startId: [2]
2020-03-11 20:32:19 |  INFO | App will be paused multitasking=true
2020-03-11 20:32:04 |  INFO | App will be resumed multitasking=true
2020-03-11 20:32:04 |  INFO | App is visible
2020-03-11 20:31:48 |  INFO | App is no longer visible
2020-03-11 20:31:45 | DEBUG | Registering event listeners
2020-03-11 20:31:45 |  INFO | Initializing plugin
2020-03-11 20:25:41 |  INFO | Network condition changed has connectivity: true
2020-03-11 20:25:41 | DEBUG | Service in [NOT STARTED] state. cmdId: [1]. startId: [1]
2020-03-11 20:25:40 |  INFO | Creating PostLocationTask
2020-03-11 20:25:40 |  INFO | Creating LocationServiceImpl
2020-03-11 20:25:40 |  INFO | Attempt to stop bg service
2020-03-11 20:25:40 |  INFO | Destroying plugin
2020-03-11 20:25:40 |  INFO | Destroying plugin
2020-03-11 20:25:40 |  INFO | App is no longer visible
2020-03-11 20:25:40 |  INFO | App will be paused multitasking=true
2020-03-11 20:25:38 |  INFO | App will be resumed multitasking=true
2020-03-11 20:25:38 |  INFO | App is visible
2020-03-11 20:25:37 |  INFO | App is no longer visible
2020-03-11 20:25:37 |  INFO | App will be paused multitasking=true
2020-03-11 20:25:35 |  INFO | App will be resumed multitasking=true
2020-03-11 20:25:35 |  INFO | App is visible
2020-03-11 20:25:30 |  INFO | App is no longer visible
2020-03-11 20:25:30 |  INFO | App will be paused multitasking=true
2020-03-11 20:25:28 |  INFO | App will be resumed multitasking=true
2020-03-11 20:25:28 |  INFO | App is visible
2020-03-11 20:19:10 |  INFO | App is no longer visible
2020-03-11 20:19:06 |  INFO | App will be paused multitasking=true
2020-03-11 20:19:06 |  INFO | App will be resumed multitasking=true
2020-03-11 20:19:06 |  INFO | App is visible
2020-03-11 20:18:43 |  INFO | App is no longer visible
2020-03-11 20:18:43 |  INFO | App will be paused multitasking=true
2020-03-11 20:18:41 |  INFO | App will be resumed multitasking=true
2020-03-11 20:18:32 |  INFO | App will be paused multitasking=true
2020-03-11 20:18:27 |  INFO | App will be resumed multitasking=true
2020-03-11 20:18:01 |  INFO | App will be paused multitasking=true
2020-03-11 20:17:30 | DEBUG | Registering event listeners
2020-03-11 20:17:30 |  INFO | Initializing plugin

Also the following incident where I did exit the app at 09:29 and not moved it to the background.
The notification icon appeared at 09:55 :-(
I entered the app to close it at 10:23...

2020-03-12 10:23:00 |  INFO | Initializing plugin
2020-03-12 09:29:16 | DEBUG | Service in [NOT STARTED] state. cmdId: [1]. startId: [1]
2020-03-12 09:29:16 |  INFO | Network condition changed has connectivity: true
2020-03-12 09:29:16 |  INFO | Creating PostLocationTask
2020-03-12 09:29:16 |  INFO | Creating LocationServiceImpl
2020-03-12 09:29:16 |  INFO | Attempt to stop bg service
2020-03-12 09:29:16 |  INFO | Destroying plugin
2020-03-12 09:29:16 |  INFO | Destroying plugin
2020-03-12 09:29:16 |  INFO | App is no longer visible
2020-03-12 09:29:15 |  INFO | App will be paused multitasking=true
2020-03-12 09:25:10 | DEBUG | Registering event listeners
2020-03-12 09:25:10 |  INFO | Initializing plugin

@mauron85 anything I can do to help solving this?
I'm not sure what I'm doing wrong but the issue still exists in version 3.1.0... :-(

Anyone?? :-((( this bug is super annoying and I don't have a clue how to solve it... :-(((
Has anyone found a workaround for this? a fork? anything?

cc:
@shalini-github

Same problem for me !
Stil not any fix ?

For information here is the way I clear the notification :

when the user close the app, I check it with Capacitor and appStateChange. If after 15 secondes the app is still not active I kill the app. Then the notification disappear

@dabaaaz thanks for the info! The problem I'm facing is not that the icon keeps appearing after I close the app but that it starts to appear a few hours after I close the app. Does your workaround solve this kind of issue?

I've tried that solution : https://stackoverflow.com/a/30391451/2019903

When I pass the NOTIFICATION_ID to 0 at the line 107 of the file LocationServiceImpl.java it works, no more notification, even at the start

@dabaaaz do you have a fork/npm package I can test? In the mentioned SO question this seems like a backwards compatibility solution that might stop working in the future...

stale commented

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.