Skip to content

Commit

Permalink
location tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
Salmankhan033 committed Feb 5, 2024
1 parent 217fbdf commit 4a4a2cd
Show file tree
Hide file tree
Showing 19 changed files with 8,266 additions and 21 deletions.
117 changes: 105 additions & 12 deletions App.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,112 @@
/**
* Sample React Native App
* https://github.com/facebook/react-native
*
* @format
*/

import { View, Text } from 'react-native'
import React from 'react'
import { StyleSheet, Text, View, PermissionsAndroid,Platform ,Button, Alert, SafeAreaView} from 'react-native'
import React, {useEffect,useRef } from 'react';
import Geolocation from 'react-native-geolocation-service';
import ReactNativeForegroundService from "@supersami/rn-foreground-service";

const App = () => {
const watchIdRef = useRef(null);
if (Platform.OS === 'android') {
// Use PermissionsAndroid here
}
useEffect(() => {
requestLocationPermission()
updateforeground();
Notification()
startTracking();
}, []);
const requestLocationPermission = async () => {
Geolocation.requestAuthorization('always')
try {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
{
title: 'Location Permission',
message: 'App needs access to your location.',
buttonNeutral: 'Ask Me Later',
buttonNegative: 'Cancel',
buttonPositive: 'OK',
},
);

if (granted === PermissionsAndroid.RESULTS.GRANTED) {
console.log('1Location permission granted');
} else {
console.log('Location permission denied');
}
PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.ACCESS_BACKGROUND_LOCATION,
{
title: 'Background Location Permission',
message:
'We need access to your location ' +
'so you can get live quality updates.',
buttonNeutral: 'Ask Me Later',
buttonNegative: 'Cancel',
buttonPositive: 'OK',
},
);
} catch (err) {
console.warn(err);
}
}
const updateforeground =()=>{
ReactNativeForegroundService.add_task(() => startTracking(), {
delay: 100,
onLoop: true,
taskId: "taskid",
onError: (e) => console.log(`Error logging:`, e),
});
}
const Notification =()=>{

ReactNativeForegroundService.start({
id: 1244,
title: 'Location Tracking',
message: 'Location Tracking',
icon: 'ic_launcher',
button: false,
button2: false,
// buttonText: "Button",
// button2Text: "Anther Button",
// buttonOnPress: "cray",
setOnlyAlertOnce: true,
color: '#000000',
});
startTracking()
}

const startTracking = async () => {
let s= Geolocation.requestAuthorization('always');

Geolocation.watchPosition(
position => {
let coordinates: any = [];
coordinates[0] = position.coords.longitude;
coordinates[1] = position.coords.latitude;
console.warn(Platform.OS,"App Position tracking",coordinates)
},
error => {
console.log('maperror in getting location', error.code, error.message);

},


{ enableHighAccuracy: true, distanceFilter: 0 },
);
};

return (
<View>
<Text>App</Text>



<View style={{justifyContent:'center',alignItems:'center',flex:1}}>
<Text style={{color:'red',fontWeight:'600',fontSize:20,margin:30}}>Location Tracking</Text>
{Platform.OS==="android" && <Button onPress={Notification} title='Start Tracking'/>}

</View>

)
}
export default App

export default App
const styles = StyleSheet.create({})
10 changes: 9 additions & 1 deletion android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ android {
versionName "1.0"
}
signingConfigs {
release {
if (project.hasProperty('MYAPP_UPLOAD_STORE_FILE')) {
storeFile file(MYAPP_UPLOAD_STORE_FILE)
storePassword MYAPP_UPLOAD_STORE_PASSWORD
keyAlias MYAPP_UPLOAD_KEY_ALIAS
keyPassword MYAPP_UPLOAD_KEY_PASSWORD
}
}
debug {
storeFile file('debug.keystore')
storePassword 'android'
Expand All @@ -97,7 +105,7 @@ android {
release {
// Caution! In production, you need to generate your own keystore file.
// see https://reactnative.dev/docs/signed-apk-android.
signingConfig signingConfigs.debug
signingConfig signingConfigs.release
minifyEnabled enableProguardInReleaseBuilds
proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
}
Expand Down
Binary file added android/app/release/app-release.apk
Binary file not shown.
20 changes: 20 additions & 0 deletions android/app/release/output-metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"version": 3,
"artifactType": {
"type": "APK",
"kind": "Directory"
},
"applicationId": "com.backgroundlocation",
"variantName": "release",
"elements": [
{
"type": "SINGLE",
"filters": [],
"attributes": [],
"versionCode": 1,
"versionName": "1.0",
"outputFile": "app-release.apk"
}
],
"elementType": "File"
}
20 changes: 20 additions & 0 deletions android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,14 +1,34 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />

<!-- <uses-permission android:name="android.permission.POST_NOTIFICATIONS"/> -->

<application
android:name=".MainApplication"
android:label="@string/app_name"
android:icon="@mipmap/ic_launcher"
android:roundIcon="@mipmap/ic_launcher_round"
android:allowBackup="false"
android:theme="@style/AppTheme">
<meta-data
android:name="com.supersami.foregroundservice.notification_channel_name"
android:value="Sticky Title"
/>
<meta-data
android:name="com.supersami.foregroundservice.notification_channel_description"
android:value="Sticky Description."
/>
<meta-data
android:name="com.supersami.foregroundservice.notification_color"
android:resource="@color/blue"
/>
<service android:name="com.supersami.foregroundservice.ForegroundService"></service>
<service android:name="com.supersami.foregroundservice.ForegroundServiceTask"></service>

<activity
android:name=".MainActivity"
android:label="@string/app_name"
Expand Down
8 changes: 8 additions & 0 deletions android/app/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

<resources>
<item name="blue" type="color">#00C4D1
</item>
<integer-array name="androidcolors">
<item>@color/blue</item>
</integer-array>
</resources>
2 changes: 2 additions & 0 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ buildscript {
targetSdkVersion = 34
ndkVersion = "25.1.8937393"
kotlinVersion = "1.8.0"
playServicesLocationVersion = "21.0.1"
}
repositories {
google()
Expand All @@ -15,6 +16,7 @@ buildscript {
classpath("com.android.tools.build:gradle")
classpath("com.facebook.react:react-native-gradle-plugin")
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin")

}
}

Expand Down
4 changes: 4 additions & 0 deletions android/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,7 @@ newArchEnabled=false
# Use this property to enable or disable the Hermes JS engine.
# If set to false, you will be using JSC instead.
hermesEnabled=true
MYAPP_RELEASE_STORE_FILE=my-release-key.keystore
MYAPP_RELEASE_KEY_ALIAS=my-key-alias
MYAPP_RELEASE_STORE_PASSWORD=romankhan
MYAPP_RELEASE_KEY_PASSWORD=romankhan
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
*/

import {AppRegistry} from 'react-native';
import ReactNativeForegroundService from '@supersami/rn-foreground-service';

import App from './App';
import {name as appName} from './app.json';

ReactNativeForegroundService.register();

AppRegistry.registerComponent(appName, () => App);
4 changes: 4 additions & 0 deletions ios/BackgroundLocation-Bridging-Header.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
//
// Use this file to import your target's public headers that you would like to expose to Swift.
//

Loading

0 comments on commit 4a4a2cd

Please sign in to comment.