Update user location to server when application is in background - android

Mobile Technology: React native
Live Tracking using: Socket.io
I have taxi services application, in which I'm updating driver's location continuously to server. It is working fine when application is foreground. Now I want to update location of driver to server using socket on any other way when the application is in background. I need solution that works for both ios/android.
Please share your suggestions

im not sure about using socket but you can try saving your data on an sqlite database then using react-native-background-task for when your app is in the background
https://github.com/jamesisaac/react-native-background-task
use it like so:
BackgroundTask.define(() => {
//API CALL
BackgroundTask.finish()
})
componentDidMount() {
BackgroundTask.schedule();
}
please note that it takes ATLEAST 15 minutes for it to be called on iOS

OK, here goes not such an answer more an explanation of the situation you are in.
I've been in the same situation before and there are some things you can do to test your React Native app in the same scenario as being in 'Doze Mode'. Android introduced this as a way to limit CPU and battery usage whilst the phone is in Doze Mode. Doze mode itself is reached if the phone is stationary and/or hasn't had any interaction with the user for a set amount of time.
Firstly, with your current solution - you want to test your app in Standby/Doze Mode. You can achieve this by doing the below:
Testing your app with Doze
You can test Doze mode by following these steps:
https://developer.android.com/training/monitoring-device-state/doze-standby#testing_doze
Configure a hardware device or virtual device with an Android 6.0 (API level 23) or higher system image.
Connect the device to your development machine and install your app.
Run your app and leave it active.
Force the system into idle mode by running the following command:
$ adb shell dumpsys deviceidle force-idle
When ready, exit idle mode by running the following command:
$ adb shell dumpsys deviceidle unforce
Reactivate the device by performing the following command:
$ adb shell dumpsys battery reset
Observe the behavior of your app after you reactivate the device. Make sure the app recovers gracefully when the device exits Doze.
This will give you your testing scenario in order to test on Android.
The problem:
Android gives the apps less and less control of phones connectivity whilst in the background. It will stop networking all together and only opens in a 'Maintenance Window'. Android state:
The Doze restriction on network access is also likely to affect your app, especially if the app relies on real-time messages such as tickles or notifications. If your app requires a persistent connection to the network to receive messages, you should use Firebase Cloud Messaging (FCM) if possible.
The issue:
The problem is not running your app in the background, your problem will be using network in the background. For example, headless JS will run - runnable tasks will run BUT it may not be able to make any requests for network.
Possible solutions:
Using Alarms
If you need to set alarms that fire while in Doze, use setAndAllowWhileIdle() or setExactAndAllowWhileIdle().
Alarms set with setAlarmClock()continue to fire normally — the system exits Doze shortly before those alarms fire.
Putting your App into a whitelist (visit the link above for acceptable use cases).
iOS:
For React Native, I used this:
https://github.com/mauron85/react-native-background-geolocation
This also works for Android, however the Doze mode (in my case) wouldn't send off network requests.

Related

Android: how to execute service when nearby found some ble device

Right now I'm developing client application for Android that works with physical locks with our controllers. User can open lock via Bluetooth using this application (connection -> secure session creation -> sending some user key).
All works fine except one feature: opening lock when Android device screen is off.
I am using following approach:
Foreground service that periodically scans for nearby BLE devices and when scan is successful (nearby lock device found using BLE filters), application tries to connect and send lock key to lock device.
So, there are two problems:
Android kills foreground service in few minutes after screen off. After setting ignore battery optimization it works ok, however I can't find universal way to navigate user to those settings (because those settings are vendor specific, see https://dontkillmyapp.com/ and android open battery settings programically).
Background limits for BLE scanning are tricky.
I know one application that achieved same thing without foreground service: https://play.google.com/store/apps/details?id=eu.hoermann.ast.bluesecur, this application seems to work good in background without user interaction with battery usage optimization menu.
So, question is:
what is the best way to trigger background process (device screen is turned off) when nearby suitable BLE device available?
Your requirements perfectly describe the scenario of a companion app. Disadvantage: it is only available from Android 12 (API level 31) onwards.
I think you shoul add a wake lock with the service:
https://developer.android.com/training/scheduling/wakelock
https://developer.android.com/training/scheduling/wakelock#cpu

Automated tests with Doze mode on android

I am trying to write some automated tests for how my app functions when the device enters and exits Doze mode. I know that you can use dumpsys deviceidle force-idle to force the device into idle mode when doing manual testing.
Is there a way to run that command from within a test app, or (better yet) an Android API to request Doze mode? If there isn't, does anyone have a recommendation for documentation regarding a good way to run adb commands at fixed points in an integration test?

All ADB devices offline when starting adb at boot

I'm testing an application which records user travels for later review. One part of testing involves testing application behavior when under user specified battery level.
It must stop recording to save battery. I have built a system with a raspberry pi that when you flip a switch, adb commands are sent to all connected devices to mock battery status.
My issue is when I tried to have adb as startup or as a service it, adb list all device offline thus the device doesn't work. My script will work as long you start manually the adb server.
More info:
It is the cold season here so we use a car to record travels. We can't have a screen on the device to comply with local regulations that is why, I require the adb to start at boot or during account automatic login.
Can anyone help me on this issue?

How to receive call on android + pjsip when phone in deep sleep

I'm trying to build voip app based on PJSIP on Android. I decided to use TCP for connection to make the connection be persistent. And app right now has background service (same process) which is responsible for all interactions with SIP server. TCP keep alive timeout is set to 1 minute (just for testing purposes). Everything works fine until the moment when phone goes into a deep sleep mode. From logs I see that phone still sends TCP KA but with 4 minutes interval (why???). But the main issue is if I try to call from another phone to this one the phone keeps sleeping and skips all INVITE messages. Appreciate any help.
When the Android device goes into deep sleep, the CPU throttles down and almost all OS services are suspended. See for example:
Android Sleep/Standby Mode
what is the difference between Sleep mode and Deep sleep mode?
If this were not the case, the OS and most other components would still be on, which would drain battery extremely fast, and most users would uninstall your app.
In order to receive incoming messages (such as the INVITE), it's recommended to use GCM. In this, you need to implement a server piece that is essentially handling the client's responsibilities while the client device is asleep.

Can an application on Android phone be updated in sleep mode?

There are 3 states an Android phone can be in: Awake, Asleep and Off. See: Android: Sleep stages/levels on an Android device?
In sleep mode screen goes off, CPU shuts down along with Wi-Fi and GPS radios. See: http://developer.android.com/reference/android/os/PowerManager.html and Android Sleep/Standby Mode
But GSM or CDMA radio still works and the device can receive incoming calls, SMSes, and IP packets. Also, Google introduced new Google Cloud Messaging service that sends data to devices and can, as I understood, wake the device if necessary. See: http://developer.android.com/guide/google/gcm/index.html
So, my question is: can application on Android device be updated via Google Play if the device is in sleep mode? I mean is Google Play allowed to receive some kind of messages from the cloud, wake the device and update applications? I assume that the application is allowed to be updated automatically and can be updated through mobile network (3G/4G).
Thank you.
UPDATE: I did some research and it happens that Nippey's answer and comments are correct. I did not see any updates to wake my device but as soon as the device is awake updating mechanism starts to work normally. So, theoretically, Google Play Store can start updating your applications right after the device wake up.
The device is able to process push-messages while in sleep mode.
As soon as you trigger an update via the play.google.com, this will issue a push request, which, as a consequence will wake up your device. The update will be installed immediately.
So, it doesn't work in sleep mode, but if the device gets a trigger to wake up, it will do what has to be done.

Categories

Resources