在 cordova 中触发地理围栏退出通知时如何获得警报

how to get alert when geofence exit notification is fired in cordova

之前有人问过这个问题,但在 cordova 中没有。

大家好,我想在用户进入地理围栏区域时收到警报,也希望在用户离开地理围栏区域时收到警报,以便我可以进入。

它应该适用于所有前台、后台甚至应用被终止的情况

我在用户进入区域时收到警报,但在用户退出区域时不会收到警报。

任何帮助将不胜感激。

代码:

  window.geofence.addOrUpdate({
        id: "69ca1b88-6fbe-4e80-a4d4-ff4d3748acdb",
        latitude: xx.12345,
        longitude: xx.12345,
        radius: 100,
        transitionType:1,
    notification: {
            id: 1,
            title: "Welcome!",
            text: "In.",
            openAppOnClick: true
        } 
    }, {
            id: "69ca1b88-6fbe-4e80-a4d4-ff4d3748acdc",
            latitude: xx.12345,
            longitude: xx.12345,
            radius: 100,
            transitionType:2,
            notification: {
                id: 1,
                title: "Bye!",
                text: "Out.",
                openAppOnClick: true
            }
        }).then(function () {
     navigator.notification.alert('successfully added', function () { });   
    }, function (reason) {
        navigator.notification.alert('failed', function () { });    
    })

转换回调函数: 仅当我在区域内时才会被调用,当我不在该区域时不会被调用

window.geofence.onTransitionReceived = function (geofences) {
        alert(JSON.stringify(geofences));
}

在何处使用此插件:https://github.com/cowbell/cordova-plugin-geofence 根据您的需要,请注意以下事项:

Javascript后台执行

这是已知限制。 当处于后台时 您的应用程序may/will 被暂停以不使用系统资源。 因此,任何javascript代码都不会运行,只有后台服务才能在后台运行。用户穿过地理围栏区域时的本地通知仍然有效,但任何自定义 javascript 代码都不会。如果您想对地理围栏交叉执行自定义操作,try to write it in native code.

我们可以在插件文档中看到这个例子:

window.geofence.onTransitionReceived = function (geofences) {
    geofences.forEach(function (geo) {
        console.log('Geofence transition detected', geo);
    });
};

如果我们搜索插件代码,我们会发现这个 (www/TransitionType.js) :

var TransitionType = {
    ENTER: 1,
    EXIT: 2,
    BOTH: 3,
};

所以你必须检查这个是否有效:

window.geofence.onTransitionReceived = function (geofences) {
    geofences.forEach(function (geo) {
        if (geo.TransitionType === 2 ) {
           // Do what you want
        }
    });
};

编辑 1

将您的代码添加到主代码后,我注意到两件事:

首先,文档指定当您想一次添加多个地理围栏时,您必须从一个数组中进行,因此需要使用多个参数。 可能没什么,但最好相信文档。

然后,文档还指定

Geofence overrides the previously one with the same id.

这正是您所做的 这可能就是活动无法正常进行的原因。

如果我正确地按照文档进行操作,您应该得到如下所示的内容:

window.geofence.addOrUpdate({
    id: "69ca1b88-6fbe-4e80-a4d4-ff4d3748acdb",
    latitude: xx.12345,
    longitude: xx.12345,
    radius: 100,
    transitionType: 3, // Both (Enter and Exit)
    notification: {
            id: 1,
            title: "Welcome!",
            text: "In.",
            openAppOnClick: true
        } 
    }
).then(function () {
    navigator.notification.alert('successfully added', function () { });   
}, function (error) {
    navigator.notification.alert('failed', function () { });
});

window.geofence.onTransitionReceived = function (geofences) {
    geofences.forEach(function (geo) {
        console.log('Geofence transition detected', geo);
        // Do what you want
    });
};