I am trying to get the Cordova cordova-plugin-geofence Plugin to work. My testing device is a Google Nexus 5 (Android). I followed the steps from the documentation (https://github.com/cowbell/cordova-plugin-geofence). It also said I needed Google Play Services on my device. I have that (8.1.15).
I
Maybe I am forgetting something, but if I do please tell me. The only file I edited was index.js. And from that file I edited onDeviceReady and receivedEvent:
onDeviceReady: function() {
app.receivedEvent('deviceready');
window.geofence.initialize(function() {
alert('success');
}, function(err) {
alert(err);
});
},
receivedEvent: function(id) {
var parentElement = document.getElementById(id);
var listeningElement = parentElement.querySelector('.listening');
var receivedElement = parentElement.querySelector('.received');
listeningElement.setAttribute('style', 'display:none;');
receivedElement.setAttribute('style', 'display:block;');
console.log('Received Event: ' + id);
//add geofence
window.geofence.addOrUpdate({
id: 'abc',
latitude: 51.808944,
longitude: 4.667041,
radius: 3,
transitionType: 3,
notification: {
id: 1,
title: 'Welcome',
text: 'Hello',
smallIcon: 'file://img/icon.png',
icon: 'file://img/logo.png',
openAppOnClick: false,
vibration: [2000],
data: {
name: 'foo',
lastName: 'bar'
}
}
}).then(function() {
console.log('Geofence successfully added');
alert('Geofence successfully added');
}, function(reason) {
console.log('Adding geofence failed', reason);
alert('Adding geofence failed: ' + reason);
});
//listen for geofences
window.geofence.onTransitionReceived = function(geofences) {
geofences.forEach(function(geo) {
console.log('Geofence transition detected', geo);
alert('Geofence transition detected: ' + geo);
});
};
}
I received the two alerts (success and Geofence successfully added).
Try with example application https://github.com/cowbell/ionic-geofence
Also this plugin uses Google Geofence API https://developer.android.com/training/location/geofencing.html and it is not accurate to the level of few meters. Try increase the radius, 3 meters is not enough to catch any transition, try with 100 instead.
For testing I am using Lockito Fake GPS, in order to make it work you need to allow location mocking on your device.