cordova-plugin-ibeacon icon indicating copy to clipboard operation
cordova-plugin-ibeacon copied to clipboard

Add support for notifyOnEntry, notifyOnExit parameters

Open petermetz opened this issue 10 years ago • 13 comments

CLRegion.h defines these parameters below. Goal: Expose these properties for the Javascript runtime via the plugin.

/*
 *  notifyOnEntry
 *
 *  Discussion:
 *    App will be launched and the delegate will be notified via locationManager:didEnterRegion:
 *    when the user enters the region. By default, this is YES.
 */
@property (nonatomic, assign) BOOL notifyOnEntry __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_7_0);

/*
 *  notifyOnExit
 *
 *  Discussion:
 *    App will be launched and the delegate will be notified via locationManager:didExitRegion:
 *    when the user exits the region. By default, this is YES.
 */
@property (nonatomic, assign) BOOL notifyOnExit __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_7_0);


petermetz avatar Apr 02 '14 19:04 petermetz

Is it possible to create a local notification then? That would be really awesome (:

simkli avatar Apr 07 '14 17:04 simkli

It is possible but for the notification you will need to utilize another Cordova plugin.

petermetz avatar Apr 07 '14 17:04 petermetz

Would completing this ticket be needed in order to use this plugin to configure my application to wake from a closed or background state in response to entering an iBeacon region?

morrissinger avatar Aug 29 '14 04:08 morrissinger

Hi @morrissinger,

No, that should work out of the box with the current codebase. I'm writing should, because I only ever tested it with circular regions but that's definitely working and so should with beacons too.

Peter

petermetz avatar Aug 29 '14 08:08 petermetz

@petermetz , first of all, thank you for being so responsive.

I'm a little confused about how to do this from the examples in your readme. I am looking under the "Start Monitoring for a Single iBeacon" section. If I call startMonitoringForRegion() in your example, is that going to automatically start region monitoring even when the app is closed?

morrissinger avatar Aug 29 '14 13:08 morrissinger

@morrissinger, yepp that's how it should work. From the calling side it should be transparent whether the app was running or not. Theoretically... :-)

petermetz avatar Aug 29 '14 16:08 petermetz

@petermetz -- awesome. Thanks for your help. I'll post an update with my results.

morrissinger avatar Aug 29 '14 19:08 morrissinger

I tried this out by using the PhoneGap local notification plugin (https://github.com/katzer/cordova-plugin-local-notifications) to generate location notifications when I entered and exited regions, and I can confirm that even with the app completely closed (i.e., double-tap home button and swipe app up), and the phone not active (i.e., screen is black), this plugin is able to do region monitoring. Excellent.

N.B.: I have not tested on Android.

morrissinger avatar Aug 29 '14 22:08 morrissinger

@morrissinger, Can you please share some snippet on how you are implementing in the call of startMonitoringForRegion()? It seems to be not working with the same.

didEnterRegion: function(pluginResult) { window.plugin.notification.local.add({ message: 'Exit!' }); } Is this necessary to run the app in the background to make it work(notify on entering a region)? Or I am doing something wrong here. Also, I do see, didStartMonitoringForRegion is giving same pluginresult when beacon is off and on. Appreciate your response....Thanks

Ranjan-2014 avatar Sep 01 '14 20:09 Ranjan-2014

@petermetz -- question: given that these two properties (notifyOnEntry and notifyOnExit) are not exposed to the JavaScript side of the plugin presently, what values are being supplied to these properties by default?

The reason I ask is thatI have set up an app with your plugin and all it does is set a local notifications whenever the phone enters or exits a region. I have also installed the Locate app by Radius (https://itunes.apple.com/us/app/locate-beacon/id738709014?mt=8), which ostensibly does the same thing. I have both apps listening to my Estimote iBeacons, and yet, with both apps closed, Locate appears to pick up the iBeacons far more frequently and also picks seems to pick them up every time I turn on the screen to my iPhone.

With the Locate app operating as a sort of control in this experiment, one would think that the same iPhone would detect the same iBeacons at approximately the same times. Yet this is not the case. Thus, I am looking for anything that may be configured differently between the two and these two properties caught my eye.

morrissinger avatar Sep 02 '14 13:09 morrissinger

@morrissinger

By default, both notifyOnEntry and notifyOnExit are set to YES, according to the documentation of Apple (see the description of this issue for reference).

The difference between Locate Beacon and the Cordova iBeacon plugin is definitely not because of the two mentioned properties, since they are both enabled by default.

My guess is that Locate Beacon might be using the iOS Core Bluetooth API under the hood, to be a bit more sophisticated than the simplistic Core Location API (which is used by this plugin).

This is just pure speculation though, the only surefire way to tell if you go and ask Radius Networks about their code. :-)

Peter

petermetz avatar Sep 04 '14 18:09 petermetz

Ransome14: Here is what I did to get it working. Note, I needed to use the localnotification plugin as well to ensure that I could actually receive an event with the app closed. An alert() is not sufficient. You need to use a method that iOS provides for actually delivering a message to a user when an app is closed. Local notifications are a perfect method for this.

        var createLocalNotification = function (message) {
            window.plugin.notification.local.add({
                title:   'iBeaconSpike1',
                message: message
            });
        };

        var delegate = new cordova.plugins.locationManager.Delegate().implement({

            didDetermineStateForRegion: function (pluginResult) {
                createLocalNotification('[DOM] didDetermineStateForRegion: ' + JSON.stringify(pluginResult));
            },

            didStartMonitoringForRegion: function (pluginResult) {
                createLocalNotification('didStartMonitoringForRegion:' + JSON.stringify(pluginResult));
            },

            didRangeBeaconsInRegion: function (pluginResult) {
                createLocalNotification('[DOM] didRangeBeaconsInRegion: ' + JSON.stringify(pluginResult));
            }

        });

        var uuid = 'MY UUID';
        var identifier = 'JUST AN IDENTIFIER PASSED IN ON THE MESSAGE';
        var minor = /* MY MAJOR */;
        var major = /* MY MINOR */;
        var beaconRegion = new cordova.plugins.locationManager.BeaconRegion(identifier, uuid, major, minor);

        cordova.plugins.locationManager.setDelegate(delegate);
        cordova.plugins.locationManager.startMonitoringForRegion(beaconRegion)
                .fail(console.error)
                .done();

morrissinger avatar Sep 12 '14 13:09 morrissinger

Hi I monitor a single beacon. The didDetermineStateForRegion with notification works fine. Now i want to notify the user just one time if he enters the region - just to say "hello please open your app now" - and only if he is about 4meters away from the beacon. The user should be notified if the app is closed - is that possible? something like:

     delegate.didDetermineStateForRegion = function (pluginResult) {
            //window.alert('[DOM] didDetermineStateForRegion: ' + JSON.stringify(pluginResult));
           if ( notAlreadyNotified == true && distanceToBeacon < 4 meters && appIsInBackground)
            {
                createLocalNotification('Welcome to our company please open your app ' + JSON.stringify(pluginResult.state));notAlreadyNotified = false;

            }               
        };

sebastianh84 avatar Feb 24 '15 08:02 sebastianh84