React Native - Execute code in background on specific battery percentage - react-native

I have a react native application where the user can choose any battery level from 15 to 100. When his battery reaches the chosen value, javascript code should be executed.
Is there a way to do this when the app is in the background? I am aware I can have long running tasks, which will be displayed as a notification to the user and I can have my code logic there.
Are there any alternatives that do not include long running tasks?

My solution for this problem to register a BroadcastReceiver in android, which will be executed every time when the battery percentage changes. It works in background and if the battery reaches a certain threshhold, a headless task can be executed which will run javascript code.

Related

Running cleanup when a react native app is closed

In my react-native app's code:
I am monitoring AppState changes
When AppState changes to 'inactive' or 'background', I write about 2000 key/value pairs of data to AsyncStorage
When the app starts I read this data
When I test the app on android (didn't test on iOS yet):
If I minimize the app, the data is written as expected. I can see that by closing the app after minimizing it, and restarting it
However, if I click on the 'Recent' button in the navigation bar (which changes AppState to 'inactive') and immediately click on the 'X' button at the top right of the app, the data doesn't seem to be written, or at least not all of it (I am not checking all 2000 values, just few of them). If I try to write only few values, they are written
Question:
The explanation to the above behavior seems to be simple: the app has enough time to perform few operations before I click on 'close', but not to write 2000 values. Is there a way to perform the writing before the app is closed?
Actually it's not recommended to do any async function or anything that could take too long. Because you don't have the control of how much time it will take to write the 2000 values. The app can go to background and the SO can kill the proccess of your app. When the app is in background is the SO that controls it.

react native background geolocation

I have a requirement to save user's geolocation when react-native app is running in background. I know react-native-background-geolocation is available for this but I don't want to use any third party tool for this. Is there a way I can fetch users current location even if the app is running in the background?
With the last Expo SDK 32 you should be able to to it.
We’re excited to announce that this release includes initial support for background location, a highly requested feature from many Expo users. You can now define simple JavaScript tasks in your app and register them to receive location updates in the background. Additionally, you can set up geofencing tasks that are triggered when the device enters or leaves specific geographic regions

How to simulate the status bar in Codename one?

I am currently trying to get a local notification to work on an example app. I was following the simple guide here and just copied its code to see it working:
https://github.com/codenameone/codenameone-demos/blob/master/LocalNotificationTest/src/com/codename1/tests/localnotifications/LocalNotificationTest.java
However, in the simulation, I cannot see any changes in the status bar. Is the status bar even simulated or just a static image? Do I have to build the app and send it to an actual device every time I want to test it? That would not only be tedious but also crunch on the available monthly builds.
Is there a setting in the simulator to activate this that I missed?
Thanks and best regards
Local notifications happen in the background which isn't supported by the simulator so this is something you will only see on the device. You can simulate the minimizing of the app (pause/resume) but the Codename One simulator is not a full mobile OS simulator.

WP8: Can this (PhoneApplicationService.RunningInBackground ) be used outside of location based app?

From this link below, it seems that your app can still get event when app is switched to background. But it seems it is used only for location based app. Can normal app do that? I had tried to declare ID_CAP_LOCATION but still Application_RunningInBackground not get called when switch to background.
http://msdn.microsoft.com/en-us/library/windowsphone/develop/microsoft.phone.shell.phoneapplicationservice.runninginbackground(v=vs.105).aspx
The app doesn't execute in background, unless it is continously tracking the location.
This section lists the conditions under which the operating system
will deactivate an app running in the background....
The app stops actively tracking location. An app stops tracking location by removing event handlers for the PositionChanged and
StatusChanged events of the Geolocator class or by calling the Stop()
method of the GeoCoordinateWatcher class.
Source: Running location-tracking apps in the background for Windows Phone 8
You will find complete info of how to run apps in background here:
How to run location-tracking apps in the background for Windows Phone 8

Long-running task performed in foreground is suspended when app enters background

When a user first opens my app, I need to download and install some content from a server before they can begin using the app. The problem is that this takes around 5 minutes on wifi, during which time the app goes into the background and the download is suspended.
Is there any way to either:
prevent an iOS app from entering the background whilst I perform my download
or continue peforming the task in the background (i.e. perform the task irrespective of whether the app is in the foreground or background)
Thanks
It really doesn't matter, if the user presses the home button it will go to background. Although you can do two things to mitigate the problem:
Use beginBackgroundTaskWithExpirationHandler, to give you a bit more time to download. Which you can read here.
Don't allow the device to become iddle, with [UIApplication sharedApplication].idleTimerDisabled = YES;. You can read more about that here.
Either way, the best thing you can do is to tell the user, that is an important download and he shouldn't quit the application.
Can't you include some or all of the content in your app bundle instead, and just download changes on first run?
I can't imagine this is a good first user experience, and it may not pass App Store review like this.
The only third party apps that are allowed to download in the background are newsstand apps loading issue content, and Apple are pretty strict about what they allow as newsstand apps.
You can't do what you want, in this situation. One way, and I think the best and only, is to resume your download when you app becomes active (returns to foreground state). Also, don't forget to register for connectivity notifications (Reachability class can be used for this purpose from this Apple sample app http://developer.apple.com/library/ios/#samplecode/Reachability/Introduction/Intro.html). Good Luck!