For React Native apps
To integrate the new methods into your existing React Native code, you'll need to follow a similar pattern to what has already been done with existing methods like addFutureTrackTag in both iOS and Android.
iOS Bridge for NotificationCenter Observer
First, let's add the observer in your Swift code for iOS. You'll need to define a method that will be called when RPTrackerDidChangeActivity notification is observed. Then, you'll expose this method to React Native.
1. Swift Implementation
Modify your existing Swift class (presumably TelematicsSdk.swift) to include the observer setup and the corresponding method:
import Foundation
import React
@objc(TelematicsSdk)
class TelematicsSdk: NSObject {
override init() {
super.init()
NotificationCenter.default.addObserver(
self,
selector: #selector(observeTracker(_:)),
name: .RPTrackerDidChangeActivity,
object: nil
)
}
@objc func observeTracker(_ notification: Notification) {
// Process the notification and send event to React Native
if let userInfo = notification.userInfo as? [String: Any] {
// Assuming you want to send some data to React Native
sendEvent(withName: "onTrackerChange", body: userInfo)
}
}
// ... existing methods ...
// This method is required to send events to React Native
func sendEvent(withName name: String, body: Any?) {
if let bridge = self.bridge {
bridge.eventDispatcher().sendAppEvent(withName: name, body: body)
}
}
}
In this code, observeTracker is the method that will be called when the RPTrackerDidChangeActivity notification is observed. The sendEvent method is used to send data back to React Native.
2. React Native Integration
On the JavaScript side, you need to listen for the event
import { NativeEventEmitter, NativeModules } from 'react-native';
const { TelematicsSdk } = NativeModules;
const telematicsSdkEmitter = new NativeEventEmitter(TelematicsSdk);
const subscription = telematicsSdkEmitter.addListener(
'onTrackerChange',
(trackerInfo) => {
console.log('Tracker Info:', trackerInfo);
// Process the tracker info as needed
}
);
// Remember to unsubscribe when the component unmounts
// subscription.remove();
Android Bridge for Tracking State Listener
For Android, you'll need to set up a bridge method that registers the TrackingStateListener callback and sends the tracking state changes back to React Native.
1. Java Implementation
Modify your existing Java class (presumably TelematicsSdkModule.java) to include the tracking state listener setup:
import com.facebook.react.bridge.*;
import com.facebook.react.modules.core.DeviceEventManagerModule;
public class TelematicsSdkModule extends ReactContextBaseJavaModule {
// ... existing methods ...
@ReactMethod
public void startTrackingListener(Promise promise) {
TrackingStateListener callback = new TrackingStateListener() {
@Override
public void onStopTracking() {
sendEvent(getReactApplicationContext(), "onStopTracking", null);
}
@Override
public void onStartTracking() {
sendEvent(getReactApplicationContext(), "onStartTracking", null);
}
};
try {
TrackingApi.getInstance().registerCallback(callback);
promise.resolve(null);
} catch (Exception e) {
promise.reject("Error", "Failed to register tracking listener");
}
}
private void sendEvent(ReactContext reactContext, String eventName, @Nullable WritableMap params) {
reactContext
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit(eventName, params);
}
}
In this code, startTrackingListener is a method that sets up the TrackingStateListener and sends events back to React Native.
2. React Native Integration
Similar to iOS, set up the listener in your JavaScript code:
import { NativeEventEmitter, NativeModules } from 'react-native';
const { TelematicsSdkModule } = NativeModules;
const telematicsSdkEmitter = new NativeEventEmitter(TelematicsSdkModule);
// Start the tracking listener
TelematicsSdkModule.startTrackingListener()
.then(() => {
console.log('Tracking listener started');
})
.catch((error) => {
console.error('Failed to start tracking listener:', error);
});
const subscriptionStart = telematicsSdkEmitter.addListener(
'onStartTracking',
() => {
console.log('Tracking started');
// Handle tracking start
}
);
const subscriptionStop = telematicsSdkEmitter.addListener(
'onStopTracking',
() => {
console.log('Tracking stopped');
// Handle tracking stop
}
);
// Remember to unsubscribe when the component unmounts
// subscriptionStart.remove();
// subscriptionStop.remove();
In both platforms, ensure that you handle the lifecycle of your components correctly, unsubscribing from events when components unmount to prevent memory leaks and other issues.
Updated 9 months ago