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

Monitoring beacons with app closed - IOS #259

Closed
codingjam opened this issue Oct 13, 2016 · 9 comments
Closed

Monitoring beacons with app closed - IOS #259

codingjam opened this issue Oct 13, 2016 · 9 comments

Comments

@codingjam
Copy link

I have seen many questions here related to this topic, but I haven't found an answer. This is a great plugin, it works as expected in foreground and background, but I can't get it to Monitor when app is closed. According to the Apple documentation, Monitoring for beacons is allowed in background and when app is killed/closed. When you kill the app, the iOS monitors for beacon regions that your app created and wakes up your app for few seconds in background when you enter/exit the beacon region. I have seen this working fine on Xamarin and native apps that I built before, but I can't get it work using this plugin on Cordova. The plugins documentation also says "Region Monitoring (or geo fencing), works in all app states." for IOS. I saw a couple of posts here where people said they were able to Monitor with app closed.

I have all the right permissions requestWhenInUseAuthorization and requestAlwaysAuthorization, Background App Refresh is on. I am also using local-notifications plugin. I am using the 3.4.1 version of this plugin and running iOS 10.0.2,

Here are other existing related issues I have looked into: #227 #224 #247

Can someone provide some direction on this? How can I get it to Monitor beacons with app closed on ios?

@codingjam
Copy link
Author

codingjam commented Oct 18, 2016

UPDATE- It automatically started to work. The app is closed, I didn't make any changes to plugin version or iOS and it suddenly started working. I left the phone next to the beacon and forgot about it for few minutes and I did see my application executing the code I had in region enter event. Once it started working it has been working consistently so after that..so far. I have tried it many times before over last few days and did not work. But the question is why it wasn't working before!? It's really strange!

@RikdeVos
Copy link

RikdeVos commented Nov 28, 2016

Hi,

I'm facing exactly the same issue. I set up an environment which simply makes an http call to my server on didEnterRegion and didExitRegion events and I turn my ibeacon on/off every few minutes. In background mode it works for a couple of minutes (5-10), after that it stops making requests to my server and logging the events. However, in some cases it continued to work for hours at a time!

Like @codingjam I'm running requestWhenInUseAuthorization and requestAlwaysAuthorization which asks the user permission, and I've got (most of) the Background Modes checked in Capabilities in XCode.

Have you by any chance figured out what was causing iOS to stop calling the event handlers?

This is the code I am using:

    document.addEventListener("deviceready", function () {

        function lan_log(name, obj) {
            if(typeof(obj) == typeof(undefined)) {
                obj = {};
            }
            console.log(name, obj);
            var obj_str = JSON.stringify(obj);
            $http({
                method: 'POST',
                url: 'http://192.168.2.58:8888/log/log.php?name='+name+'&obj='+encodeURIComponent(obj_str),
                data: {
                    name: name,
                    obj: obj_str
                }
            }).then(function() {
                console.log('logged success');
            })
        }

        var delegate = new cordova.plugins.locationManager.Delegate();

        delegate.didDetermineStateForRegion = function (pluginResult) {
            lan_log('didDetermineStateForRegion: ', pluginResult);
        };

        delegate.didStartMonitoringForRegion = function (pluginResult) {
            lan_log('didStartMonitoringForRegion:', pluginResult);
        };

        delegate.didRangeBeaconsInRegion = function (pluginResult) {
            lan_log('didRangeBeaconsInRegion: ', pluginResult);
        };

        delegate.didEnterRegion = function(pluginResult) {
            lan_log('Entered region: ', pluginResult);
        };

        delegate.didExitRegion = function(pluginResult) {
            lan_log('Exited region: ', pluginResult);
        };

        var uuid = 'a546722b-c9e2-4a07-8763-d1c78d3b48f1';
        var major = '2';
        var minor = '1';
        var identifier = 'test';
        var beaconRegion = new cordova.plugins.locationManager.BeaconRegion(identifier, uuid, major, minor, true);

        lan_log("Going to monitor region now...");
        cordova.plugins.locationManager.setDelegate(delegate);

        // required in iOS 8+
        cordova.plugins.locationManager.requestWhenInUseAuthorization();
        cordova.plugins.locationManager.requestAlwaysAuthorization();
        // or cordova.plugins.locationManager.requestAlwaysAuthorization()

        cordova.plugins.locationManager.startMonitoringForRegion(beaconRegion)
            .fail(function(e) { lan_log("Error while trying to start monitoring",e); })
            .done();
    });

@codingjam
Copy link
Author

By background you mean app killed or the app is just pushed to the background by pressing home button? I was having trouble with Monitoring with app killed. It was working fine for me when the app is in background. And I still don't know why it started working. I just refreshed the app on my phone and it started working for me. Are you on iOS 10? I don't know what exactly are you facing, but people have reported some inconsistent behavior with ibeacon on ios 10 #254

@RikdeVos
Copy link

Hi, I think I've managed to fix the issue. Make sure to place the code inside the .run function of app.js, or put it in a provider/service and call it from .run. This is essential as controllers are not properly loaded by ios when it starts in background mode. Previously I was loading the code from within the controller, but this fails intermittently.

Now I'm able to scan for beacons even if I kill the app myself. After killing, it starts right up in the background in about 1-2 minutes. I'm using iOS 10.1.1 and version 3.4.1 (the latest) of this ibeacon plugin.

@kevinleequinn
Copy link

kevinleequinn commented Dec 2, 2016

hi, I cannot get iOS to run my delegate.didDetermineStateForRegion function upon that first startup in background. for testing I have set my app to only set the delegate ondeviceready so I am sure it is the fastest possible app you can have.

but sure enough, after I kill the app, the first time I enter a region, nothing happens. second and third times happen like a charm because the app was already running in background and therefore had primed the delegate. #269

@RikdeVos
Copy link

RikdeVos commented Dec 2, 2016

Make sure you wait about 1-2 minutes after you're entering/exiting the region or killing the app. Also check that you're placing the code inside the .run and not a controller, as controllers don't always init in the background mode.

@kevinleequinn
Copy link

got things working @RikdeVos, thx. #269

but what is this ".run" you speak of? is that some sort of Angular thing?

@RikdeVos
Copy link

RikdeVos commented Dec 2, 2016

Great, yes if you were using Ionic (which uses Angular JS) by coincidence then that would be applicable, otherwise forget what I said :)

@petermetz
Copy link
Owner

Please check out v3.6.1, the latest release as of right now.
Thank you again for @Chuckytuh for the contribution of the fix.

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

No branches or pull requests

4 participants