androidcordovacordova-pluginsvisual-studio-cordovageofencing

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


This is asked previously but not in cordova.

Hi all, I want to get alert when user enters the geofence region and also want alert when user exit from the geofence so that I can make entry.

it should work on all cases foreground, background, and even when the app is killed

I'm getting alert when user enters but not when user exits from region.

Any help would be really appreciated .

CODE:

  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 () { });    
    })

Transition callback function: which is getting called only only when i am in within region, it is not called when i'm out of the region

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

Solution

  • Where using this plugin : https://github.com/cowbell/cordova-plugin-geofence and depending on you'r needs, be carefull about the following :

    Javascript background execution

    This is known limitation. When in background your app may/will be suspended to not use system resources. Therefore, any javascript code won't run, only background services can run in the background. Local notification when user crosses a geofence region will still work, but any custom javascript code won't. If you want to perform a custom action on geofence crossing, try to write it in native code.

    We can see this exemple into the plugin documention :

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

    And if we search into the plugin code we found this (www/TransitionType.js) :

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

    So you have to check if this work :

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

    EDIT 1

    After adding your code to your primary code, I noticed two things :

    First, the documentation specifies that when you want to add several geofences at once you must do so from an array and therefore with several parameters. It may be nothing but it's better to trust the documentation.

    Then, the documentation also specifies

    Geofence overrides the previously one with the same id.

    And that exactly what you do That may be why the event can't work properly.

    If I follow the documentation correctly, you should have something that looks like this :

    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
        });
    };