react-native-beacons-manager icon indicating copy to clipboard operation
react-native-beacons-manager copied to clipboard

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

Open oferRounds opened this issue 6 years ago • 28 comments

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 avatar Oct 15 '17 03:10 oferRounds

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`

oferRounds avatar Oct 15 '17 05:10 oferRounds

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

MacKentoch avatar Oct 15 '17 16:10 MacKentoch

I think the issue here is a typo. Check the spelling of background in the method you are calling!

On 16 Oct 2017, at 00:43, Erwan DATIN [email protected] wrote:

Thank you @oferRounds for this details 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 — You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or mute the thread.

eurobob avatar Oct 15 '17 22:10 eurobob

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

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

oferRounds avatar Oct 16 '17 06:10 oferRounds

@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:

https://github.com/MacKentoch/react-native-beacons-manager/pull/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.

eurobob avatar Oct 16 '17 08:10 eurobob

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 avatar Oct 16 '17 11:10 oferRounds

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

Any idea?

oferRounds avatar Oct 16 '17 11:10 oferRounds

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

eurobob avatar Oct 16 '17 14:10 eurobob

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

oferRounds avatar Oct 17 '17 13:10 oferRounds

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

eurobob avatar Oct 17 '17 13:10 eurobob

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

oferRounds avatar Oct 17 '17 13:10 oferRounds

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

eurobob avatar Oct 17 '17 13:10 eurobob

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

oferRounds avatar Oct 17 '17 13:10 oferRounds

PR #58 is merged in master CHANGELOG

MacKentoch avatar Oct 18 '17 04:10 MacKentoch

@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?

eurobob avatar Oct 18 '17 08:10 eurobob

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.

sign2k avatar Oct 25 '17 11:10 sign2k

Thank you @sign2k for sharing.

This is definitely a good fix to add.

MacKentoch avatar Oct 25 '17 20:10 MacKentoch

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%)

MacKentoch avatar Oct 27 '17 04:10 MacKentoch

@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 avatar Nov 06 '17 07:11 oferRounds

@eurobob any findings perhaps from your tests?

oferRounds avatar Nov 07 '17 07:11 oferRounds

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 avatar Nov 07 '17 12:11 oferRounds

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 avatar Nov 07 '17 12:11 oferRounds

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: https://github.com/Estimote/Android-SDK/issues/62#issuecomment-342519115

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

oferRounds avatar Nov 07 '17 16:11 oferRounds

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

innyso avatar Dec 12 '17 12:12 innyso

@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<Props> { 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 ( <View style={styles.container}> <Text style={styles.welcome}> Welcome to React Native! </Text> <Text style={styles.instructions}> To get started, edit App.js </Text> <Text style={styles.instructions}> {instructions} </Text> </View> ); } }

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 avatar Aug 07 '18 09:08 d0uub

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!

d0uub avatar Aug 08 '18 06:08 d0uub

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!

Gillinghammer avatar Dec 17 '18 11:12 Gillinghammer

@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!

ger86 avatar Jan 16 '19 00:01 ger86