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

iOS montioring when the app is killed/in background mode does not work #57

Open
oferRounds opened this issue Oct 15, 2017 · 28 comments
Open

Comments

@oferRounds
Copy link

oferRounds commented Oct 15, 2017

Version

1.0.7

Platform

iOS

OS version

iOS 11.0.2

Steps to reproduce


listener = DeviceEventEmitter.addListener(
        'beaconsDidRange',
        (data) => {

          PushNotification.localNotificationSchedule({
            message: 'Did Detect Beacon', // (required)
            date: new Date(Date.now())
          })
        }
)

Beacons.requestAlwaysAuthorization()
Beacons.startMonitoringForRegion(region)
Beacons.startRangingBeaconsInRegion(region)
Beacons.startUpdatingLocation()

Expected behavior

Beacon should be detected also when the app is killed/in background mode

Actual behavior

The listener callback does not get called (it does get called on foreground)

@oferRounds
Copy link
Author

oferRounds commented Oct 15, 2017

I did try to add:
Beacons.setBackgroundBetweenScanPeriod(1)
which using it I sometimes get push on background (I get inconsistent behaviour – sometimes I do get, sometimes I don’t), but still - not at all when the app is killed.

Also, I now get the following warning:

TypeError:_reactNativeBeaconsManager2.default.setBckgroundBetweenScanPeriod is not a function`

@MacKentoch
Copy link
Owner

MacKentoch commented Oct 15, 2017

Thank you @oferRounds for this detailed issue.

Master branch contains a merged PR to try to fix these inconsistencies in iOS (drop of DeviceEventEmitter in favor of Beacons.BeaconsEventEmitter: see CHANGELOG).

Would you give a try (hoping it solves it)?

Get master branch version:

npm install git+https://github.com/MacKentoch/react-native-beacons-manager.git

@eurobob
Copy link

eurobob commented Oct 15, 2017 via email

@oferRounds
Copy link
Author

@MacKentoch - thanks! Do you mean it suppose to suppose to solve the background/app killed issue?

@eurobob - oh, you’re right! Sorry for this :(

@eurobob
Copy link

eurobob commented Oct 16, 2017

@oferRounds I was having issues with detection when the app is killed but have been having much more consistent behaviour with a small addition to the library. Try the code from my pull request and see how it works:

#58

Be aware that you should be able to detect when monitoring and ranging is working, but if you are in range of the beacon when starting the app you will not trigger the entry event from monitoring. I'm trying to find a workaround for this now.

@oferRounds
Copy link
Author

oferRounds commented Oct 16, 2017

Replied directly on issue #58.

So it now does works great for me in the background/foreground, but not when the app is killed. The problems are the app either only wakes up once, when it enters the region (i.e., I would like to have the control to detect the proximity and only do something on an immediate event) or it doesn’t wake up at all.

@eurobob I saw some posts that mention that when the app wakes up, in the didFinishLaunchingWithOptions we should reactivate the location manager. Is that true from what you aware of? If so, how can get the location manager’s instance?

@oferRounds
Copy link
Author

@MacKentoch - I did the update, but I get the error: Property 'BeaconsEventEmitter' does not exist on type 'typeof 'react-native-beacons-manager'.

Any idea?

@eurobob
Copy link

eurobob commented Oct 16, 2017

Can you share more code from the file you're working in?

@oferRounds
Copy link
Author

oferRounds commented Oct 17, 2017

Thanks! Sure:

import { DeviceEventEmitter, EmitterSubscription, Alert, Platform, AppState } from 'react-native'
import Beacons from 'react-native-beacons-manager'
import { toggleClock } from '../actions'
const PushNotification = require('react-native-push-notification')

const region = {
  identifier: ***,
  uuid: ***,
  major: ***,
  minor: ***
}

let didEnterRegionListener: EmitterSubscription = null

export default class TimeLogService {

  static trackUserLocation(dispatch: any) {

    if (didEnterRegionListener === null) {
      didEnterRegionListener = Beacons.BeaconsEventEmitter.addListener(
        'regionDidEnter',
        () => {

          if (AppState.currentState === 'active') {
            Alert.alert(
              'Toggle clock sent.',
              '',
              [
                {text: 'OK', onPress: () => console.log('Cancel Pressed'), style: 'cancel'}
              ],
              { cancelable: false }
            )
          } else {
            PushNotification.localNotification({
              message: 'Toggle clock sent.'
            })
          }

          dispatch(toggleClock())
        }
      )

      if (Platform.OS === 'ios') {
        Beacons.requestAlwaysAuthorization()

        Beacons.startMonitoringForRegion(region)
        Beacons.startRangingBeaconsInRegion(region)

        Beacons.startUpdatingLocation()
      } else {
        Beacons.detectIBeacons()
        try {
          Beacons.startRangingBeaconsInRegion(region)
          console.log(`Beacons ranging started succesfully!`)
        } catch (error) {
          console.log(`Beacons ranging not started, error: ${error}`)
        }
      }
    }
  }
}

@eurobob
Copy link

eurobob commented Oct 17, 2017

It sounds like you might still be using v1.0.7? Or are you installing the package directly from the git repository?

@oferRounds
Copy link
Author

oferRounds commented Oct 17, 2017

I followed your suggestion and installed it by:
npm install git+https://github.com/MacKentoch/react-native-beacons-manager.git

@eurobob
Copy link

eurobob commented Oct 17, 2017

I'm not sure what could be the problem then. Just to be thorough, maybe unlink it and uninstall the npm package, then try again?

You can also use my fork to include this pull request:

npm install git+https://github.com/eurobob/react-native-beacons-manager.git

@oferRounds
Copy link
Author

That’s an idea! I’ll do that.

@MacKentoch
Copy link
Owner

MacKentoch commented Oct 18, 2017

PR #58 is merged in master CHANGELOG

@eurobob
Copy link

eurobob commented Oct 18, 2017

@oferRounds going back to the initial problem, i've found that in debugging mode sometimes the app can take a long time to load (bad wifi or whatever), which eats into the time window allocated to take action on the region entry from the monitoring. Perhaps you could try a production build to see if it works that way?

@sign2k
Copy link

sign2k commented Oct 25, 2017

We had the same problem.

Adding
self.locationManager.allowsBackgroundLocationUpdates = YES;
to the init method of RNIBeacon.m solved the problem.

https://developer.apple.com/documentation/corelocation/cllocationmanager/1620568-allowsbackgroundlocationupdates

maybe some function like

RCT_EXPORT_METHOD(allowsBackgroundLocationUpdates:(BOOL)allow)
{
  self.locationManager.allowsBackgroundLocationUpdates = allow;
}

would be helpful. This would allow the developer to control, when ranging in background really needed.

@MacKentoch
Copy link
Owner

Thank you @sign2k for sharing.

This is definitely a good fix to add.

@MacKentoch
Copy link
Owner

As suggested by @sign2k, I added:

/**
 * set background location updates to ensure monitoring when app is killed or in background mode
 *
 * @param {boolean} [allow=false] allow or disallow background modes
 */
function allowsBackgroundLocationUpdates(allow: boolean = false): void {
  BeaconsManager.allowsBackgroundLocationUpdates(allow);
}

Please note this requires iOS9+ (it should not be a problem, since iPhone owners should be at iOS10 for more than 90%)

@oferRounds
Copy link
Author

oferRounds commented Nov 6, 2017

@MacKentoch, @sign2k

I’m facing the issue also in Android. I.e, on both platform, when the app is killed, the beacon is sometimes not detected and the app is not woken up. Otherwise — when in foreground or background - it works great.

Two questions please:

  1. Is using allowsBackgroundLocationUpdates should affect monitoring also? Anyhow, I’ll test it now and follow up with results.
  2. Any solution to Android?

@oferRounds
Copy link
Author

@eurobob any findings perhaps from your tests?

@oferRounds
Copy link
Author

oferRounds commented Nov 7, 2017

On iOS - I just added the code to AppDelegate.m according to Estimote guide, and now it works! Even when the app was killed by the user.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{ 
  if([launchOptions objectForKey:@"UIApplicationLaunchOptionsLocationKey"])
  {
    self.beaconManager = [ESTBeaconManager new];
    self.beaconManager.delegate = self;
    // don't forget the NSLocationAlwaysUsageDescription in your Info.plist
    [self.beaconManager requestAlwaysAuthorization];
    [self.beaconManager startMonitoringForRegion:[[ESTBeaconRegion alloc]
                                                  initWithProximityUUID:ESTIMOTE_PROXIMITY_UUID
                                                  identifier:@"AppRegion"]];
  }
  return YES;
}

-(void)beaconManager:(ESTBeaconManager *)manager didEnterRegion:(ESTBeaconRegion *)region
{
  UILocalNotification *notification = [[UILocalNotification alloc] init];
  notification.alertBody = @"Enter region";
  notification.soundName = UILocalNotificationDefaultSoundName;

  [[UIApplication sharedApplication] presentLocalNotificationNow:notification];
}
 
-(void)beaconManager:(ESTBeaconManager *)manager didExitRegion:(ESTBeaconRegion *)region
{
  UILocalNotification *notification = [[UILocalNotification alloc] init];
  notification.alertBody = @"Exit region";
  notification.soundName = UILocalNotificationDefaultSoundName;
 
  [[UIApplication sharedApplication] presentLocalNotificationNow:notification];
}

Do we want to address this in the library?

@oferRounds
Copy link
Author

oferRounds commented Nov 7, 2017

As for Android, I see in Estimote docs that the following scenario is indeed not handled

Keep in mind that if the app is killed by device memory requirements, it is possible to carry out background monitoring to relaunch it. 
However, if the user killes the app background monitoring won't work.

Any idea for a workaround?

@oferRounds
Copy link
Author

I see that Estimote added this feature in their new coming up SDK.

See here: https://forums.estimote.com/t/alpha-4-monitoring/7516

And here: Estimote/Android-Fleet-Management-SDK#62 (comment)

Can we add something like this here also? What do you think?

@innyso
Copy link

innyso commented Dec 12, 2017

Hi All,

I enabled background update by setting BeaconsManager.allowsBackgroundLocationUpdates(true) in the beginning of componentWillMount

I would like to know if this suppose to work for only single region or multi region as I noticed when I am sending ranging information from multi region I will only get one region update instead of all of it.
Or if I am doing something wrong so not all then not all region ranging update are send while in phone is locked.

Thank you :D

@d0uub
Copy link

d0uub commented Aug 7, 2018

@MacKentoch , i tried v1.1 and background scanning not work. (i am in debug mode) do you have any idea?

version:
"react": "16.3.1",
"react-native": "0.55.4",
"react-native-beacons-manager": "git+https://github.com/MacKentoch/react-native-beacons-manager.git"

Source:

import React, { Component } from 'react';
import {
Platform,
StyleSheet,
Text,
View
} from 'react-native';
import Beacons from 'react-native-beacons-manager';

const instructions = Platform.select({
ios: 'Press Cmd+R to reload,\n' +
'Cmd+D or shake for dev menu',
android: 'Double tap R on your keyboard to reload,\n' +
'Shake or press menu button for dev menu',
});

type Props = {};
export default class App extends Component {
componentWillMount() {
Beacons.allowsBackgroundLocationUpdates(true)
Beacons.requestAlwaysAuthorization()
Beacons.shouldDropEmptyRanges(true);
const region = {
identifier: ***,
uuid: ***
};
Beacons.startMonitoringForRegion(region) // or like < v1.0.7: .startRangingBeaconsInRegion(identifier, uuid)
.then(() => console.warn('Beacons monitoring started succesfully'))
.catch(error =>
console.warn(Beacons monitoring not started, error: ${error}),
);
Beacons.startRangingBeaconsInRegion(region) // or like < v1.0.7: .startRangingBeaconsInRegion(identifier, uuid)
.then(() => console.warn('Beacons ranging started succesfully'))
.catch(error =>
console.warn(Beacons ranging not started, error: ${error}),
);
Beacons.startUpdatingLocation()

}

componentDidMount() {

this.authStateDidRangeEvent = Beacons.BeaconsEventEmitter.addListener(
  'authorizationStatusDidChange',
  info => console.warn('authorizationStatusDidChange: ', info),
);

// Ranging: Listen for beacon changes
this.beaconsDidRangeEvent = Beacons.BeaconsEventEmitter.addListener(
  'beaconsDidRange',
  data => {
    console.warn(data)
  }
);

// monitoring events
this.regionDidEnterEvent = Beacons.BeaconsEventEmitter.addListener(
  'regionDidEnter',
  ({ uuid, identifier }) => {
   console.warn('monitoring - regionDidEnter data: ', { uuid, identifier });
 }

);

this.regionDidExitEvent = Beacons.BeaconsEventEmitter.addListener(
'regionDidExit',
({ identifier, uuid, minor, major }) => {
console.warn('monitoring - regionDidExit data: ', { identifier, uuid, minor, major });
}
);
}

render() {
return (


Welcome to React Native!


To get started, edit App.js


{instructions}


);
}
}

const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
},
});

@d0uub
Copy link

d0uub commented Aug 8, 2018

I just solve it by change the privacy.
as your suggested, both "location always use" + "location when in use" description must fill something to make it works in the plist!

@Gillinghammer
Copy link

We had the same problem.

Adding
self.locationManager.allowsBackgroundLocationUpdates = YES;
to the init method of RNIBeacon.m solved the problem.

https://developer.apple.com/documentation/corelocation/cllocationmanager/1620568-allowsbackgroundlocationupdates

maybe some function like

RCT_EXPORT_METHOD(allowsBackgroundLocationUpdates:(BOOL)allow)
{
  self.locationManager.allowsBackgroundLocationUpdates = allow;
}

would be helpful. This would allow the developer to control, when ranging in background really needed.

I was not able to see ranging in the background until I found your post. Thank you fine sir, this is exactly what I needed!

@ger86
Copy link

ger86 commented Jan 16, 2019

@oferRounds , can you show me what code did you use in your AppDelegate? Because I think you have pasted the one from Estimote but the needed for this library will be different. Thanks!

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

8 participants