Nativescript plugin for MDK app to send location updates in background to SAP.
https://blog.nativescript.org/nativescript-7-for-plugin-authors/
Navigate to publish and run the below command to generate plugin
./pack.sh
sh ./pack.sh
In Command prompt / Terminal navigate to your application root folder and run:
tns plugin add /Users/mithunk/Desktop/Nativescript/ScheduledLocUpdate/publish/package/nativescript-scheduled-location-update-1.0.2.tgz
In app delegate import plugin and EventHandler
import * as locationUpdate from "nativescript-scheduled-location-update";
import { EventHandler } from 'mdk-core/EventHandler';
Under application did finish launching function add the below code which sends EventHandler to plugin.
let scheduledLocManager = locationUpdate.getInstance();
let handler = new EventHandler();
scheduledLocManager.setEventHandler(handler)
Prepare the project before generating iPA file.
To start the location updates trigger the below function, by passing location time interval and action path to update location.
scheduledLocManager.startUpdatingLocation(60, "/SAPAssetManager/Actions/Location/ZLocationCreateOnlineService.action");
To stop the location updates trigger the below function.
scheduledLocManager.stopUpdatingLocation();
var appSettings = require('application-settings');
import Logger from '../Log/Logger';
import * as locationUpdate from "nativescript-scheduled-location-update";
/**
* Describe this function...
* @param {IClientAPI} context
*/
export default function ZChangeClockInOutStatus(context) {
let isKeyExists = appSettings.hasKey("isClockIn");
let isClockIn = appSettings.getBoolean("isClockIn");
let scheduledLocManager = locationUpdate.getInstance();
return scheduledLocManager.requestAuthorization().then(result => {
if(result){
Logger.info('SAM: Authorised Location');
if(isKeyExists && isClockIn){
Logger.info('isAuthorized: Stopping Location Service');
context.getPageProxy().setToolbarItemCaption('IssuePartTbI', context.localizeText('clock_in'));
scheduledLocManager.stopUpdatingLocation();
return appSettings.setBoolean("isClockIn", false);
}else{
context.getPageProxy().setToolbarItemCaption('IssuePartTbI', context.localizeText('clock_out'));
Logger.info('isAuthorized: Starting Location Service');
scheduledLocManager.startUpdatingLocation(60, "/SAPAssetManager/Actions/Location/ZLocationCreateOnlineService.action");
return appSettings.setBoolean("isClockIn", true);
}
}else{
Logger.info('SAM: Not Authorised Location');
}
});
}
Add the below keys to externals in WebIDE before deploying the app.
application-settings;nativescript-scheduled-location-update
Add the below keys to app plist
NSLocationWhenInUseUsageDescription
NSLocationAlwaysAndWhenInUseUsageDescription
Add the following background modes in Xcode
Add the following lines to Info.plist
located at the path app/App_Resources/iOS/Info.plist
<key>UIBackgroundModes</key>
<array>
<string>fetch</string>
<string>location</string>
<string>processing</string>
</array>
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string/>
Method | Description |
---|---|
isAuthorized(): boolean |
Checks for location authorization status |
public requestAuthorization(): Promise<boolean> |
Requests for location permission |
startUpdatingLocation(interval: number, action: string ): void |
Starts the Location Update and sends location in specified interval to SAP by triggering OData Action |
stopUpdatingLocation(): void |
Stops location update |
setEventHandler(handler: any): void |
To send EventHandler to plugin from AppDelegate |
Start Location tracking immidiatly after granting/accepting location permissionsSpelling corrections in API- Check with SAP regarding “EventHandler” import in AppDelegate
- Test the plugin in multiple scenarios