Native android background module for react native - android

I want to write a android native module which work as a background service and get current location and post to server and then integrate that module with react native.

if you just want it for the location then i suggest react-native-workers
it has access to native modules (network, geolocation, storage ...) you can aslo integrate it with react-native-queue

There are a couple of example projects / demo apps you might want to check out which implement both "Foreground Services" and "Background Services" in Android to help you get location updates while the app is closed or the screen is off. The background service example will only allow you to get updates as frequent as 1 minute, while the foreground service example will allow you to get updates as fast as 1 second (or maybe faster, I haven't tested that yet) while also displaying an "Ongoing Notification" to the user.
Background Service Example: https://github.com/comoser/rn-background-location
Foreground Service Example: https://github.com/andersryanc/ReactNative-LocationSample
There are a number of Android specific code adjustments you will need to make in either case. It's not ready yet, but in the near future I plan to update my repo's readme with a detailed set of instructions for implementing the necessary changes in your project.

Related

React-Native Android Mobile App - Starting a Server at Local Host Not Working

I've recently been trying to get a mobile app I'm working on functioning in react-native android. For context I have a few services that I start that are served on :5009 and :5010 respectively, these services are used for basically offline API calls that I store on the phone itself and run with a golang web server cross compiled from gomobile. The services start by just running a function that just initializes the server.
The issue is, I've gotten these services to work on both android with a non-react-native java version using a foreground intent service and in iOS using DispatchQueue background pretty easily actually.
I've tried using a headless js service (foregroundBool on and off) that calls java code, starting a foreground service in the main application itself and a bunch of other stuff but nothing seems to be working as desired. I'm kind of out of ideas and basically running into variations of the same problem (if one starts the other one blocks) so any suggestion would be greatly appreciated.
It looks like a true foreground service in android might not be possible with react-native after doing a bit more research? Is this accurate?
Thanks,

How to create automotive application to trigger notifation for overspeed in android studio

I want to create first application to listen car speed without open this application.
It is possible to monitor car speed in background
To listen to Car speed you need to create an object of CarPropertyManager and register for change in Car Speed Property value.(PERF_VEHICLE_SPEED) using method registerCallback() present in CarPropertyManager
registerCallback() method requires a Callback in which to receive speed updates, proprty id of Speed(Constant Value: 291504647 (0x11600207) which is mentioned for PERF_VEHICLE_SPEED in above mentioned link) and rate at which to receive these updates.
Put all code in a service to receive updates in the background.
You can find samples for the Android Automotive application at https://android.googlesource.com/platform/packages/services/Car/+/master/tests/EmbeddedKitchenSinkApp
I have tested sample apk with automotive emulator.
My apk contains some services to run some code on background.
when my app is open in emulator, services are running successfully. But I try to minimize the app services are stopped automatically.
there any restrictions in automotive os or we need to follow any procedure to use services in our app.

Requesting Permissions from Xamarin Android Service

Using Xamarin Android, I need to be able to access location updates from within a service.
I have tried the native Android.LocationManager, Plugin.Geolocator, and Xamarin.Essentials.Geolocation, but none of them seem to provide location updates.
If I do it inside an activity, it works fine, but I have a requirement to post location updates from a service which launches at boot time.
Yeah, I know it's a "privacy violation" or whatever, but the app is only used on company devices for a specific internal use.
I saw somewhere that requesting permissions from a service "is tricky," so I know it's possible, but the guy didn't elaborate on how to do it.

Is it possible without react-native to gather geolocation information in a background task for Android and/or iOS?

Is it possible in react-native to execute a background task with monitoring GPS position (without running the whole app in the background), even if the phone is locked?
I want to show some information (notification) onscreen when the phone will be in some position.
I need to declare the subject of a project in my studies, but I don't know if it's possible to implement this functionality.
It most certainly is... if you have only one given position (Or fewer than 20 positions) what you'd be best using is CoreLocation's geofencing APIs!
You can set up a region like so:
if CLLocationManager.isMonitoringAvailable(for: CLCircularRegion.self) {
// Register the region.
let region = CLCircularRegion(
center: center,
radius: maxDistance, identifier: identifier
)
region.notifyOnEntry = true
locationManager.startMonitoring(for: region)
}
(Assuming you have already requested and checked the user's location permissions)!
Your app will then get launched in the background whenever a region monitoring event occurs, so you must make sure to setup a new CLLocationManager and give it a delegate to receive the updates in your application(_application:didFinishLaunchingWithOptions) function!
You can then use the delegate callbacks to trigger a local notification.
There is a slight caveat here, that this won't work if the user has disabled background app refresh for your app!
Luck, there's already a react-native library that allows you to setup these regions: https://github.com/martijndeh/react-native-region-monitor. Unfortunately I do think it will launch your JavaScript app fully (in the background of course) when a notification comes through, but that's not too big an issue.
There may also be info on the GitHub provided for how to implement this on Android too!

How does "Find My Friends" (FMF) App respond to location?

I am trying to create an App that will respond with locations of people in a group when anyone requests it - similar to what Find My Friends (FMF) does but across Android and iOS and I am using Phonegap. I have been researching how the FMF App returns locations - along the lines of this SO Post and am trying to understand that better.
At some point iOS will terminate the App due to memory pressure. I understand if you set your Background mode in Capabilities section of XCode to location and if there is a significant change in location the App will restart in Background. Is that what the FMF App is doing to ensure it stays up to respond to location request?
When location is requested I send a push notification (data notification only so it is processed in the background - users doesn't see it) and the App is expected to respond if it is in Background/Foreground. However in the case of iOS if the App has been terminated by iOS and there has been no significant location change, the App will not be up and running in Background and won't respond. Is that the best we can do besides somehow preventing iOS from suspending/terminating the App? Appreciate any insights.
I have read about creating background services to help but that seems to apply more to Android than to iOS.
Thank you for any guidance and pointers.
Sanjay.
First of all, you shoud Declaring Your App’s Supported Background Tasks.
Support for some types of background execution must be declared in advance by the app that uses them. In Xcode 5 and later, you declare the background modes your app supports from the Capabilities tab of your project settings. Enabling the Background Modes option adds the UIBackgroundModes key to your app’s Info.plist file. Selecting one or more checkboxes adds the corresponding background mode values to that key.
Then Tracking the User’s Location and cached the latest location.
And handle your remote notification to upload the latest location.
When your want to get somebody's location, Server push a remote-notification to the Client with APNS, and Client handle the remote-notification with application:didReceiveRemoteNotification:fetchCompletionHandler:
Or timed to upload location with Finite-Length Tasks in the background:
- (void)applicationDidEnterBackground:(UIApplication *)application
{
bgTask = [application beginBackgroundTaskWithName:#"MyTask" expirationHandler:^{
// Clean up any unfinished task business by marking where you
// stopped or ending the task outright.
[application endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
// Start the long-running task and return immediately.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// Do the work associated with the task, preferably in chunks.
[application endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
});
}
Hope that help you ~

Categories

Resources