cordova-plugin-mserviceinterface-2.0 1.0.73 → 1.0.75
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 +4 -3
- package/plugin.xml +7 -1
- package/src/android/GeofenceBroadcastReceiver.java +117 -0
- package/src/android/mInterface.java +87 -2
- package/src/ios/mInterface.h +2 -0
- package/src/ios/mInterface.m +160 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cordova-plugin-mserviceinterface-2.0",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.75",
|
|
4
4
|
"description": "This plugin establishes the connection to the native interface and performs the requested task.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -19,9 +19,10 @@
|
|
|
19
19
|
"url": "https://github.com/selfservit/cordova-plugin-mserviceinterface-2.0/issues"
|
|
20
20
|
},
|
|
21
21
|
"homepage": "https://github.com/selfservit/cordova-plugin-mserviceinterface-2.0#readme",
|
|
22
|
-
"engines": [
|
|
22
|
+
"engines": [
|
|
23
|
+
{
|
|
23
24
|
"name": "cordova",
|
|
24
25
|
"version": ">=5.0.0"
|
|
25
26
|
}
|
|
26
27
|
]
|
|
27
|
-
}
|
|
28
|
+
}
|
package/plugin.xml
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
|
|
3
3
|
xmlns:android="http://schemas.android.com/apk/res/android"
|
|
4
4
|
id="cordova-plugin-mserviceinterface-2.0"
|
|
5
|
-
version="1.0.
|
|
5
|
+
version="1.0.75">
|
|
6
6
|
<name>mserviceinterface-2.0</name>
|
|
7
7
|
<description>This plugin establishes the connection to the native interface and performs the requested task.</description>
|
|
8
8
|
<author>selfservit@gmail.com</author>
|
|
@@ -48,6 +48,7 @@
|
|
|
48
48
|
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
|
|
49
49
|
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
|
|
50
50
|
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
|
|
51
|
+
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
|
|
51
52
|
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
|
|
52
53
|
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
|
|
53
54
|
</config-file>
|
|
@@ -61,6 +62,11 @@
|
|
|
61
62
|
<action android:name="com.example.checkservice" />
|
|
62
63
|
</intent-filter>
|
|
63
64
|
</receiver>
|
|
65
|
+
<receiver android:enabled="true" android:exported="true" android:name="com.selfservit.util.GeofenceBroadcastReceiver">
|
|
66
|
+
<intent-filter>
|
|
67
|
+
<action android:name="com.google.android.gms.location.GeofenceEvent" />
|
|
68
|
+
</intent-filter>
|
|
69
|
+
</receiver>
|
|
64
70
|
<provider android:name="com.selfservit.util.FileProvider" android:authorities="${applicationId}.provider" android:exported="false" android:grantUriPermissions="true">
|
|
65
71
|
<meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/opener_paths" />
|
|
66
72
|
</provider>
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
package com.selfservit.util;
|
|
2
|
+
|
|
3
|
+
import android.app.NotificationChannel;
|
|
4
|
+
import android.app.NotificationManager;
|
|
5
|
+
import android.app.PendingIntent;
|
|
6
|
+
import android.content.BroadcastReceiver;
|
|
7
|
+
import android.content.Context;
|
|
8
|
+
import android.content.Intent;
|
|
9
|
+
import android.os.Build;
|
|
10
|
+
import android.util.Log;
|
|
11
|
+
import android.widget.Toast;
|
|
12
|
+
|
|
13
|
+
import androidx.core.app.NotificationCompat;
|
|
14
|
+
import androidx.core.app.NotificationManagerCompat;
|
|
15
|
+
|
|
16
|
+
import com.google.android.gms.location.Geofence;
|
|
17
|
+
import com.google.android.gms.location.GeofencingEvent;
|
|
18
|
+
import com.selfservit.pegdev.MainActivity;
|
|
19
|
+
import com.selfservit.pegdev.R;
|
|
20
|
+
|
|
21
|
+
import org.json.JSONObject;
|
|
22
|
+
|
|
23
|
+
import java.io.File;
|
|
24
|
+
import java.io.FileOutputStream;
|
|
25
|
+
import java.io.FileWriter;
|
|
26
|
+
import java.io.IOException;
|
|
27
|
+
import java.io.OutputStreamWriter;
|
|
28
|
+
import java.util.UUID;
|
|
29
|
+
|
|
30
|
+
public class GeofenceBroadcastReceiver extends BroadcastReceiver {
|
|
31
|
+
|
|
32
|
+
private static final String TAG = "GeofenceReceiver";
|
|
33
|
+
private static final String CHANNEL_ID = "geofence_channel";
|
|
34
|
+
|
|
35
|
+
@Override
|
|
36
|
+
public void onReceive(Context context, Intent intent) {
|
|
37
|
+
GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);
|
|
38
|
+
|
|
39
|
+
if (geofencingEvent.hasError()) {
|
|
40
|
+
Log.e(TAG, "GeofencingEvent error: " + geofencingEvent.getErrorCode());
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
String enterMessage = intent.getStringExtra("enterMessage");
|
|
44
|
+
String exitMessage = intent.getStringExtra("exitMessage");
|
|
45
|
+
int geofenceTransition = geofencingEvent.getGeofenceTransition();
|
|
46
|
+
if (geofenceTransition == Geofence.GEOFENCE_TRANSITION_ENTER ||
|
|
47
|
+
geofenceTransition == Geofence.GEOFENCE_TRANSITION_EXIT) {
|
|
48
|
+
String transitionType = geofenceTransition == Geofence.GEOFENCE_TRANSITION_ENTER ? "enter" : "exit";
|
|
49
|
+
|
|
50
|
+
for (Geofence geofence : geofencingEvent.getTriggeringGeofences()) {
|
|
51
|
+
String geofenceId = geofence.getRequestId();
|
|
52
|
+
writeEventToJsonFile(context, geofenceId, transitionType);
|
|
53
|
+
createNotificationChannel(context);
|
|
54
|
+
String message = (transitionType.equals("enter") ? enterMessage : exitMessage);
|
|
55
|
+
sendNotification(context, "GeoFence", message);
|
|
56
|
+
// showToast(context, "Geofence " + transitionType + ": " + geofenceId);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
private void showToast(Context context, String message) {
|
|
61
|
+
//Toast.makeText(context.getApplicationContext(), message, Toast.LENGTH_LONG).show();
|
|
62
|
+
}
|
|
63
|
+
private void writeEventToJsonFile(Context context, String geofenceId, String eventType) {
|
|
64
|
+
File notificationsFile, fileDirectory;
|
|
65
|
+
try {
|
|
66
|
+
JSONObject jsonObject = new JSONObject();
|
|
67
|
+
jsonObject.put("eventName", eventType);
|
|
68
|
+
jsonObject.put("geofenceID", geofenceId);
|
|
69
|
+
String[] parameters = geofenceId.split("--");
|
|
70
|
+
fileDirectory = new File(new File(context.getExternalFilesDir(""), "") + "/" + parameters[0] + "/" + parameters[1] + "/" + parameters[2] + "/" + parameters[3] + "/" + "geofence");
|
|
71
|
+
if (!fileDirectory.exists()) {
|
|
72
|
+
boolean success = fileDirectory.mkdirs();
|
|
73
|
+
if (!success) {
|
|
74
|
+
System.out.println("Directory not created");
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
notificationsFile = new File(fileDirectory, geofenceId + "--" + eventType + ".json");
|
|
78
|
+
FileWriter fw = new FileWriter(notificationsFile);
|
|
79
|
+
fw.write(mInterfaceUtil.encrypt(jsonObject.toString()));
|
|
80
|
+
fw.close();
|
|
81
|
+
Log.i(TAG, "Event written to file: " + geofenceId + ".json");
|
|
82
|
+
} catch (Exception e) {
|
|
83
|
+
Log.e(TAG, "Error writing event to JSON file", e);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
private void sendNotification(Context context, String title, String message) {
|
|
88
|
+
Intent intent = new Intent(context, MainActivity.class);
|
|
89
|
+
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
|
|
90
|
+
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
|
|
91
|
+
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context, CHANNEL_ID)
|
|
92
|
+
.setSmallIcon(R.drawable.ic_notification)
|
|
93
|
+
.setContentTitle(title)
|
|
94
|
+
.setContentText(message)
|
|
95
|
+
.setAutoCancel(true)
|
|
96
|
+
.setContentIntent(pendingIntent);
|
|
97
|
+
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
|
|
98
|
+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
|
99
|
+
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, "Channel human readable title", NotificationManager.IMPORTANCE_DEFAULT);
|
|
100
|
+
notificationManager.createNotificationChannel(channel);
|
|
101
|
+
};
|
|
102
|
+
int notificationId = UUID.randomUUID().hashCode();
|
|
103
|
+
notificationManager.notify(notificationId, notificationBuilder.build());
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
private void createNotificationChannel(Context context) {
|
|
107
|
+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
|
108
|
+
CharSequence name = "Geofence Channel";
|
|
109
|
+
String description = "Channel for geofence events";
|
|
110
|
+
int importance = NotificationManager.IMPORTANCE_HIGH;
|
|
111
|
+
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
|
|
112
|
+
channel.setDescription(description);
|
|
113
|
+
NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
|
|
114
|
+
notificationManager.createNotificationChannel(channel);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
@@ -6,6 +6,7 @@ import android.Manifest;
|
|
|
6
6
|
import android.app.Activity;
|
|
7
7
|
import android.app.ActivityManager;
|
|
8
8
|
import android.app.AlertDialog;
|
|
9
|
+
import android.app.PendingIntent;
|
|
9
10
|
import android.app.ProgressDialog;
|
|
10
11
|
import android.content.ContentResolver;
|
|
11
12
|
import android.content.ContentUris;
|
|
@@ -57,6 +58,9 @@ import com.google.android.gms.common.api.GoogleApiClient;
|
|
|
57
58
|
import com.google.android.gms.common.api.PendingResult;
|
|
58
59
|
import com.google.android.gms.common.api.ResultCallback;
|
|
59
60
|
import com.google.android.gms.common.api.Status;
|
|
61
|
+
import com.google.android.gms.location.Geofence;
|
|
62
|
+
import com.google.android.gms.location.GeofencingClient;
|
|
63
|
+
import com.google.android.gms.location.GeofencingRequest;
|
|
60
64
|
import com.google.android.gms.location.LocationRequest;
|
|
61
65
|
import com.google.android.gms.location.LocationServices;
|
|
62
66
|
import com.google.android.gms.location.LocationSettingsRequest;
|
|
@@ -94,6 +98,7 @@ import java.net.URL;
|
|
|
94
98
|
import java.text.SimpleDateFormat;
|
|
95
99
|
import java.util.ArrayList;
|
|
96
100
|
import java.util.Arrays;
|
|
101
|
+
import java.util.Collections;
|
|
97
102
|
import java.util.Date;
|
|
98
103
|
import java.util.List;
|
|
99
104
|
import java.util.Stack;
|
|
@@ -102,6 +107,8 @@ import java.util.zip.ZipEntry;
|
|
|
102
107
|
import java.util.zip.ZipInputStream;
|
|
103
108
|
|
|
104
109
|
import id.zelory.compressor.Compressor;
|
|
110
|
+
import com.selfservit.util.GeofenceBroadcastReceiver;
|
|
111
|
+
|
|
105
112
|
|
|
106
113
|
public class mInterface extends CordovaPlugin implements com.google.android.gms.location.LocationListener, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, ResultCallback<LocationSettingsResult> {
|
|
107
114
|
private static final String TAG = "mInterface";
|
|
@@ -134,6 +141,8 @@ public class mInterface extends CordovaPlugin implements com.google.android.gms.
|
|
|
134
141
|
private static final long FASTEST_INTERVAL = 5 * 60 * 1000;
|
|
135
142
|
public static String attachmentDestPath = "";
|
|
136
143
|
|
|
144
|
+
private GeofencingClient geofencingClient;
|
|
145
|
+
private PendingIntent geofencePendingIntent;
|
|
137
146
|
/* START SERVICE WHEN USER COMES FROM INACTIVE TO ACTIVE STATE IF SERVICE NOT RUNNING */
|
|
138
147
|
@Override
|
|
139
148
|
public void onResume(boolean multitasking) {
|
|
@@ -158,7 +167,28 @@ public class mInterface extends CordovaPlugin implements com.google.android.gms.
|
|
|
158
167
|
if (mInterfaceUtil.packageName.equals("")) {
|
|
159
168
|
mInterfaceUtil.packageName = cordova.getActivity().getApplicationContext().getPackageName().split("\\.")[2];
|
|
160
169
|
}
|
|
161
|
-
if (action.equals("
|
|
170
|
+
if (action.equals("viewRoute")) {
|
|
171
|
+
double endLat = Double.parseDouble(args.getJSONObject(0).optString("destinationLat"));
|
|
172
|
+
double endLng = Double.parseDouble(args.getJSONObject(0).optString("destinationLng"));
|
|
173
|
+
openMaps(endLat, endLng, callbackContext);
|
|
174
|
+
return true;
|
|
175
|
+
} else if (action.equals("addGeoFence")) {
|
|
176
|
+
geofencingClient = LocationServices.getGeofencingClient(cordova.getActivity().getApplicationContext());
|
|
177
|
+
double latitude = Double.parseDouble(args.getJSONObject(0).optString("latitude"));
|
|
178
|
+
double longitude = Double.parseDouble(args.getJSONObject(0).optString("longitude"));
|
|
179
|
+
float radius = Float.parseFloat(args.getJSONObject(0).optString("radius"));
|
|
180
|
+
String geofenceId = args.getJSONObject(0).optString("geofenceId");
|
|
181
|
+
String enterMessage = args.getJSONObject(0).optString("enterNotificationMsg");
|
|
182
|
+
String exitMessage = args.getJSONObject(0).optString("exitNotificationMsg");
|
|
183
|
+
|
|
184
|
+
addGeofence(latitude, longitude, radius, geofenceId, enterMessage, exitMessage, callbackContext, cordova.getActivity().getApplicationContext());
|
|
185
|
+
callbackContext.success("Success");
|
|
186
|
+
return true;
|
|
187
|
+
} else if (action.equals("removeGeofence")) {
|
|
188
|
+
String geofenceId = args.getJSONObject(0).optString("geofenceId");
|
|
189
|
+
removeGeofence(geofenceId, callbackContext);
|
|
190
|
+
return true;
|
|
191
|
+
} else if (action.equals("AppCleanUp")) {
|
|
162
192
|
mInterfaceUtil.appCleanUp(callbackContext, cordova.getActivity().getApplicationContext());
|
|
163
193
|
} else if (action.equals("StartService")) {
|
|
164
194
|
try {
|
|
@@ -966,7 +996,6 @@ public class mInterface extends CordovaPlugin implements com.google.android.gms.
|
|
|
966
996
|
result = new PluginResult(PluginResult.Status.ERROR, "copyAttachmentFile failed, exception is : " + e.getMessage());
|
|
967
997
|
callback.sendPluginResult(result);
|
|
968
998
|
}
|
|
969
|
-
|
|
970
999
|
} else if (action.equals("getMyAccountsData")) {
|
|
971
1000
|
try {
|
|
972
1001
|
String folderPath = mInterfaceUtil.rootDirectory + "/" + args.getJSONObject(0).optString("folderPath");
|
|
@@ -987,7 +1016,56 @@ public class mInterface extends CordovaPlugin implements com.google.android.gms.
|
|
|
987
1016
|
}
|
|
988
1017
|
return true;
|
|
989
1018
|
}
|
|
1019
|
+
private void addGeofence(double latitude, double longitude, float radius, String geofenceId, String enterMessage, String exitMessage, CallbackContext callbackContext, Context context) {
|
|
1020
|
+
Geofence geofence = new Geofence.Builder()
|
|
1021
|
+
.setRequestId(geofenceId)
|
|
1022
|
+
.setCircularRegion(latitude, longitude, radius)
|
|
1023
|
+
.setExpirationDuration(Geofence.NEVER_EXPIRE)
|
|
1024
|
+
.setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_EXIT)
|
|
1025
|
+
.build();
|
|
990
1026
|
|
|
1027
|
+
GeofencingRequest geofencingRequest = new GeofencingRequest.Builder()
|
|
1028
|
+
.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER)
|
|
1029
|
+
.addGeofence(geofence)
|
|
1030
|
+
.build();
|
|
1031
|
+
|
|
1032
|
+
geofencePendingIntent = getGeofencePendingIntent(enterMessage, exitMessage);
|
|
1033
|
+
|
|
1034
|
+
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
|
|
1035
|
+
|
|
1036
|
+
return;
|
|
1037
|
+
}
|
|
1038
|
+
geofencingClient.addGeofences(geofencingRequest, geofencePendingIntent)
|
|
1039
|
+
.addOnSuccessListener(aVoid -> {
|
|
1040
|
+
callbackContext.success("Geofence added successfully");
|
|
1041
|
+
Log.i(TAG, "Geofence added successfully");
|
|
1042
|
+
})
|
|
1043
|
+
.addOnFailureListener(e -> {
|
|
1044
|
+
callbackContext.error("Failed to add geofence");
|
|
1045
|
+
Log.e(TAG, "Failed to add geofence", e);
|
|
1046
|
+
});
|
|
1047
|
+
}
|
|
1048
|
+
private void removeGeofence(String geofenceId, CallbackContext callbackContext) {
|
|
1049
|
+
geofencingClient.removeGeofences(Collections.singletonList(geofenceId))
|
|
1050
|
+
.addOnSuccessListener(aVoid -> {
|
|
1051
|
+
callbackContext.success("Geofence removed successfully");
|
|
1052
|
+
Log.i(TAG, "Geofence removed successfully");
|
|
1053
|
+
})
|
|
1054
|
+
.addOnFailureListener(e -> {
|
|
1055
|
+
callbackContext.error("Failed to remove geofence");
|
|
1056
|
+
Log.e(TAG, "Failed to remove geofence", e);
|
|
1057
|
+
});
|
|
1058
|
+
}
|
|
1059
|
+
private PendingIntent getGeofencePendingIntent( String enterMessage, String exitMessage) {
|
|
1060
|
+
if (geofencePendingIntent != null) {
|
|
1061
|
+
return geofencePendingIntent;
|
|
1062
|
+
}
|
|
1063
|
+
Intent intent = new Intent(cordova.getActivity(), GeofenceBroadcastReceiver.class);
|
|
1064
|
+
intent.putExtra("enterMessage", enterMessage);
|
|
1065
|
+
intent.putExtra("exitMessage", exitMessage);
|
|
1066
|
+
geofencePendingIntent = PendingIntent.getBroadcast(cordova.getActivity(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_MUTABLE);
|
|
1067
|
+
return geofencePendingIntent;
|
|
1068
|
+
}
|
|
991
1069
|
public static ArrayList<JSONObject> getJsonDataFromFiles(String folderPath) {
|
|
992
1070
|
ArrayList<JSONObject> dataArray = new ArrayList<>();
|
|
993
1071
|
try {
|
|
@@ -2472,4 +2550,11 @@ public class mInterface extends CordovaPlugin implements com.google.android.gms.
|
|
|
2472
2550
|
}
|
|
2473
2551
|
return "";
|
|
2474
2552
|
}
|
|
2553
|
+
|
|
2554
|
+
private void openMaps( double endLat, double endLng, CallbackContext callbackContext) {
|
|
2555
|
+
Uri mapUri = Uri.parse("https://www.google.com/maps?daddr=" + endLat + "," + endLng);
|
|
2556
|
+
Intent mapIntent = new Intent(Intent.ACTION_VIEW, mapUri);
|
|
2557
|
+
this.cordova.getActivity().startActivity(mapIntent);
|
|
2558
|
+
callbackContext.success("Google Maps intent launched successfully");
|
|
2559
|
+
}
|
|
2475
2560
|
}
|
package/src/ios/mInterface.h
CHANGED
|
@@ -42,6 +42,7 @@
|
|
|
42
42
|
UIInterfaceOrientation _lastOrientation;
|
|
43
43
|
}
|
|
44
44
|
@property(strong) NSString* callbackID;
|
|
45
|
+
- (void)viewRoute:(CDVInvokedUrlCommand*)command;
|
|
45
46
|
- (void)GetFCMUtils:(CDVInvokedUrlCommand *)command;
|
|
46
47
|
-(void)GetBase64String:(CDVInvokedUrlCommand *)command;
|
|
47
48
|
- (void)ValidateAppStateInfo;
|
|
@@ -109,6 +110,7 @@
|
|
|
109
110
|
- (void)GetSyncIndicator:(CDVInvokedUrlCommand*)command;
|
|
110
111
|
- (void)CopyAttachmentFile:(CDVInvokedUrlCommand*)command;
|
|
111
112
|
- (void)getMyAccountsData:(CDVInvokedUrlCommand*)command;
|
|
113
|
+
- (void)addGeoFence:(CDVInvokedUrlCommand*)command;
|
|
112
114
|
@property (strong, nonatomic) CLLocationManager * locationManager;
|
|
113
115
|
@property (strong, nonatomic) NSMutableArray * coordinatesArray;
|
|
114
116
|
@property (weak, nonatomic) IBOutlet UIButton *btnGallery;
|
package/src/ios/mInterface.m
CHANGED
|
@@ -29,6 +29,166 @@
|
|
|
29
29
|
@synthesize controller = docController;
|
|
30
30
|
@synthesize callbackID;
|
|
31
31
|
|
|
32
|
+
- (void)viewRoute:(CDVInvokedUrlCommand*)command {
|
|
33
|
+
CDVPluginResult* pluginResult = nil;
|
|
34
|
+
|
|
35
|
+
NSDictionary *dict = [[command arguments] objectAtIndex:0];
|
|
36
|
+
NSString* destinationLat = dict[@"destinationLat"];
|
|
37
|
+
NSString* destinationLng = dict[@"destinationLng"];
|
|
38
|
+
|
|
39
|
+
if (destinationLat != nil && destinationLng != nil && [destinationLat length] != 0 && [destinationLng length] != 0) {
|
|
40
|
+
NSString* urlString = [NSString stringWithFormat:@"https://www.google.com/maps?daddr=%@,%@", destinationLat, destinationLng];
|
|
41
|
+
NSURL* url = [NSURL URLWithString:urlString];
|
|
42
|
+
|
|
43
|
+
if ([[UIApplication sharedApplication] canOpenURL:url]) {
|
|
44
|
+
[[UIApplication sharedApplication] openURL:url options:@{} completionHandler:^(BOOL success) {
|
|
45
|
+
if (success) {
|
|
46
|
+
CDVPluginResult *result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"Google Maps launched successfully"];
|
|
47
|
+
[self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
|
|
48
|
+
} else {
|
|
49
|
+
CDVPluginResult *result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Failed to launch Google Maps app"];
|
|
50
|
+
[self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
|
|
51
|
+
}
|
|
52
|
+
}];
|
|
53
|
+
} else {
|
|
54
|
+
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Google Maps app is not installed"];
|
|
55
|
+
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
|
|
56
|
+
}
|
|
57
|
+
} else {
|
|
58
|
+
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Invalid coordinates"];
|
|
59
|
+
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
- (void)addGeoFence:(CDVInvokedUrlCommand *)command {
|
|
63
|
+
NSDictionary *dict = [[command arguments] objectAtIndex:0];
|
|
64
|
+
@try {
|
|
65
|
+
|
|
66
|
+
if (!self.locationManager) {
|
|
67
|
+
self.locationManager = [[CLLocationManager alloc] init];
|
|
68
|
+
self.locationManager.delegate = self;
|
|
69
|
+
[self.locationManager requestAlwaysAuthorization];
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
|
|
73
|
+
[center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert + UNAuthorizationOptionSound)
|
|
74
|
+
completionHandler:^(BOOL granted, NSError * _Nullable error) {
|
|
75
|
+
if (!granted) {
|
|
76
|
+
NSLog(@"Notification permission not granted");
|
|
77
|
+
}
|
|
78
|
+
}];
|
|
79
|
+
|
|
80
|
+
double latitude = [dict[@"latitude"] doubleValue];
|
|
81
|
+
double longitude = [dict[@"longitude"] doubleValue];
|
|
82
|
+
double radius = [dict[@"radius"] doubleValue];
|
|
83
|
+
NSString *identifier = dict[@"geofenceId"];
|
|
84
|
+
NSString *enterMessage = dict[@"enterNotificationMsg"];
|
|
85
|
+
NSString *exitMessage = dict[@"exitNotificationMsg"];
|
|
86
|
+
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
|
|
87
|
+
[defaults setObject:enterMessage forKey:[NSString stringWithFormat:@"%@_enterMessage", identifier]];
|
|
88
|
+
[defaults setObject:exitMessage forKey:[NSString stringWithFormat:@"%@_exitMessage", identifier]];
|
|
89
|
+
[defaults synchronize];
|
|
90
|
+
|
|
91
|
+
CLLocationCoordinate2D centerCoordinate = CLLocationCoordinate2DMake(latitude, longitude);
|
|
92
|
+
CLCircularRegion *region = [[CLCircularRegion alloc] initWithCenter:centerCoordinate radius:radius identifier:identifier];
|
|
93
|
+
region.notifyOnEntry = YES;
|
|
94
|
+
region.notifyOnExit = YES;
|
|
95
|
+
|
|
96
|
+
[self.locationManager startMonitoringForRegion:region];
|
|
97
|
+
|
|
98
|
+
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"Geofence added"];
|
|
99
|
+
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
|
|
100
|
+
} @catch (NSException *exception) {
|
|
101
|
+
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Failed to add geofence"];
|
|
102
|
+
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region {
|
|
107
|
+
if ([region isKindOfClass:[CLCircularRegion class]]) {
|
|
108
|
+
[self handleEventForRegion:(CLCircularRegion *)region eventType:@"enter"];
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region {
|
|
113
|
+
if ([region isKindOfClass:[CLCircularRegion class]]) {
|
|
114
|
+
[self handleEventForRegion:(CLCircularRegion *)region eventType:@"exit"];
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
- (void)handleEventForRegion:(CLCircularRegion *)region eventType:(NSString *)eventType {
|
|
119
|
+
NSLog(@"Handling event for region: %@ with event type: %@", region.identifier, eventType);
|
|
120
|
+
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
|
|
121
|
+
NSString *messageKey = [NSString stringWithFormat:@"%@_%@Message", region.identifier, eventType];
|
|
122
|
+
NSString *customMessage = [defaults stringForKey:messageKey];
|
|
123
|
+
UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
|
|
124
|
+
content.title = @"Geofence Event";
|
|
125
|
+
content.body = customMessage ? customMessage : [NSString stringWithFormat:@"Geofence %@: %@", eventType, region.identifier];
|
|
126
|
+
content.sound = [UNNotificationSound defaultSound];
|
|
127
|
+
|
|
128
|
+
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:[[NSUUID UUID] UUIDString] content:content trigger:nil];
|
|
129
|
+
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
|
|
130
|
+
[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
|
|
131
|
+
if (error != nil) {
|
|
132
|
+
NSLog(@"Error adding notification request: %@", error);
|
|
133
|
+
} else {
|
|
134
|
+
NSLog(@"Notification request added successfully");
|
|
135
|
+
}
|
|
136
|
+
}];
|
|
137
|
+
|
|
138
|
+
NSArray *components = [region.identifier componentsSeparatedByString:@"--"];
|
|
139
|
+
NSString *clientID = components[1];
|
|
140
|
+
NSString *countryCode = components[2];
|
|
141
|
+
NSString *userID = components[3];
|
|
142
|
+
|
|
143
|
+
NSString *directory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
|
|
144
|
+
NSString *baseFolder = [NSString stringWithFormat:@"%@/%@/%@/%@/%@/geofence", directory, [self GetAppName], clientID, countryCode,userID];
|
|
145
|
+
|
|
146
|
+
NSString *fileName = [NSString stringWithFormat:@"%@--%@.json", region.identifier, eventType];
|
|
147
|
+
|
|
148
|
+
[self writeEventToJsonFileWithGeofenceId:region.identifier eventType:eventType folderPath:baseFolder fileName:fileName];
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
- (void)writeEventToJsonFileWithGeofenceId:(NSString *)geofenceId eventType:(NSString *)eventType folderPath:(NSString *)folderPath fileName:(NSString *)fileName {
|
|
152
|
+
NSDictionary *event = @{
|
|
153
|
+
@"eventName": eventType,
|
|
154
|
+
@"geofenceID": geofenceId
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
NSError *error;
|
|
158
|
+
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:event options:NSJSONWritingPrettyPrinted error:&error];
|
|
159
|
+
|
|
160
|
+
if (!jsonData) {
|
|
161
|
+
NSLog(@"Error creating JSON: %@", error.localizedDescription);
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
NSString *filePath = [folderPath stringByAppendingPathComponent:fileName];
|
|
165
|
+
NSFileManager *fileManager = [NSFileManager defaultManager];
|
|
166
|
+
if (![fileManager fileExistsAtPath:folderPath]) {
|
|
167
|
+
[fileManager createDirectoryAtPath:folderPath withIntermediateDirectories:YES attributes:nil error:&error];
|
|
168
|
+
if (error) {
|
|
169
|
+
NSLog(@"Error creating directory: %@", error.localizedDescription);
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
BOOL success = [jsonData writeToFile:filePath atomically:YES];
|
|
174
|
+
|
|
175
|
+
if (success) {
|
|
176
|
+
NSLog(@"Successfully wrote event to file: %@", filePath);
|
|
177
|
+
} else {
|
|
178
|
+
NSLog(@"Failed to write event to file: %@", filePath);
|
|
179
|
+
}
|
|
180
|
+
NSData *jsonData1 = [NSData dataWithContentsOfFile:filePath options:NSDataReadingMappedIfSafe error:&error];
|
|
181
|
+
|
|
182
|
+
if (!jsonData1) {
|
|
183
|
+
NSLog(@"Error reading JSON file: %@", error.localizedDescription);
|
|
184
|
+
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// Convert JSON data to a string
|
|
188
|
+
NSString *jsonString = [[NSString alloc] initWithData:jsonData1 encoding:NSUTF8StringEncoding];
|
|
189
|
+
NSLog(@"content: %@", jsonString);
|
|
190
|
+
|
|
191
|
+
}
|
|
32
192
|
- (void)GetFCMUtils:(CDVInvokedUrlCommand *)command {
|
|
33
193
|
OSDeviceState *deviceState = [OneSignal getDeviceState];
|
|
34
194
|
NSMutableDictionary * deviceDict = [[NSMutableDictionary alloc] init];
|