react-native-background-live-tracking 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json ADDED
@@ -0,0 +1,30 @@
1
+ {
2
+ "name": "react-native-background-live-tracking",
3
+ "version": "1.0.0",
4
+ "description": "Production-ready Android background GPS tracking with REST/WebSocket, offline queue, and foreground service.",
5
+ "main": "src/index.ts",
6
+ "types": "src/index.ts",
7
+ "license": "MIT",
8
+ "author": "",
9
+ "homepage": "https://github.com/your-org/react-native-background-live-tracking#readme",
10
+ "keywords": [
11
+ "react-native",
12
+ "background",
13
+ "location",
14
+ "tracking",
15
+ "foreground-service",
16
+ "android"
17
+ ],
18
+ "files": [
19
+ "src",
20
+ "android/build.gradle",
21
+ "android/consumer-rules.pro",
22
+ "android/src",
23
+ "README.md",
24
+ "react-native.config.js"
25
+ ],
26
+ "peerDependencies": {
27
+ "react": ">=18.0.0",
28
+ "react-native": ">=0.73.0"
29
+ }
30
+ }
@@ -0,0 +1,12 @@
1
+ module.exports = {
2
+ dependency: {
3
+ platforms: {
4
+ android: {
5
+ packageImportPath:
6
+ 'import com.reactnativebackgroundlivetracking.TrackingPackage;',
7
+ packageInstance: 'new TrackingPackage()',
8
+ },
9
+ ios: null,
10
+ },
11
+ },
12
+ };
@@ -0,0 +1,26 @@
1
+ import { NativeModules, Platform } from 'react-native';
2
+
3
+ type NativeApi = {
4
+ startTracking: (configJson: string) => Promise<void>;
5
+ stopTracking: () => Promise<void>;
6
+ isTracking: () => Promise<boolean>;
7
+ openBatteryOptimizationSettings: () => Promise<void>;
8
+ };
9
+
10
+ const LINKING_ERROR =
11
+ "The package 'react-native-background-live-tracking' doesn't seem to be linked. Make sure: \n\n" +
12
+ Platform.select({ android: '- You rebuilt the app after installing the package.\n', default: '' }) +
13
+ '- You are not using Expo Go (native modules require a dev build).\n';
14
+
15
+ const Native: NativeApi = NativeModules.RNBackgroundLiveTracking
16
+ ? NativeModules.RNBackgroundLiveTracking
17
+ : new Proxy(
18
+ {},
19
+ {
20
+ get() {
21
+ throw new Error(LINKING_ERROR);
22
+ },
23
+ },
24
+ ) as NativeApi;
25
+
26
+ export default Native;
package/src/index.ts ADDED
@@ -0,0 +1,103 @@
1
+ import { DeviceEventEmitter, EmitterSubscription, Platform } from 'react-native';
2
+ import Native from './NativeTracking';
3
+ import type { LocationPayload, TrackingStartOptions, TrackingStatus } from './types';
4
+
5
+ const EVENT_LOCATION = 'RNBackgroundLiveTracking_location';
6
+ const EVENT_STATUS = 'RNBackgroundLiveTracking_status';
7
+
8
+ export const Tracking = {
9
+ /**
10
+ * Start continuous background location tracking and sync to your backend.
11
+ * Android only (this package disables iOS autolinking).
12
+ */
13
+ async start(options: TrackingStartOptions): Promise<void> {
14
+ if (Platform.OS !== 'android') {
15
+ console.warn(
16
+ '[react-native-background-live-tracking] Tracking.start is a no-op on this platform (Android-only library).',
17
+ );
18
+ return;
19
+ }
20
+ const {
21
+ driverId,
22
+ interval,
23
+ serverUrl,
24
+ socketUrl,
25
+ socketTransport,
26
+ notificationTitle,
27
+ notificationBody,
28
+ autoStartOnBoot,
29
+ restHeaders,
30
+ notificationMapPreview,
31
+ googleStaticMapsApiKey,
32
+ pickup,
33
+ } = options;
34
+ if (!driverId || !serverUrl || !interval || interval < 1000) {
35
+ throw new Error(
36
+ 'Tracking.start requires driverId, serverUrl, and interval (minimum 1000 ms).',
37
+ );
38
+ }
39
+ const payload = {
40
+ driverId,
41
+ intervalMs: Math.floor(interval),
42
+ serverUrl,
43
+ socketUrl: socketUrl ?? null,
44
+ socketTransport: socketTransport ?? null,
45
+ notificationTitle: notificationTitle ?? 'Driver tracking',
46
+ notificationBody:
47
+ notificationBody ?? 'Live location is being shared in the background.',
48
+ autoStartOnBoot: autoStartOnBoot !== false,
49
+ restHeaders: restHeaders ?? {},
50
+ notificationMapPreview: notificationMapPreview === true,
51
+ googleStaticMapsApiKey: googleStaticMapsApiKey ?? '',
52
+ pickup: pickup ?? null,
53
+ };
54
+ await Native.startTracking(JSON.stringify(payload));
55
+ },
56
+
57
+ async stop(): Promise<void> {
58
+ if (Platform.OS !== 'android') {
59
+ return;
60
+ }
61
+ await Native.stopTracking();
62
+ },
63
+
64
+ async isActive(): Promise<boolean> {
65
+ if (Platform.OS !== 'android') {
66
+ return false;
67
+ }
68
+ return Native.isTracking();
69
+ },
70
+
71
+ async getStatus(): Promise<TrackingStatus> {
72
+ const active = await this.isActive();
73
+ return { active };
74
+ },
75
+
76
+ /** Opens the screen where the user can disable battery optimization for your app (Android). */
77
+ async openBatterySettings(): Promise<void> {
78
+ if (Platform.OS !== 'android') {
79
+ return;
80
+ }
81
+ await Native.openBatteryOptimizationSettings();
82
+ },
83
+
84
+ /**
85
+ * Subscribe to location updates (same payload sent to the server).
86
+ * Useful for showing the driver UI while the service runs.
87
+ */
88
+ addLocationListener(
89
+ listener: (location: LocationPayload) => void,
90
+ ): EmitterSubscription {
91
+ return DeviceEventEmitter.addListener(EVENT_LOCATION, listener);
92
+ },
93
+
94
+ addStatusListener(
95
+ listener: (status: TrackingStatus) => void,
96
+ ): EmitterSubscription {
97
+ return DeviceEventEmitter.addListener(EVENT_STATUS, listener);
98
+ },
99
+ };
100
+
101
+ export type { LocationPayload, TrackingStartOptions, TrackingStatus };
102
+ export { EVENT_LOCATION, EVENT_STATUS };
103
+ export default Tracking;
package/src/types.ts ADDED
@@ -0,0 +1,41 @@
1
+ export type SocketTransport = 'rest' | 'websocket' | 'socket.io';
2
+
3
+ export type TrackingStartOptions = {
4
+ driverId: string;
5
+ interval: number;
6
+ serverUrl: string;
7
+ socketUrl?: string;
8
+ /** How to use socketUrl when provided. Default: 'socket.io' if URL path suggests it, else 'websocket'. */
9
+ socketTransport?: SocketTransport;
10
+ /** Shown in the ongoing notification. */
11
+ notificationTitle?: string;
12
+ /** Shown in the ongoing notification body. */
13
+ notificationBody?: string;
14
+ /** Restart tracking after device reboot if it was active. Default true. */
15
+ autoStartOnBoot?: boolean;
16
+ /** Extra headers for REST POST (JSON string map). */
17
+ restHeaders?: Record<string, string>;
18
+ /**
19
+ * Show a map image in the expanded notification (Android only). This is **not** a live MapView:
20
+ * it uses the Google Static Maps API, so you need a key and it refreshes on an interval (see README).
21
+ */
22
+ notificationMapPreview?: boolean;
23
+ /** Google Cloud API key with Static Maps API enabled (billing). */
24
+ googleStaticMapsApiKey?: string;
25
+ /** Optional green “pickup” marker on the static map preview. */
26
+ pickup?: { latitude: number; longitude: number };
27
+ };
28
+
29
+ export type TrackingStatus = {
30
+ active: boolean;
31
+ };
32
+
33
+ export type LocationPayload = {
34
+ latitude: number;
35
+ longitude: number;
36
+ timestamp: number;
37
+ driverId: string;
38
+ accuracy?: number;
39
+ speed?: number;
40
+ bearing?: number;
41
+ };