First you need to require the module
var geofence = require("ti.android.geofence");
You need to ask for Geolocation permissions in order to work with this module
if (!Titanium.Geolocation.hasLocationPermissions(Titanium.Geolocation.AUTHORIZATION_ALWAYS)) {
Titanium.API.warn('Location permissions not granted! Asking now...');
Titanium.Geolocation.requestLocationPermissions(Titanium.Geolocation.AUTHORIZATION_ALWAYS, function(e) {
if (!e.success) {
Titanium.API.error('Location permissions declined!');
} else {
Titanium.API.info('Location permissions ready');
//Initialize monitoring here
}
});
} else {
Titanium.API.warn('Location permissions already granted!');
//Initialize monitoring here
}
Then you need to add the fences
geofence.addGeofences({
clearExistingFences : false,
fences : [
{
"id" : "google",
"latitude" : 37.422196,
"longitude" : -122.084004,
"radius" : 1000,
"transitions" : [
geofence.GEOFENCE_TRANSITION_ENTER
],
//Notification
"title" : "Google - Mountain View",
"sound" : "notification",
"accentColor" : "#E65100",
"type" : geofence.TYPE_PLACE_FENCE,
"showGooglePlaceBigImage" : true,
//"bigImage" : "https://lh3.googleusercontent.com/jOsYWBsr1muoRiMQFW9EU-ZqSCtfLBibu6S2g4nIbihP0SYL4Em6VD20WuieL1h5bBzbSrnIYVQZy5lhjUSR"
},
{
"id" : "tesla",
"latitude" : 37.394834,
"longitude" : -122.150046,
"radius" : 700,
"transitions" : [
geofence.GEOFENCE_TRANSITION_ENTER,
geofence.GEOFENCE_TRANSITION_DWELL
],
"dwellTime" : 5 * 60 * 1000,
//Notification
"title" : "Tesla HQ",
"sound" : "notification",
"accentColor" : "#E65100",
"type" : geofence.TYPE_PLACE_FENCE,
"showGooglePlaceBigImage" : true,
//"bigImage" : "https://static.wixstatic.com/media/92734c_31512f187c9241149ba53ee30e7ca7f7~mv2.jpg_256"
},
{
"id" : "apple",
"latitude" : 37.331829,
"longitude" : -122.029749,
"radius" : 1000,
"transitions" : [
geofence.GEOFENCE_TRANSITION_DWELL
],
"dwellTime" : 3 * 60 * 1000,
//Notification
"title" : "Apple Infinite Loop",
"sound" : "notification",
"accentColor" : "#E65100",
"type" : geofence.TYPE_PLACE_FENCE,
"showGooglePlaceBigImage" : true,
//"bigImage" : "https://img.evbuc.com/https%3A%2F%2Fcdn.evbuc.com%2Fimages%2F38585705%2F47533216579%2F1%2Foriginal.jpg?h=512&w=512&auto=compress&rect=0%2C40%2C500%2C250&s=c87e485256c42bd0c9f181eb9c371a3f"
}
]
});
Now you are good to go!
geofence.startMonitoring();
Please take a look at the full example of the module being used.
Methods | Description |
---|---|
addGeofences | Passes the fences to the module |
startMonitoring | Start monitoring the fences for the chosen transitions |
clearExistingFences | Clear all fences and stops monitoring |
stopMonitoring | Just stops monitoring without clearing the fences |
This method will add all fences you need to the module. Remember that Google Geofence can monitor only 100 fences at a time.
Attributes | Description |
---|---|
clearExistingFences | (Boolean) - clear all existing fences before adding new ones |
fences | (Array) - List of fences objects |
service | (String) - This is javascript written service you would like to run when a Geofence transition event gets fired. Example : com.myproject.geofence.myServiveService |
Attributes | Description |
---|---|
id | (String) - an unique id of your choosing |
type | (String) - the type is a free string in case you want to have more control of your fences. For example : "standard", "master_fence" or "fence_updater" |
latitude | (Number) - latitude of fence center |
longitude | (Number) - longitude of fence center |
radius | (Number) - fence radius |
dwellTime | (Number) - the amount of time the user has to remain inside the fence. The time is MILLISECONDS. |
canNotify | (Boolean) - If you want the notication to be shown immediately when Geofence events get fired. Note : If you want to fire the notification yourself (via the method fireNotification()), set canNotify to false, but don't forget to fill all notification attributes when creating the fence, otherwise the module will not be able to show the notification. |
transitions | (Array) - You can monitor different types of transition for different fences. You can monitor all of them at once or only the ones you need. The possibilities are : geofence.GEOFENCE_TRANSITION_ENTER, geofence.GEOFENCE_TRANSITION_EXIT and geofence.GEOFENCE_TRANSITION_DWELL |
title | (String) - Notification - The notification's title |
message | (String) - Notification - The notification's text content (excluding the title) |
ledColor | (String) - Notification - Certain Android hardware devices have LED notification lights built-in, allowing notifications to trigger a colored notification light on a device upon receipt. Notification colors are set using ARGB Hex values (Ex: #FFD50000). Doc here |
accentColor | (String) - Notification - Android Accent Color. Colors are set using ARGB Hex values (Ex: #FFD50000) Doc here |
smallIcon | (String) - Notification - Small Icon. It has to be a local image ("@drawable/large_icon"). Doc here |
largeIcon | (String) - Notification - Large Icon. Can either be a local image ("@drawable/large_icon") or url ("https://image/large_icon.png") [Doc here] |
bigImage | (String) - Notification - big image url. Doc here(https://documentation.onesignal.com/docs/customize-notification-icons) |
showGooglePlaceBigImage | (Boolean) - Notification - If you want to display a big image on the notification but do not have one. This will show a picture of the place taken from Google Places API. |
The transition type indicating that the user enters and dwells in geofences for a given period of time. If GEOFENCE_TRANSITION_ENTER is also specified, this alert will always be sent after the GEOFENCE_TRANSITION_ENTER alert.
Transitions | Integer Values |
---|---|
GEOFENCE_TRANSITION_ENTER | 1 |
GEOFENCE_TRANSITION_EXIT | 2 |
GEOFENCE_TRANSITION_DWELL | 4 |
This method will check your geofences and start monitoring for the transitions you want. Once you use this method, the fences will be monitored until told otherwise. Your fences will be re-added whenever the user restarts it phone or change the location provider.
Clears all existing fences already being monitored. It will also stop the monitoring service.
Just stops the monitoring service for the current fences. It will NOT clear the existing fences already being monitored.
Returns the data from the latest fired event in json like this.
{
"fences":[
{
"latitude":"28.41259",
"id":"huda_metro",
"longitude":"77.0425996"
}
],
"event":"ENTERED"
}
Fires a local notification with fence information if there is a Geofence transition waiting to be fired.
These events can only be monitored when your app is in foreground or in background. They will never fire when your app is closed because the instance of your application does not exist.
Events | Description |
---|---|
ENTERED | When user enters a geofence |
EXIT | When user exits a geofence |
DWELL | When user remains inside a geofence for the specified period of time |
STARTED_MONITORING | When the Android starts monitoring for your fences |
STOPPED_MONITORING | When the Android stops monitoring for your fences |
GEOFENCES_ADDED | When the fences get added to the module |
NOTIFICATION_CLICKED | When user clicks on the notification generated by the GEOFENCE_TRANSITION_ENTER, GEOFENCE_TRANSITION_EXIT or GEOFENCE_TRANSITION_DWELL transitions. |
NOTIFICATION_FIRED_IN_BACKGROUND | When the app is in background (not killed) when the notification gets fired |
NOTIFICATION_FIRED_IN_FOREGROUND | When the app is in foreground (not killed) when the notification gets fired |
ERROR | When a error occur on the process |
If you want to execute a javascript code whenever the ENTERED, EXIT or DWELL events get fired. This is how:
1 ) Create a file name myService.js and write your backgroundService code. Place it in Resources folder.
//EXAMPLE
Ti.API.info('IT WORKED! It is a service');
var geofence = require("ti.android.geofence");
var geoTriggers = geofence.getLastestFiredGeofenceTransitionData();
if(geoTriggers.fences) {
var gLength = geoTriggers.fences.length;
for (var i=0; i < gLength; i++) {
if(geoTriggers.fences[i].id == "huda_metro" && geoTriggers.event == geofence.ENTERED){
geofence.fireNotification();
}
};
} else {
Titanium.API.warn("No fences for this event!");
};
<android xmlns:android="http://schemas.android.com/apk/res/android">
<manifest>
<!-- YOUR MANIFEST CODES HERE -->
</manifest>
<services>
<service type="interval" url="myService.js"/>
</services>
</android>
Before moving on and start using your service you will need to re-compile your project. After recompiling your project, open your "YOUR_PROJECT_FOLDER"/build/android/AndroidManifest.xml. Look for your service name and you will find its full name, something like "com.myproject.geofence.MyServiceService". This is important as Titanium generates this name. To learn more about Android Services please read the documentation here.
geofence.addGeofences({
clearExistingFences : false,
fences : ["YOUR_FENCES"],
service : "com.myproject.geofence.MyServiceService" //THIS IS IT!
});
You can extract them from Ti.PlayServices
These can be found at "/Library/Application Support/Titanium/mobilesdk/osx/8.0.0.GA/android"