首页 文章

无法获得Cordova cordova-plugin-geofence插件

提问于
浏览
2

我想让Cordova cordova-plugin-geofence插件工作 . 我的测试设备是Google Nexus 5(Android) . 我按照文档中的步骤(https://github.com/cowbell/cordova-plugin-geofence) . 它还说我需要在我的设备上使用Google Play服务 . 我有(8.1.15) .

一世

  • 将插件安装到一个干净的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);
        });
    };
}

我收到了两个警报(成功和Geofence成功添加) .

1 回答

相关问题