Appcelerator Android module wrapping Geofence functionalities
This module adds support for using Geofence in Titanium Apps. It can monitor all geofence transitions and fire a local notification as well. You can customize the notification in order to fit your needs. This module works when the app is in foreground, background, closed, after phone restart, App data cleared and if location providers get changed.
Will work only with 7+
Download the Ti.Android.Geofence module through the here.
Try this module.
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.
All your files are listed in the file explorer. You can switch from one to another by clicking a file in the list.
| 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 |
You can delete the current file by clicking the Remove button in the file explorer. The file will be moved into the Trash folder and automatically deleted after 7 days of inactivity.
| Attributes | Description |
|---|---|
| id | (Boolean) - clear all existing fences before adding new ones |
| type | (String) - |
| 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. |
| 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.
This method will clear all existing fences already being monitored. It will also stop the monitoring service.
This method will just stop the monitoring service for the current fences. It will NOT clear the existing fences already being monitored.
These events can only be monitored when your app is in foreground or in background. They will never fire when you app you 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. |
| ERROR | When a error occur on the process |
You can extract them from Ti.PlayServices
These can be found at "/Library/Application Support/Titanium/mobilesdk/osx/7.0.0.GA/android"
