我正在試圖讓Cordova cordova-plugin-geofence插件正常工作。我的測試設備是Google Nexus 5(Android)。我遵循文檔中的步驟(https://github.com/cowbell/cordova-plugin-geofence)。它還表示我需要在我的設備上使用Google Play服務。我有(8.1.15)。無法讓Cordova cordova-plugin-geofence插件正常工作
我
- 插件安裝到一個乾淨的PhoneGap項目。
- 在deviceready事件之後初始化插件。
- 添加了地理圍欄並開始監聽地理圍欄過渡。
- 走進和離開地理圍欄,但沒有發生任何事情。
也許我忘記了一些東西,但如果我確實請告訴我。我編輯的唯一文件是index.js。並從該文件我編輯onDeviceReady和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);
});
};
}
我收到的兩個警報(成功,併成功添加地理柵欄)。
我把半徑增加到了30米。然後,我離開了半徑再回到它,但我沒有收到通知。我也使用模擬位置。之後,我嘗試了您的示例應用程序。我得到了死亡的白色屏幕。當進一步調查爲什麼我得到死亡白屏時,我看到我錯過了模塊烤麪包機。控制檯錯誤:https://gyazo.com/b843825c2505e3d2a60db3058715e34b – SPottuit
我看到缺角的toster。你是否在開始應用程序之前運行'bower install'和'npm install'? – tsubik
您應該考慮的警告也很少。首先,如果您的應用程序未在Android的前臺運行(但如果設置了geofence中的通知對象,您應該獲得本地通知),則不會獲得任何JavaScript代碼回調。第二個30米仍然可能是太小的半徑,這個插件只是使用谷歌的API,但不是微博。 – tsubik