Skip to content

Commit 2a2a4bd

Browse files
authored
Add example of how to integrate clevertap-geofence-sdk (#195)
1 parent b1f3729 commit 2a2a4bd

File tree

4 files changed

+103
-0
lines changed

4 files changed

+103
-0
lines changed

CTExample/Assets/Plugins/Android/AndroidManifest.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@
1717
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
1818
<uses-permission android:name="android.permission.WAKE_LOCK" />
1919

20+
<!-- Permissions for Geofence -->
21+
<!--
22+
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
23+
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
24+
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
25+
-->
26+
2027
<uses-feature android:glEsVersion="0x00020000" />
2128

2229
<uses-feature
@@ -37,6 +44,7 @@
3744
android:label="@string/app_name"
3845
android:theme="@style/UnityThemeSelector">
3946

47+
<!-- For Geofence example use anroid:name="com.clevertap.unity.example.GeofenceExampleActivity" -->
4048
<activity
4149
android:name="com.clevertap.unity.CleverTapOverrideActivity"
4250
android:configChanges="mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|orientation|screenLayout|uiMode|screenSize|smallestScreenSize|fontScale"

CTExample/Assets/Plugins/Android/mainTemplate.gradle

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ dependencies {
2020
// Optional CleverTap push templates
2121
implementation 'com.clevertap.android:push-templates:1.2.4'
2222

23+
// Optional CleverTap Geofence libraries
24+
// implementation 'com.clevertap.android:clevertap-geofence-sdk:1.3.0'
25+
// implementation 'com.google.android.gms:play-services-location:21.0.0'
26+
// implementation 'androidx.work:work-runtime:2.7.1' // required for FETCH_LAST_LOCATION_PERIODIC
27+
// implementation 'androidx.concurrent:concurrent-futures:1.1.0' // required for FETCH_LAST_LOCATION_PERIODIC
28+
2329
// Optional AndroidX Media3 Libraries for CleverTap Audio/Video Inbox Messages.
2430
// Audio/Video messages will be dropped without these dependencies
2531
// implementation "androidx.media3:media3-exoplayer:1.1.1"
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package com.clevertap.unity.example;
2+
3+
import static com.clevertap.android.geofence.Logger.DEBUG;
4+
5+
import android.Manifest;
6+
import android.content.pm.PackageManager;
7+
import android.location.Location;
8+
import android.os.Bundle;
9+
import android.widget.Toast;
10+
11+
import androidx.annotation.NonNull;
12+
import androidx.core.app.ActivityCompat;
13+
import androidx.core.content.ContextCompat;
14+
15+
import com.clevertap.android.geofence.CTGeofenceAPI;
16+
import com.clevertap.android.geofence.CTGeofenceSettings;
17+
import com.clevertap.android.geofence.interfaces.CTGeofenceEventsListener;
18+
import com.clevertap.android.geofence.interfaces.CTLocationUpdatesListener;
19+
import com.clevertap.android.sdk.CleverTapAPI;
20+
import com.clevertap.unity.CleverTapOverrideActivity;
21+
22+
import org.json.JSONObject;
23+
24+
public class GeofenceExampleActivity extends CleverTapOverrideActivity implements CTGeofenceAPI.OnGeofenceApiInitializedListener, CTGeofenceEventsListener, CTLocationUpdatesListener {
25+
26+
private CTGeofenceAPI geofenceAPI;
27+
28+
@Override
29+
protected void onCreate(Bundle savedInstanceState) {
30+
super.onCreate(savedInstanceState);
31+
geofenceAPI = CTGeofenceAPI.getInstance(getApplicationContext());
32+
geofenceAPI.setOnGeofenceApiInitializedListener(this);
33+
geofenceAPI.setCtGeofenceEventsListener(this);
34+
geofenceAPI.setCtLocationUpdatesListener(this);
35+
}
36+
37+
@Override
38+
protected void onResume() {
39+
super.onResume();
40+
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION
41+
) == PackageManager.PERMISSION_GRANTED) {
42+
initGeofenceApi();
43+
} else {
44+
ActivityCompat.requestPermissions(
45+
this,
46+
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
47+
1
48+
);
49+
}
50+
}
51+
52+
@Override
53+
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
54+
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
55+
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
56+
initGeofenceApi();
57+
}
58+
}
59+
60+
private void initGeofenceApi() {
61+
CleverTapAPI cleverTapAPI = CleverTapAPI.getDefaultInstance(this);
62+
CTGeofenceSettings ctGeofenceSettings = new CTGeofenceSettings.Builder()
63+
.setLogLevel(DEBUG)
64+
.build();
65+
geofenceAPI.init(ctGeofenceSettings, cleverTapAPI);
66+
}
67+
68+
@Override
69+
public void OnGeofenceApiInitialized() {
70+
Toast.makeText(this, "Geofence Api Initialized", Toast.LENGTH_SHORT).show();
71+
}
72+
73+
@Override
74+
public void onGeofenceEnteredEvent(JSONObject geofenceEnteredEventProperties) {
75+
Toast.makeText(this, "Geofence Entered", Toast.LENGTH_SHORT).show();
76+
}
77+
78+
@Override
79+
public void onGeofenceExitedEvent(JSONObject geofenceExitedEventProperties) {
80+
Toast.makeText(this, "Geofence Exited", Toast.LENGTH_SHORT).show();
81+
}
82+
83+
@Override
84+
public void onLocationUpdates(Location location) {
85+
Toast.makeText(this, "Location update " + location, Toast.LENGTH_SHORT).show();
86+
}
87+
}

CTExample/Assets/Plugins/Android/src/com/clevertap/unity/example/GeofenceExampleActivity.java.meta

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)