Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Execute code on enter/exit fence? #35

Open
dylanvdmerwe opened this issue Apr 2, 2015 · 13 comments
Open

Execute code on enter/exit fence? #35

dylanvdmerwe opened this issue Apr 2, 2015 · 13 comments

Comments

@dylanvdmerwe
Copy link

Is it possible to run some code when a user enters or exists a fence? For example I want to call a REST method which lets the server know what has happened. This can then be used for really cool things such as sending a push message to the user, etc etc.

For example:

window.geofence.addOrUpdate({
    id: "69ca1b88-6fbe-4e80-a4d4-ff4d3748acdb",
    latitude: 50.2980049,
    longitude: 18.6593152,
    radius: 3000,
    transitionType: TransitionType.ENTER,
    notification: {
        id: 1,
        title: "Welcome in Gliwice",
        text: "You just arrived to Gliwice city center.",
        openAppOnClick: true
    },
    action: function(id, transitionType) {
        // perform some code such as as calling a REST web service
    }
}).then(function() {
    console.log('Geofence successfully added');
}, function(reason) {
    console.log('Adding geofence failed', reason);
})
@tsubik
Copy link
Member

tsubik commented Apr 2, 2015

Only by receiveTransition event so far. Take a look here. It's not gonna work when your app is closed though.

@dylanvdmerwe
Copy link
Author

There must be a way to get a customization action to execute when a geofence event happens though? Natively this can happen on both iOS and Android as far as I know.

@jgcaruso
Copy link

jgcaruso commented May 7, 2015

I issued a pull request that was merged a few days ago that allows this for iOS. It uses NotificationCenter to send a message to anyone who is registered to listen to it. Check it out, there are details on how to implement it (Objective-c code only, but Swift shouldn't be too hard to figure out): #43

I'll be working on Android support for this in a little while.

@tsubik
Copy link
Member

tsubik commented Jan 22, 2016

Example how to do this in native code is here. I would say this is similar to #53 and #86.

@johnrobertcobbold
Copy link

This is already do-able with this plugin (at least on iOS, haven't started working on Android yet).

@frmi
Copy link

frmi commented Feb 23, 2016

I have tested it on Android and it seems to work aswell without native code

@rebebop
Copy link

rebebop commented Feb 23, 2016

@frmi Can you post your code? It isn't working form me. Also, the problem is not that it doesn't work at all but it doesn't work when your app is in background (for android).

@frmi
Copy link

frmi commented Feb 23, 2016

@razagill i'm sorry, i was wrong this does not work if the app is forcefully closed, since the webview will be null.

@johnrobertcobbold
Copy link

@razagill "since the webview will be null" - is that the reason on Android ? When the app was forcefully closed, iOS re-opens the app on a geofence event and therefore recreates the webview .

@rebebop
Copy link

rebebop commented Feb 24, 2016

@johnrobertcobbold Not exactly, you can't run javascript code in the background on android. So you have to modify the plugin and add new class that extends the BroadcastReceiver (details are on the main page of this plugin), I was able to catch the transitions in the background after this modification.

@johnrobertcobbold
Copy link

So basically like iOS using the new wkWebView... oh great :-/

@frmi
Copy link

frmi commented Feb 24, 2016

@razagill @johnrobertcobbold The plugin is still alive even if you forcefully kill the app. The notification is successfully tiggered, but the onTransitionReceived callback cannot be executed since the cordova web view has been released by closing the app.
02-24 12:24:49.309 711-972/com.frmi.app D/GeofencePlugin﹕ Transition Event Received! 02-24 12:24:49.309 711-972/com.frmi.app D/GeofencePlugin﹕ Webview is null

I guess there is no way around the broadcast receiver for now.

@Wade-BuildOtto
Copy link

their must be a method to call a custom url scheme, rather than setting up a notification, it seems like you have to setup everything you need to when you create the fence and the system takes over.

maybe use it in conjunction with https://github.com/EddyVerbruggen/Custom-URL-scheme
then capture the data from the url and execute the code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants